From e5b91a4aba9a69f1da822119847225775be308c2 Mon Sep 17 00:00:00 2001 From: Henrique Limas Date: Wed, 31 May 2023 21:39:43 -0700 Subject: [PATCH] Move logic to a function --- .../src/server/ReactFizzConfigDOM.js | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js index b74492102158d..dc03bdee9d373 100644 --- a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js +++ b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js @@ -199,6 +199,19 @@ export type ExternalRuntimeScript = { src: string, chunks: Array, }; + +/** + * We normalize crossOrigin to be "use-credentials", "" or null. + * Unless using "use-credentials" all the other strings behave like "anonymous" and "" is + * shortest bytes-wise. + */ +function getCrossOrigin(options?: mixed) { + return options == null || typeof options.crossOrigin !== 'string' + ? null + : options.crossOrigin === 'use-credentials' + ? 'use-credentials' + : ''; +} // 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) @@ -267,12 +280,7 @@ export function createResponseState( typeof scriptConfig === 'string' ? scriptConfig : scriptConfig.src; const integrity = typeof scriptConfig === 'string' ? undefined : scriptConfig.integrity; - const crossOrigin = - typeof scriptConfig === 'string' || scriptConfig.crossOrigin == null - ? undefined - : scriptConfig.crossOrigin === 'use-credentials' - ? 'use-credentials' - : ''; + const crossOrigin = getCrossOrigin(scriptConfig); bootstrapChunks.push( startScriptSrc, @@ -306,12 +314,7 @@ export function createResponseState( typeof scriptConfig === 'string' ? scriptConfig : scriptConfig.src; const integrity = typeof scriptConfig === 'string' ? undefined : scriptConfig.integrity; - const crossOrigin = - typeof scriptConfig === 'string' || scriptConfig.crossOrigin == null - ? undefined - : scriptConfig.crossOrigin === 'use-credentials' - ? 'use-credentials' - : ''; + const crossOrigin = getCrossOrigin(scriptConfig); bootstrapChunks.push( startModuleSrc, @@ -5085,12 +5088,7 @@ export function preconnect(href: string, options?: ?{crossOrigin?: string}) { } if (typeof href === 'string' && href) { - const crossOrigin = - options == null || typeof options.crossOrigin !== 'string' - ? null - : options.crossOrigin === 'use-credentials' - ? 'use-credentials' - : ''; + const crossOrigin = getCrossOrigin(options); const key = `[preconnect][${ crossOrigin === null ? 'null' : crossOrigin