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
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Support headers [crossOrigin and referralPolicy] in Image without srcset or src and only source.uri",
"packageName": "react-native-windows",
"email": "54227869+anupriya13@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ exports[`Text Tests Text can have advanced borders 1`] = `
"__Children": [
{
"Offset": "0, 0, 0",
"Size": "916, 40",
"Size": "916, 39",
"Visual Type": "SpriteVisual",
"__Children": [
{
"Offset": "0, 0, 0",
"Size": "916, 40",
"Size": "916, 39",
"Visual Type": "SpriteVisual",
"__Children": [
{
Expand Down Expand Up @@ -245,7 +245,7 @@ exports[`Text Tests Text can have advanced borders 1`] = `
"Color": "rgba(255, 0, 0, 255)",
},
"Offset": "-10, 20, 0",
"Size": "10, 14",
"Size": "10, 13",
"Visual Type": "SpriteVisual",
},
{
Expand Down Expand Up @@ -281,7 +281,7 @@ exports[`Text Tests Text can have advanced borders 1`] = `
"Color": "rgba(255, 0, 0, 255)",
},
"Offset": "0, 22, 0",
"Size": "20, 10",
"Size": "20, 9",
"Visual Type": "SpriteVisual",
},
],
Expand All @@ -290,12 +290,12 @@ exports[`Text Tests Text can have advanced borders 1`] = `
},
{
"Offset": "0, 38, 0",
"Size": "916, 39",
"Size": "916, 40",
"Visual Type": "SpriteVisual",
"__Children": [
{
"Offset": "0, 0, 0",
"Size": "916, 39",
"Size": "916, 40",
"Visual Type": "SpriteVisual",
"__Children": [
{
Expand Down Expand Up @@ -331,7 +331,7 @@ exports[`Text Tests Text can have advanced borders 1`] = `
"Color": "rgba(0, 0, 255, 255)",
},
"Offset": "-10, 20, 0",
"Size": "10, 13",
"Size": "10, 14",
"Visual Type": "SpriteVisual",
},
{
Expand Down Expand Up @@ -367,7 +367,7 @@ exports[`Text Tests Text can have advanced borders 1`] = `
"Color": "rgba(0, 0, 255, 255)",
},
"Offset": "0, 22, 0",
"Size": "20, 9",
"Size": "20, 10",
"Visual Type": "SpriteVisual",
},
],
Expand Down Expand Up @@ -548,7 +548,7 @@ exports[`Text Tests Text can have borders 1`] = `
"Visual Tree": {
"Comment": "text-border",
"Offset": "0, 0, 0",
"Size": "916, 385",
"Size": "916, 384",
"Visual Type": "SpriteVisual",
"__Children": [
{
Expand Down Expand Up @@ -661,12 +661,12 @@ exports[`Text Tests Text can have borders 1`] = `
},
{
"Offset": "0, 365, 0",
"Size": "916, 19",
"Size": "916, 20",
"Visual Type": "SpriteVisual",
"__Children": [
{
"Offset": "0, 0, 0",
"Size": "916, 19",
"Size": "916, 20",
"Visual Type": "SpriteVisual",
},
],
Expand Down Expand Up @@ -761,7 +761,7 @@ exports[`Text Tests Text can have inline views/images 1`] = `
"Visual Tree": {
"Comment": "text-view",
"Offset": "0, 0, 0",
"Size": "916, 26",
"Size": "916, 23",
"Visual Type": "SpriteVisual",
"__Children": [
{
Expand All @@ -781,13 +781,13 @@ exports[`Text Tests Text can have inline views/images 1`] = `
],
},
{
"Offset": "384, 0, 0",
"Size": "34, 23",
"Offset": "384, 18, 0",
"Size": "1, 1",
"Visual Type": "SpriteVisual",
"__Children": [
{
"Offset": "0, 0, 0",
"Size": "34, 23",
"Size": "1, 1",
"Visual Type": "SpriteVisual",
},
],
Expand Down Expand Up @@ -845,17 +845,17 @@ exports[`Text Tests Text can have nested views 1`] = `
"Visual Tree": {
"Comment": "text-nested-view",
"Offset": "0, 0, 0",
"Size": "916, 40",
"Size": "916, 41",
"Visual Type": "SpriteVisual",
"__Children": [
{
"Offset": "0, 0, 0",
"Size": "916, 20",
"Size": "916, 19",
"Visual Type": "SpriteVisual",
"__Children": [
{
"Offset": "0, 0, 0",
"Size": "916, 20",
"Size": "916, 19",
"Visual Type": "SpriteVisual",
},
],
Expand Down Expand Up @@ -913,7 +913,7 @@ exports[`Text Tests Text can have shadows 1`] = `
"Visual Tree": {
"Comment": "text-shadow",
"Offset": "0, 0, 0",
"Size": "916, 28",
"Size": "916, 27",
"Visual Type": "SpriteVisual",
},
}
Expand Down Expand Up @@ -1031,7 +1031,7 @@ exports[`Text Tests Texts can clip inline View/Images 1`] = `
"Visual Tree": {
"Comment": "text-view-images-clipped",
"Offset": "0, 0, 0",
"Size": "916, 223",
"Size": "916, 222",
"Visual Type": "SpriteVisual",
"__Children": [
{
Expand Down Expand Up @@ -1094,12 +1094,12 @@ exports[`Text Tests Texts can clip inline View/Images 1`] = `
},
{
"Offset": "0, 103, 0",
"Size": "916, 19",
"Size": "916, 20",
"Visual Type": "SpriteVisual",
"__Children": [
{
"Offset": "0, 0, 0",
"Size": "916, 19",
"Size": "916, 20",
"Visual Type": "SpriteVisual",
},
],
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/Samples/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ export default class Bootstrap extends React.Component<
onLoadStart={() => console.log('onLoadStart')}
onLoadEnd={() => console.log('onLoadEnd')}
onProgress={this.handleOnProgress}
crossOrigin="use-credentials"
referrerPolicy="no-referrer"
/* srcSet={this.state.imageUri}*/
/>
)}
</View>
Expand Down
7 changes: 7 additions & 0 deletions vnext/overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,13 @@
"baseHash": "a5abee6de7dca3cb043b834925de3f6f0443c738",
"issue": 4590
},
{
"type": "patch",
"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?

},
{
"type": "patch",
"file": "src-win/Libraries/Image/resolveAssetSource.windows.js",
Expand Down
84 changes: 84 additions & 0 deletions vnext/src-win/Libraries/Image/ImageSourceUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

import type {ResolvedAssetSource} from './AssetSourceResolver';
import type {ImageProps} from './ImageProps';

import resolveAssetSource from './resolveAssetSource';

/**
* A function which returns the appropriate value for image source
* by resolving the `source`, `src` and `srcSet` props.
*/
export function getImageSourcesFromImageProps(
imageProps: ImageProps,
): ?ResolvedAssetSource | $ReadOnlyArray<{uri: string, ...}> {
let source = resolveAssetSource(imageProps.source);

let sources;

const {crossOrigin, referrerPolicy, src, srcSet, width, height} = imageProps;

const headers: {[string]: string} = {};
if (crossOrigin === 'use-credentials') {
headers['Access-Control-Allow-Credentials'] = 'true';
}
if (referrerPolicy != null) {
headers['Referrer-Policy'] = referrerPolicy;
}
if (srcSet != null) {
const sourceList = [];
const srcSetList = srcSet.split(', ');
// `src` prop should be used with default scale if `srcSet` does not have 1x scale.
let shouldUseSrcForDefaultScale = true;
srcSetList.forEach(imageSrc => {
const [uri, xScale = '1x'] = imageSrc.split(' ');
if (!xScale.endsWith('x')) {
console.warn(
'The provided format for scale is not supported yet. Please use scales like 1x, 2x, etc.',
);
} else {
const scale = parseInt(xScale.split('x')[0], 10);
if (!isNaN(scale)) {
// 1x scale is provided in `srcSet` prop so ignore the `src` prop if provided.
shouldUseSrcForDefaultScale =
scale === 1 ? false : shouldUseSrcForDefaultScale;
sourceList.push({headers: headers, scale, uri, width, height});
}
}
});

if (shouldUseSrcForDefaultScale && src != null) {
sourceList.push({
headers: headers,
scale: 1,
uri: src,
width,
height,
});
}
if (sourceList.length === 0) {
console.warn('The provided value for srcSet is not valid.');
}

sources = sourceList;
} 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}];
  }

sources = [{...source, headers}];
} // [Windows
else {
sources = source;
}
return sources;
}
Loading