Skip to content

Commit dc6a7e0

Browse files
authored
[Float] Don't preload images inside <noscript> (#28815)
`<noscript>` scopes should be considered inert from the perspective of Fizz since we assume they'll only be used in rare and adverse circumstances. When we added preload support for img tags we did not include the noscript scope check in the opt-out for preloading. This change adds it in fixes: #27910
1 parent 3f947b1 commit dc6a7e0

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,7 +2762,7 @@ function pushImg(
27622762
props: Object,
27632763
resumableState: ResumableState,
27642764
renderState: RenderState,
2765-
pictureTagInScope: boolean,
2765+
pictureOrNoScriptTagInScope: boolean,
27662766
): null {
27672767
const {src, srcSet} = props;
27682768
if (
@@ -2771,7 +2771,7 @@ function pushImg(
27712771
(typeof src === 'string' || src == null) &&
27722772
(typeof srcSet === 'string' || srcSet == null) &&
27732773
props.fetchPriority !== 'low' &&
2774-
pictureTagInScope === false &&
2774+
pictureOrNoScriptTagInScope === false &&
27752775
// We exclude data URIs in src and srcSet since these should not be preloaded
27762776
!(
27772777
typeof src === 'string' &&
@@ -3599,7 +3599,7 @@ export function pushStartInstance(
35993599
props,
36003600
resumableState,
36013601
renderState,
3602-
!!(formatContext.tagScope & PICTURE_SCOPE),
3602+
!!(formatContext.tagScope & (PICTURE_SCOPE | NOSCRIPT_SCOPE)),
36033603
);
36043604
}
36053605
// Omitted close tags

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4261,6 +4261,38 @@ body {
42614261
);
42624262
});
42634263

4264+
// Fixes: https://github.com/facebook/react/issues/27910
4265+
it('omits preloads for images inside noscript tags', async () => {
4266+
function App() {
4267+
return (
4268+
<html>
4269+
<body>
4270+
<img src="foo" />
4271+
<noscript>
4272+
<img src="bar" />
4273+
</noscript>
4274+
</body>
4275+
</html>
4276+
);
4277+
}
4278+
4279+
await act(() => {
4280+
renderToPipeableStream(<App />).pipe(writable);
4281+
});
4282+
4283+
expect(getMeaningfulChildren(document)).toEqual(
4284+
<html>
4285+
<head>
4286+
<link rel="preload" href="foo" as="image" />
4287+
</head>
4288+
<body>
4289+
<img src="foo" />
4290+
<noscript>&lt;img src="bar"&gt;</noscript>
4291+
</body>
4292+
</html>,
4293+
);
4294+
});
4295+
42644296
it('should handle media on image preload', async () => {
42654297
function App({isClient}) {
42664298
ReactDOM.preload('/server', {

0 commit comments

Comments
 (0)