Skip to content

Commit

Permalink
Move logic to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriqueLimas committed Jun 1, 2023
1 parent 08774cc commit e5b91a4
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ export type ExternalRuntimeScript = {
src: string,
chunks: Array<Chunk | PrecomputedChunk>,
};

/**
* 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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e5b91a4

Please sign in to comment.