From 8ea96ef84d8f08ed1846dec9e8ed20d2225db0d3 Mon Sep 17 00:00:00 2001 From: Josh Story Date: Mon, 1 May 2023 10:50:36 -0700 Subject: [PATCH] [Fizz] Encode external fizz runtime into chunks eagerly (#26752) in https://github.com/facebook/react/pull/26738 we added nonce to the ResponseState. Initially it was used in a variety of places but the version that got merged only included it with the external fizz runtime. This PR updates the config for the external fizz runtime so that the nonce is encoded into the script chunks at request creation time. The rationale is that for live-requests, streaming is more likely than not so doing the encoding work at the start is better than during flush. For cases such as SSG where the runtime is not required the extra encoding is tolerable (not a live request). Bots are an interesting case because if you want fastest TTFB you will end up requiring the runtime but if you are withholding until the stream is done you have already sacrificed fastest TTFB and the marginal slowdown of the extraneous encoding is hopefully neglibible I'm writing this so later if we learn that this tradeoff isn't worth it we at least understand why I made the change in the first place. --- .../src/server/ReactFizzConfigDOM.js | 56 ++++++++++--------- .../src/server/ReactFizzConfigDOMLegacy.js | 7 +-- .../src/__tests__/ReactDOMFizzServer-test.js | 2 +- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js index f4fae4d307..646e61608a 100644 --- a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js +++ b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js @@ -130,11 +130,8 @@ export type ResponseState = { startInlineScript: PrecomputedChunk, instructions: InstructionState, - // state for outputting CSP nonce - nonce: string | void, - // state for data streaming format - externalRuntimeConfig: BootstrapScriptDescriptor | null, + externalRuntimeScript: null | ExternalRuntimeScript, // preamble and postamble chunks and state htmlChunks: null | Array, @@ -196,6 +193,10 @@ export type BootstrapScriptDescriptor = { src: string, integrity?: string, }; +export type ExternalRuntimeScript = { + src: string, + chunks: Array, +}; // Allows us to keep track of what we've already written so we can refer back to it. // if passed externalRuntimeConfig and the enableFizzExternalRuntime feature flag // is set, the server will send instructions via data attributes (instead of inline scripts) @@ -215,7 +216,7 @@ export function createResponseState( '']); + ).toEqual(['']); expect(getVisibleChildren(document)).toEqual(