Skip to content

Support headers [crossOrigin and referralPolicy] in Image without src and srcSet and only remote source.uri #14521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

anupriya13
Copy link
Contributor

@anupriya13 anupriya13 commented Apr 13, 2025

Description

Type of Change

  • New feature (non-breaking change which adds functionality)

Why

What is the motivation for this change? Add a few sentences describing the context and overall goals of the pull request's commits.

Implement the crossOrigin property for Image in RNW Fabric.

For reference, check the public API documentation: https://reactnative.dev/docs/image#crossorigin

Implement the referrerPolicy property for Image in RNW Fabric.

For reference, check the public API documentation: https://reactnative.dev/docs/image#referrerpolicy

Also refer docs for source, src, srcSet
https://reactnative.dev/docs/image#source
https://reactnative.dev/docs/image#src
https://reactnative.dev/docs/image#srcset

Resolves #14537

Related #13746 and #13754

What

What changes were made to the codebase to solve the bug, add the functionality, etc. that you specified above.

It's not mentioned in the doc that when src / srcSet is missing then crossOrigin / referralPolicy would be ignored when source uri is a remote URL that is passed.

Currently these were ignored if src / srcSet was not passed and not added to sources headers.

This change adds headers support even without passing src / srcSet and only sources uri that consists of remote URL.

crossOrigin and referrerPolicy are passed as source.headers here:

import {getImageSourcesFromImageProps} from './ImageSourceUtils';

Added below code in line 76 of ImageSourceUtils.js:

 else if (source != null && source.uri) {
    sources = [{...source, headers}];
  }
image

Screenshots

Add any relevant screen captures here from before or after your changes.
Before
image

After
image

Testing

If you added tests that prove your changes are effective or that your feature works, add a few sentences here detailing the added test scenarios.

Tested in playground and RNW Tester and Visual Studio Debugger

Optional: Describe the tests that you ran locally to verify your changes.

  1. Tested with only source remote uri passed
  2. Tested with both source, src
  3. Tested with source, src, srcSet
 <Image
              defaultSource={{uri: this.state.defaultImageUri}}
              source={{uri: this.state.imageUri}}
              loadingIndicatorSource={{uri: loadingImageUri}}
              resizeMode={this.state.selectedResizeMode}
              blurRadius={this.state.blurRadius}
              onLoad={() => console.log('onLoad')}
              onLoadStart={() => console.log('onLoadStart')}
              onLoadEnd={() => console.log('onLoadEnd')}
              onProgress={this.handleOnProgress}
              crossOrigin="use-credentials"
              referrerPolicy="no-referrer"
              /* srcSet={this.state.imageUri}*/
            />

Changelog

Should this change be included in the release notes: indicate yes or no Yes

Add a brief summary of the change to use in the release notes for the next release.
Support headers [crossOrigin and referralPolicy] in Image without src and srcSet and only remote source.uri

Microsoft Reviewers: Open in CodeFlow

@anupriya13 anupriya13 linked an issue Apr 13, 2025 that may be closed by this pull request
@anupriya13 anupriya13 marked this pull request as ready for review April 14, 2025 02:54
@anupriya13 anupriya13 requested a review from a team as a code owner April 14, 2025 02:54
@anupriya13 anupriya13 requested a review from acoates-ms April 14, 2025 04:26
} else if (src != null) {
sources = [{uri: src, headers: headers, width, height}];
} // [Windows
else if (source != null && source.uri) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this different on windows? -- Is this something that is missing on the other platforms in core too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be on other platforms too. Not able to test exactly in other platforms. I followed instructions for overriding and it mentioned to add comment for Windows.

Copy link
Contributor

@TatianaKapos TatianaKapos Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next, make you changes to that new file. Be sure to clearly mark your changes by surrounding code blocks with starting // [Windows and ending // Windows] comments. For this example, here we're adding a new else if clause to an if statement performing a platform check:

