Skip to content

Commit 11a175f

Browse files
committed
Add media option to ReactDOM.preload()
1 parent d445cee commit 11a175f

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,7 @@ function preloadPropsFromPreloadOptions(
22852285
imageSrcSet: options.imageSrcSet,
22862286
imageSizes: options.imageSizes,
22872287
referrerPolicy: options.referrerPolicy,
2288+
media: options.media,
22882289
};
22892290
}
22902291

packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5565,6 +5565,7 @@ function preloadPropsFromPreloadOptions(
55655565
imageSrcSet: options.imageSrcSet,
55665566
imageSizes: options.imageSizes,
55675567
referrerPolicy: options.referrerPolicy,
5568+
media: options.media,
55685569
};
55695570
}
55705571

packages/react-dom/src/__tests__/ReactDOMFloat-test.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3771,6 +3771,91 @@ body {
37713771
);
37723772
});
37733773

3774+
it('should handle media on image preload', async () => {
3775+
function App({isClient}) {
3776+
ReactDOM.preload('/server', {
3777+
as: 'image',
3778+
imageSrcSet: '/server',
3779+
imageSizes: '100vw',
3780+
media: 'print and (min-width: 768px)',
3781+
});
3782+
3783+
if (isClient) {
3784+
ReactDOM.preload('/client', {
3785+
as: 'image',
3786+
imageSrcSet: '/client',
3787+
imageSizes: '100vw',
3788+
media: 'screen and (max-width: 480px)',
3789+
});
3790+
}
3791+
3792+
return (
3793+
<html>
3794+
<body>hello</body>
3795+
</html>
3796+
);
3797+
}
3798+
3799+
await act(() => {
3800+
renderToPipeableStream(<App />).pipe(writable);
3801+
});
3802+
expect(getMeaningfulChildren(document)).toEqual(
3803+
<html>
3804+
<head>
3805+
<link
3806+
rel="preload"
3807+
as="image"
3808+
imagesrcset="/server"
3809+
imagesizes="100vw"
3810+
media="print and (min-width: 768px)"
3811+
/>
3812+
</head>
3813+
<body>hello</body>
3814+
</html>,
3815+
);
3816+
3817+
const root = ReactDOMClient.hydrateRoot(document, <App />);
3818+
await waitForAll([]);
3819+
expect(getMeaningfulChildren(document)).toEqual(
3820+
<html>
3821+
<head>
3822+
<link
3823+
rel="preload"
3824+
as="image"
3825+
imagesrcset="/server"
3826+
imagesizes="100vw"
3827+
media="print and (min-width: 768px)"
3828+
/>
3829+
</head>
3830+
<body>hello</body>
3831+
</html>,
3832+
);
3833+
3834+
root.render(<App isClient={true} />);
3835+
await waitForAll([]);
3836+
expect(getMeaningfulChildren(document)).toEqual(
3837+
<html>
3838+
<head>
3839+
<link
3840+
rel="preload"
3841+
as="image"
3842+
imagesrcset="/server"
3843+
imagesizes="100vw"
3844+
media="print and (min-width: 768px)"
3845+
/>
3846+
<link
3847+
rel="preload"
3848+
as="image"
3849+
imagesrcset="/client"
3850+
imagesizes="100vw"
3851+
media="screen and (max-width: 480px)"
3852+
/>
3853+
</head>
3854+
<body>hello</body>
3855+
</html>,
3856+
);
3857+
});
3858+
37743859
describe('ReactDOM.prefetchDNS(href)', () => {
37753860
it('creates a dns-prefetch resource when called', async () => {
37763861
function App({url}) {

packages/react-dom/src/shared/ReactDOMTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type PreloadOptions = {
1919
imageSrcSet?: string,
2020
imageSizes?: string,
2121
referrerPolicy?: string,
22+
media?: string,
2223
};
2324
export type PreinitOptions = {
2425
as: string,

0 commit comments

Comments
 (0)