Skip to content

Commit 34c6c03

Browse files
committed
ensure fallback bootstrap scripts get used in fallback cases
1 parent b0a175b commit 34c6c03

File tree

2 files changed

+101
-5
lines changed

2 files changed

+101
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,14 +2579,16 @@ export function prepareForFallback(responseState: ResponseState): void {
25792579
if (__DEV__) {
25802580
(responseState: any).inFallbackDEV = true;
25812581
}
2582-
// This function would ideally check somethign to see whether embedding
2583-
// was required however at the moment the only time we use a Request Fallback
2584-
// is when we use renderIntoDocument which is the only variant where which
2585-
// utilizes fallback children. We assume we're in that mode if this function
2586-
// is called and update the requirement accordingly
2582+
// Reset rendered states
25872583
responseState.htmlChunks = [];
25882584
responseState.headChunks = [];
25892585
responseState.rendered = NONE;
2586+
2587+
// Move fallback bootstrap to bootstrap if configured
2588+
const fallbackBootstrapChunks = responseState.fallbackBootstrapChunks;
2589+
if (fallbackBootstrapChunks && fallbackBootstrapChunks.length) {
2590+
responseState.bootstrapChunks = fallbackBootstrapChunks;
2591+
}
25902592
}
25912593

25922594
export function writeCompletedRoot(

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6367,5 +6367,99 @@ describe('ReactDOMFizzServer', () => {
63676367
</html>,
63686368
);
63696369
});
6370+
6371+
it('emits fallback bootstrap scripts if configured when rendering the fallback shell', async () => {
6372+
function Throw() {
6373+
throw new Error('uh oh');
6374+
}
6375+
6376+
let didBootstrap = false;
6377+
function bootstrap() {
6378+
didBootstrap = true;
6379+
}
6380+
window.__INIT__ = bootstrap;
6381+
6382+
let didFallback = false;
6383+
function fallback() {
6384+
didFallback = true;
6385+
}
6386+
window.__FALLBACK_INIT__ = fallback;
6387+
6388+
const errors = [];
6389+
await act(() => {
6390+
const {pipe} = renderIntoDocumentAsPipeableStream(
6391+
<html id="html">
6392+
<body id="body">
6393+
hello world
6394+
<Throw />
6395+
</body>
6396+
</html>,
6397+
<div>fallback</div>,
6398+
{
6399+
onError(err) {
6400+
errors.push(err.message);
6401+
},
6402+
bootstrapScriptContent: '__INIT__();',
6403+
fallbackBootstrapScriptContent: '__FALLBACK_INIT__();',
6404+
},
6405+
);
6406+
pipe(writable);
6407+
});
6408+
6409+
expect(getMeaningfulChildren(document)).toEqual(
6410+
<html>
6411+
<head />
6412+
<body>
6413+
<div>fallback</div>
6414+
</body>
6415+
</html>,
6416+
);
6417+
6418+
expect(didBootstrap).toBe(false);
6419+
expect(didFallback).toBe(true);
6420+
});
6421+
6422+
it('emits bootstrap scripts if no fallback bootstrap scripts are configured when rendering the fallback shell', async () => {
6423+
function Throw() {
6424+
throw new Error('uh oh');
6425+
}
6426+
6427+
let didBootstrap = false;
6428+
function bootstrap() {
6429+
didBootstrap = true;
6430+
}
6431+
window.__INIT__ = bootstrap;
6432+
6433+
const errors = [];
6434+
await act(() => {
6435+
const {pipe} = renderIntoDocumentAsPipeableStream(
6436+
<html id="html">
6437+
<body id="body">
6438+
<Throw />
6439+
hello world
6440+
</body>
6441+
</html>,
6442+
<div>fallback</div>,
6443+
{
6444+
onError(err) {
6445+
errors.push(err.message);
6446+
},
6447+
bootstrapScriptContent: '__INIT__();',
6448+
},
6449+
);
6450+
pipe(writable);
6451+
});
6452+
6453+
expect(getMeaningfulChildren(document)).toEqual(
6454+
<html>
6455+
<head />
6456+
<body>
6457+
<div>fallback</div>
6458+
</body>
6459+
</html>,
6460+
);
6461+
6462+
expect(didBootstrap).toBe(true);
6463+
});
63706464
});
63716465
});

0 commit comments

Comments
 (0)