Skip to content

Commit

Permalink
Update computation of script chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltott committed Apr 28, 2023
1 parent ceb387b commit d2d41c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
41 changes: 16 additions & 25 deletions packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,8 @@ const startInlineScript = stringToPrecomputedChunk('<script>');
const endInlineScript = stringToPrecomputedChunk('</script>');

const startScriptSrc = stringToPrecomputedChunk('<script src="');
const startScriptSrcWithNonce = stringToPrecomputedChunk('<script nonce="');
const middleScriptSrcWithNonce = stringToPrecomputedChunk('" src="');

const startModuleSrc = stringToPrecomputedChunk('<script type="module" src="');
const startModuleSrcWithNonce = stringToPrecomputedChunk('<script nonce="');
const middleModuleSrcWithNonce = stringToPrecomputedChunk(
'" type="module" src="',
);
const scriptNonce = stringToPrecomputedChunk('" nonce="');
const scriptIntegirty = stringToPrecomputedChunk('" integrity="');
const endAsyncScript = stringToPrecomputedChunk('" async=""></script>');

Expand Down Expand Up @@ -255,17 +249,15 @@ export function createResponseState(
typeof scriptConfig === 'string' ? scriptConfig : scriptConfig.src;
const integrity =
typeof scriptConfig === 'string' ? undefined : scriptConfig.integrity;
if (nonce === undefined) {
bootstrapChunks.push(
startScriptSrc,
stringToChunk(escapeTextForBrowser(src)),
);
} else {

bootstrapChunks.push(
startScriptSrc,
stringToChunk(escapeTextForBrowser(src)),
);
if (nonce) {
bootstrapChunks.push(
startScriptSrcWithNonce,
scriptNonce,
stringToChunk(escapeTextForBrowser(nonce)),
middleScriptSrcWithNonce,
stringToChunk(escapeTextForBrowser(src)),
);
}
if (integrity) {
Expand All @@ -284,17 +276,16 @@ export function createResponseState(
typeof scriptConfig === 'string' ? scriptConfig : scriptConfig.src;
const integrity =
typeof scriptConfig === 'string' ? undefined : scriptConfig.integrity;
if (nonce === undefined) {
bootstrapChunks.push(
startModuleSrc,
stringToChunk(escapeTextForBrowser(src)),
);
} else {

bootstrapChunks.push(
startModuleSrc,
stringToChunk(escapeTextForBrowser(src)),
);

if (nonce) {
bootstrapChunks.push(
startModuleSrcWithNonce,
scriptNonce,
stringToChunk(escapeTextForBrowser(nonce)),
middleModuleSrcWithNonce,
stringToChunk(escapeTextForBrowser(src)),
);
}
if (integrity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ describe('ReactDOMFizzServerBrowser', () => {
);
const result = await readResult(stream);
expect(result).toMatchInlineSnapshot(
`"<div>hello world</div><script nonce="${nonce}">INIT();</script><script nonce="${nonce}" src="init.js" async=""></script><script nonce="${nonce}" type="module" src="init.mjs" async=""></script>"`,
`"<div>hello world</div><script nonce="${nonce}">INIT();</script><script src="init.js" nonce="${nonce}" async=""></script><script type="module" src="init.mjs" nonce="${nonce}" async=""></script>"`,
);
});
});

0 comments on commit d2d41c4

Please sign in to comment.