Skip to content

[Fizz] Always load the external runtime if one is provided #33091

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

Merged
merged 1 commit into from
May 1, 2025
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
7 changes: 1 addition & 6 deletions packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -5089,15 +5089,10 @@ export function writePreambleStart(
destination: Destination,
resumableState: ResumableState,
renderState: RenderState,
willFlushAllSegments: boolean,
skipExpect?: boolean, // Used as an override by ReactFizzConfigMarkup
): void {
// This function must be called exactly once on every request
if (
enableFizzExternalRuntime &&
!willFlushAllSegments &&
renderState.externalRuntimeScript
) {
if (enableFizzExternalRuntime && renderState.externalRuntimeScript) {
// If the root segment is incomplete due to suspended tasks
// (e.g. willFlushAllSegments = false) and we are using data
// streaming format, ensure the external runtime is sent.
Expand Down
29 changes: 21 additions & 8 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3587,6 +3587,9 @@ describe('ReactDOMFizzServer', () => {
'<script type="importmap">' +
JSON.stringify(importMap) +
'</script><script async="" src="foo"></script>' +
(gate(flags => flags.shouldUseFizzExternalRuntime)
? '<script src="react-dom-bindings/src/server/ReactDOMServerExternalRuntime.js" async=""></script>'
: '') +
'<link rel="expect" href="#«R»" blocking="render">',
);
});
Expand Down Expand Up @@ -4501,7 +4504,8 @@ describe('ReactDOMFizzServer', () => {
expect(document.getElementsByTagName('script').length).toEqual(1);
});

it('does not send the external runtime for static pages', async () => {
// @gate shouldUseFizzExternalRuntime
it('does (unfortunately) send the external runtime for static pages', async () => {
await act(() => {
const {pipe} = renderToPipeableStream(
<html>
Expand All @@ -4515,11 +4519,11 @@ describe('ReactDOMFizzServer', () => {
});

// no scripts should be sent
expect(document.getElementsByTagName('script').length).toEqual(0);
expect(document.getElementsByTagName('script').length).toEqual(1);

// the html should be as-is
expect(document.documentElement.innerHTML).toEqual(
'<head><link rel="expect" href="#«R»" blocking="render"></head><body><p>hello world!</p><template id="«R»"></template></body>',
'<head><script src="react-dom-bindings/src/server/ReactDOMServerExternalRuntime.js" async=""></script><link rel="expect" href="#«R»" blocking="render"></head><body><p>hello world!</p><template id="«R»"></template></body>',
);
});

Expand Down Expand Up @@ -5317,7 +5321,9 @@ describe('ReactDOMFizzServer', () => {
});

expect(container.innerHTML).toEqual(
'<div>hello<b>world, <!-- -->Foo</b>!</div>',
(gate(flags => flags.shouldUseFizzExternalRuntime)
? '<script src="react-dom-bindings/src/server/ReactDOMServerExternalRuntime.js" async=""></script>'
: '') + '<div>hello<b>world, <!-- -->Foo</b>!</div>',
);
const errors = [];
ReactDOMClient.hydrateRoot(container, <App name="Foo" />, {
Expand Down Expand Up @@ -5518,7 +5524,7 @@ describe('ReactDOMFizzServer', () => {
pipe(writable);
});

expect(container.firstElementChild.outerHTML).toEqual(
expect(container.lastElementChild.outerHTML).toEqual(
'<div>hello<b>world<!-- --></b></div>',
);

Expand Down Expand Up @@ -5556,7 +5562,7 @@ describe('ReactDOMFizzServer', () => {
pipe(writable);
});

expect(container.firstElementChild.outerHTML).toEqual(
expect(container.lastElementChild.outerHTML).toEqual(
'<div>hello<b>world</b></div>',
);

Expand Down Expand Up @@ -5696,7 +5702,10 @@ describe('ReactDOMFizzServer', () => {
});

expect(container.innerHTML).toEqual(
'<div><!--$-->hello<!-- -->world<!-- --><!--/$--><!--$-->world<!-- --><!--/$--><!--$-->hello<!-- -->world<!-- --><br><!--/$--><!--$-->world<!-- --><br><!--/$--></div>',
(gate(flags => flags.shouldUseFizzExternalRuntime)
? '<script src="react-dom-bindings/src/server/ReactDOMServerExternalRuntime.js" async=""></script>'
: '') +
'<div><!--$-->hello<!-- -->world<!-- --><!--/$--><!--$-->world<!-- --><!--/$--><!--$-->hello<!-- -->world<!-- --><br><!--/$--><!--$-->world<!-- --><br><!--/$--></div>',
);

const errors = [];
Expand Down Expand Up @@ -6499,7 +6508,11 @@ describe('ReactDOMFizzServer', () => {
});

expect(document.documentElement.outerHTML).toEqual(
'<html><head><link rel="expect" href="#«R»" blocking="render"></head><body><script>try { foo() } catch (e) {} ;</script><template id="«R»"></template></body></html>',
'<html><head>' +
(gate(flags => flags.shouldUseFizzExternalRuntime)
? '<script src="react-dom-bindings/src/server/ReactDOMServerExternalRuntime.js" async=""></script>'
: '') +
'<link rel="expect" href="#«R»" blocking="render"></head><body><script>try { foo() } catch (e) {} ;</script><template id="«R»"></template></body></html>',
);
});

Expand Down
3 changes: 3 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFloat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,9 @@ describe('ReactDOMFloat', () => {
});
expect(chunks).toEqual([
'<!DOCTYPE html><html><head><script async="" src="foo"></script>' +
(gate(flags => flags.shouldUseFizzExternalRuntime)
? '<script src="react-dom/unstable_server-external-runtime" async=""></script>'
: '') +
'<link rel="expect" href="#«R»" blocking="render"/><title>foo</title></head>' +
'<body>bar<template id="«R»"></template>',
'</body></html>',
Expand Down
2 changes: 0 additions & 2 deletions packages/react-markup/src/ReactFizzConfigMarkup.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,12 @@ export function writePreambleStart(
destination: Destination,
resumableState: ResumableState,
renderState: RenderState,
willFlushAllSegments: boolean,
skipExpect?: boolean, // Used as an override by ReactFizzConfigMarkup
): void {
return writePreambleStartImpl(
destination,
resumableState,
renderState,
willFlushAllSegments,
true, // skipExpect
);
}
Expand Down
9 changes: 1 addition & 8 deletions packages/react-server/src/ReactFizzServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4835,14 +4835,7 @@ function flushPreamble(
preambleSegments: Array<Array<Segment>>,
) {
// The preamble is ready.
const willFlushAllSegments =
request.allPendingTasks === 0 && request.trackedPostpones === null;
writePreambleStart(
destination,
request.resumableState,
request.renderState,
willFlushAllSegments,
);
writePreambleStart(destination, request.resumableState, request.renderState);
for (let i = 0; i < preambleSegments.length; i++) {
const segments = preambleSegments[i];
for (let j = 0; j < segments.length; j++) {
Expand Down
Loading