Skip to content

Commit 5fe0371

Browse files
committed
When src or srcSet is a data URI we should not preload the image
1 parent 4e3618a commit 5fe0371

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,14 +2392,32 @@ function pushImg(
23922392
props: Object,
23932393
resources: Resources,
23942394
): null {
2395+
const {src, srcSet} = props;
23952396
if (
23962397
props.loading !== 'lazy' &&
2397-
typeof props.src === 'string' &&
2398-
props.fetchPriority !== 'low'
2398+
(typeof src === 'string' || typeof srcSet === 'string') &&
2399+
props.fetchPriority !== 'low' &&
2400+
// We exclude data URIs in src and srcSet since these should not be preloaded
2401+
!(
2402+
typeof src === 'string' &&
2403+
src[4] === ':' &&
2404+
(src[0] === 'd' || src[0] === 'D') &&
2405+
(src[1] === 'a' || src[1] === 'A') &&
2406+
(src[2] === 't' || src[2] === 'T') &&
2407+
(src[3] === 'a' || src[3] === 'A')
2408+
) &&
2409+
!(
2410+
typeof srcSet === 'string' &&
2411+
srcSet[4] === ':' &&
2412+
(srcSet[0] === 'd' || srcSet[0] === 'D') &&
2413+
(srcSet[1] === 'a' || srcSet[1] === 'A') &&
2414+
(srcSet[2] === 't' || srcSet[2] === 'T') &&
2415+
(srcSet[3] === 'a' || srcSet[3] === 'A')
2416+
)
23992417
) {
24002418
// We have a suspensey image and ought to preload it to optimize the loading of display blocking
24012419
// resources.
2402-
const {src, srcSet, sizes} = props;
2420+
const {sizes} = props;
24032421
const key = getImagePreloadKey(src, srcSet, sizes);
24042422
let resource = resources.preloadsMap.get(key);
24052423
if (!resource) {

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3969,6 +3969,36 @@ body {
39693969
);
39703970
});
39713971

3972+
it('should not preload images that have a data URIs for src or srcSet', async () => {
3973+
function App() {
3974+
return (
3975+
<html>
3976+
<body>
3977+
<img src="data:1" />
3978+
<img src="data:2" srcSet="ss2" />
3979+
<img srcSet="data:3a, data:3b 2x" />
3980+
<img src="4" srcSet="data:4a, data4b 2x" />
3981+
</body>
3982+
</html>
3983+
);
3984+
}
3985+
await act(() => {
3986+
renderToPipeableStream(<App />).pipe(writable);
3987+
});
3988+
3989+
expect(getMeaningfulChildren(document)).toEqual(
3990+
<html>
3991+
<head />
3992+
<body>
3993+
<img src="data:1" />
3994+
<img src="data:2" srcset="ss2" />
3995+
<img srcset="data:3a, data:3b 2x" />
3996+
<img src="4" srcset="data:4a, data4b 2x" />
3997+
</body>
3998+
</html>,
3999+
);
4000+
});
4001+
39724002
describe('ReactDOM.prefetchDNS(href)', () => {
39734003
it('creates a dns-prefetch resource when called', async () => {
39744004
function App({url}) {

0 commit comments

Comments
 (0)