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

[Fizz][Float] When src or srcSet is a data URI we should not preload the image #27218

Merged
merged 1 commit into from
Aug 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -2392,14 +2392,32 @@ function pushImg(
props: Object,
resources: Resources,
): null {
const {src, srcSet} = props;
if (
props.loading !== 'lazy' &&
typeof props.src === 'string' &&
props.fetchPriority !== 'low'
(typeof src === 'string' || typeof srcSet === 'string') &&
props.fetchPriority !== 'low' &&
// We exclude data URIs in src and srcSet since these should not be preloaded
!(
typeof src === 'string' &&
src[4] === ':' &&
(src[0] === 'd' || src[0] === 'D') &&
(src[1] === 'a' || src[1] === 'A') &&
(src[2] === 't' || src[2] === 'T') &&
(src[3] === 'a' || src[3] === 'A')
gnoff marked this conversation as resolved.
Show resolved Hide resolved
) &&
!(
typeof srcSet === 'string' &&
srcSet[4] === ':' &&
(srcSet[0] === 'd' || srcSet[0] === 'D') &&
(srcSet[1] === 'a' || srcSet[1] === 'A') &&
(srcSet[2] === 't' || srcSet[2] === 'T') &&
(srcSet[3] === 'a' || srcSet[3] === 'A')
gnoff marked this conversation as resolved.
Show resolved Hide resolved
)
) {
// We have a suspensey image and ought to preload it to optimize the loading of display blocking
// resources.
const {src, srcSet, sizes} = props;
const {sizes} = props;
const key = getImagePreloadKey(src, srcSet, sizes);
let resource = resources.preloadsMap.get(key);
if (!resource) {
Expand Down
30 changes: 30 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFloat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3969,6 +3969,36 @@ body {
);
});

it('should not preload images that have a data URIs for src or srcSet', async () => {
function App() {
return (
<html>
<body>
<img src="data:1" />
<img src="data:2" srcSet="ss2" />
<img srcSet="data:3a, data:3b 2x" />
<img src="4" srcSet="data:4a, data4b 2x" />
</body>
</html>
);
}
await act(() => {
renderToPipeableStream(<App />).pipe(writable);
});

expect(getMeaningfulChildren(document)).toEqual(
<html>
<head />
<body>
<img src="data:1" />
<img src="data:2" srcset="ss2" />
<img srcset="data:3a, data:3b 2x" />
<img src="4" srcset="data:4a, data4b 2x" />
</body>
</html>,
);
});

describe('ReactDOM.prefetchDNS(href)', () => {
it('creates a dns-prefetch resource when called', async () => {
function App({url}) {
Expand Down