Skip to content

Commit

Permalink
Add media option to ReactDOM.preload()
Browse files Browse the repository at this point in the history
  • Loading branch information
damnsamn committed Jul 19, 2023
1 parent d445cee commit 11a175f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,7 @@ function preloadPropsFromPreloadOptions(
imageSrcSet: options.imageSrcSet,
imageSizes: options.imageSizes,
referrerPolicy: options.referrerPolicy,
media: options.media,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5565,6 +5565,7 @@ function preloadPropsFromPreloadOptions(
imageSrcSet: options.imageSrcSet,
imageSizes: options.imageSizes,
referrerPolicy: options.referrerPolicy,
media: options.media,
};
}

Expand Down
85 changes: 85 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFloat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3771,6 +3771,91 @@ body {
);
});

it('should handle media on image preload', async () => {
function App({isClient}) {
ReactDOM.preload('/server', {
as: 'image',
imageSrcSet: '/server',
imageSizes: '100vw',
media: 'print and (min-width: 768px)',
});

if (isClient) {
ReactDOM.preload('/client', {
as: 'image',
imageSrcSet: '/client',
imageSizes: '100vw',
media: 'screen and (max-width: 480px)',
});
}

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"
media="print and (min-width: 768px)"
/>
</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"
media="print and (min-width: 768px)"
/>
</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"
media="print and (min-width: 768px)"
/>
<link
rel="preload"
as="image"
imagesrcset="/client"
imagesizes="100vw"
media="screen and (max-width: 480px)"
/>
</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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type PreloadOptions = {
imageSrcSet?: string,
imageSizes?: string,
referrerPolicy?: string,
media?: string,
};
export type PreinitOptions = {
as: string,
Expand Down

0 comments on commit 11a175f

Please sign in to comment.