Just clarifying here, the instructions state to surround any changes you made in the file with window comments. Did you change this file at all? Are the changes here not in the upstream file? If not, we can follow-up and fix it upstream :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I did change the file and added this else if condition.
Shall I remove the "Windows" comment then as the fix would be required in all
Platforms?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added below code in line 76 of ImageSourceUtils.js:

 else if (source != null && source.uri) {
    sources = [{...source, headers}];
  }

"file": "src-win/Libraries/Image/ImageSourceUtils.js",
"baseFile": "packages/react-native/Libraries/Image/ImageSourceUtils.js",
"baseHash": "136d9dad53466468b288e2fb1b05adbb18c63ed1",
"issue": 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the patch made in ImageSourceUtils.js needed in upstream too? If so, we will want to create an issue and link it here to fix upstream

Copy link
Contributor Author

@anupriya13 anupriya13 Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay, I have already created an issue. Let me update the issue number then.
Yes, it's needed in upstream! It looks like a bug.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a bug in upstream, and one that doesn't have any current partner ask to require an immediate fix in windows, we should just fix the issue upstream and save the effort of forking/unforking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@acoates-ms Do you mean I should raise this PR in react-native repo instead?

@anupriya13
Copy link
Contributor Author

@acoates-ms @TatianaKapos I have raised issue for Meta here facebook/react-native#50778

@anupriya13 anupriya13 closed this Apr 18, 2025
facebook-github-bot pushed a commit to facebook/react-native that referenced this pull request Apr 24, 2025
… and srcSet and only remote source.uri (#50799)

Summary:
Resolves #50778

### Problem Description

Implement the crossOrigin property for Image in RNW Fabric when only source.uri is passed and not src/ srcSet.

For reference, check the public API documentation: https://reactnative.dev/docs/image#crossorigin

Implement the referrerPolicy property for Image in RNW Fabric when only source.uri is passed and not src/ srcSet.

For reference, check the public API documentation: https://reactnative.dev/docs/image#referrerpolicy

Also refer docs for source, src, srcSet
https://reactnative.dev/docs/image#source
https://reactnative.dev/docs/image#src
https://reactnative.dev/docs/image#srcset

It's not mentioned in the doc that when src / srcSet is missing then crossOrigin / referralPolicy would be ignored when source uri is a remote URL that is passed.

Currently these were ignored if src / srcSet was not passed and not added to sources headers.

This change adds headers support even without passing src / srcSet and only sources uri that consists of remote URL.

crossOrigin and referrerPolicy are passed as source.headers here:

![Image](https://github.com/user-attachments/assets/6df1a274-353e-4e03-9033-75695d04e2c0)

### Steps to reproduce

```
 <Image
              defaultSource={{uri: this.state.defaultImageUri}}
              source={{uri: this.state.imageUri}}
              crossOrigin="use-credentials"
              referrerPolicy="no-referrer"
            />
```

Pass this in React Native and check source headers

## Changelog:
[GENERAL] [ADDED] - Support headers [crossOrigin and referralPolicy] in Image without src and srcSet and only remote source.uri

Pull Request resolved: #50799

Test Plan:
Refer this PR for testing; microsoft/react-native-windows#14521
## Screenshots
_Add any relevant screen captures here from before or after your changes._
Before
![image](https://github.com/user-attachments/assets/21804411-91f4-4a27-b6e9-971675cbd546)

After
<img width="790" alt="image" src="https://github.com/user-attachments/assets/8e0a2522-7009-430d-b848-da80896670f4" />

## Testing
_If you added tests that prove your changes are effective or that your feature works, add a few sentences here detailing the added test scenarios._

Tested in playground and RNW Tester and Visual Studio Debugger

_Optional_: Describe the tests that you ran locally to verify your changes.

1. Tested with only source remote uri passed
2. Tested with both source, src
3. Tested with source, src, srcSet

Reviewed By: javache

Differential Revision: D73427747

Pulled By: cipolleschi

fbshipit-source-id: f09174d1e4eaa2173b27970a6079eeb8ba6f3069
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants