Skip to content
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

Add referrerPolicy option to ReactDOM.preload() #27096

Merged
merged 2 commits into from Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2284,6 +2284,7 @@ function preloadPropsFromPreloadOptions(
fetchPriority: options.fetchPriority,
imageSrcSet: options.imageSrcSet,
imageSizes: options.imageSizes,
referrerPolicy: options.referrerPolicy,
};
}

Expand Down
Expand Up @@ -5564,6 +5564,7 @@ function preloadPropsFromPreloadOptions(
fetchPriority: options.fetchPriority,
imageSrcSet: options.imageSrcSet,
imageSizes: options.imageSizes,
referrerPolicy: options.referrerPolicy,
};
}

Expand Down
85 changes: 85 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFloat-test.js
Expand Up @@ -3686,6 +3686,91 @@ body {
);
});

it('should handle referrerPolicy on image preload', async () => {
function App({isClient}) {
ReactDOM.preload('/server', {
as: 'image',
imageSrcSet: '/server',
imageSizes: '100vw',
referrerPolicy: 'no-referrer',
});

if (isClient) {
ReactDOM.preload('/client', {
as: 'image',
imageSrcSet: '/client',
imageSizes: '100vw',
referrerPolicy: 'no-referrer',
});
}

return (
<html>
<body>hello</body>
</html>
);
}

await act(() => {
renderToPipeableStream(<App />).pipe(writable);
});
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link
rel="preload"
as="image"
imagesrcset="/server"
imagesizes="100vw"
referrerpolicy="no-referrer"
/>
</head>
<body>hello</body>
</html>,
);

const root = ReactDOMClient.hydrateRoot(document, <App />);
await waitForAll([]);
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link
rel="preload"
as="image"
imagesrcset="/server"
imagesizes="100vw"
referrerpolicy="no-referrer"
/>
</head>
<body>hello</body>
</html>,
);

root.render(<App isClient={true} />);
await waitForAll([]);
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link
rel="preload"
as="image"
imagesrcset="/server"
imagesizes="100vw"
referrerpolicy="no-referrer"
/>
<link
rel="preload"
as="image"
imagesrcset="/client"
imagesizes="100vw"
referrerpolicy="no-referrer"
/>
</head>
<body>hello</body>
</html>,
);
});

describe('ReactDOM.prefetchDNS(href)', () => {
it('creates a dns-prefetch resource when called', async () => {
function App({url}) {
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/src/shared/ReactDOMTypes.js
Expand Up @@ -18,6 +18,7 @@ export type PreloadOptions = {
fetchPriority?: 'high' | 'low' | 'auto',
imageSrcSet?: string,
imageSizes?: string,
referrerPolicy?: string,
};
export type PreinitOptions = {
as: string,
Expand Down