Skip to content

Commit

Permalink
[Float] Don't preload images inside <noscript> (#28815)
Browse files Browse the repository at this point in the history
`<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
  • Loading branch information
gnoff authored and rickhanlonii committed Apr 11, 2024
1 parent 4151551 commit 7c86590
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -2762,7 +2762,7 @@ function pushImg(
props: Object,
resumableState: ResumableState,
renderState: RenderState,
pictureTagInScope: boolean,
pictureOrNoScriptTagInScope: boolean,
): null {
const {src, srcSet} = props;
if (
Expand All @@ -2771,7 +2771,7 @@ function pushImg(
(typeof src === 'string' || src == null) &&
(typeof srcSet === 'string' || srcSet == null) &&
props.fetchPriority !== 'low' &&
pictureTagInScope === false &&
pictureOrNoScriptTagInScope === false &&
// We exclude data URIs in src and srcSet since these should not be preloaded
!(
typeof src === 'string' &&
Expand Down Expand Up @@ -3599,7 +3599,7 @@ export function pushStartInstance(
props,
resumableState,
renderState,
!!(formatContext.tagScope & PICTURE_SCOPE),
!!(formatContext.tagScope & (PICTURE_SCOPE | NOSCRIPT_SCOPE)),
);
}
// Omitted close tags
Expand Down
32 changes: 32 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFloat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4261,6 +4261,38 @@ body {
);
});

// Fixes: https://github.com/facebook/react/issues/27910
it('omits preloads for images inside noscript tags', async () => {
function App() {
return (
<html>
<body>
<img src="foo" />
<noscript>
<img src="bar" />
</noscript>
</body>
</html>
);
}

await act(() => {
renderToPipeableStream(<App />).pipe(writable);
});

expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link rel="preload" href="foo" as="image" />
</head>
<body>
<img src="foo" />
<noscript>&lt;img src="bar"&gt;</noscript>
</body>
</html>,
);
});

it('should handle media on image preload', async () => {
function App({isClient}) {
ReactDOM.preload('/server', {
Expand Down

0 comments on commit 7c86590

Please sign in to comment.