Unverified Commit 58a4c7b1 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Perf: Skip turning Set into Array (#91383)

## What?

Converting from Set->Array is not needed as it's only used to iterate
over the list. Avoids the overhead of converting this.
parent df88058f
...@@ -29,17 +29,20 @@ export async function createComponentStylesAndScripts({ ...@@ -29,17 +29,20 @@ export async function createComponentStylesAndScripts({
const styles = renderCssResource(entryCssFiles, ctx) const styles = renderCssResource(entryCssFiles, ctx)
const scripts = jsHrefs const scripts: React.ReactNode[] = []
? jsHrefs.map((href, index) => let scriptIndex = 0
createElement('script', { for (const href of jsHrefs) {
src: `${ctx.assetPrefix}/_next/${encodeURIPath(href)}${getAssetQueryString(ctx, true)}`, scripts.push(
async: true, createElement('script', {
key: `script-${index}`, src: `${ctx.assetPrefix}/_next/${encodeURIPath(href)}${getAssetQueryString(ctx, true)}`,
}) async: true,
) key: `script-${scriptIndex}`,
: null })
)
scriptIndex++
}
const Comp = interopDefault(await getComponent()) const Comp = interopDefault(await getComponent())
return [Comp, styles, scripts] return [Comp, styles, scripts.length ? scripts : null]
} }
...@@ -9,7 +9,7 @@ export function getLinkAndScriptTags( ...@@ -9,7 +9,7 @@ export function getLinkAndScriptTags(
injectedCSS: Set<string>, injectedCSS: Set<string>,
injectedScripts: Set<string>, injectedScripts: Set<string>,
collectNewImports?: boolean collectNewImports?: boolean
): { styles: CssResource[]; scripts: string[] } { ): { styles: ReadonlySet<CssResource>; scripts: ReadonlySet<string> } {
const filePathWithoutExt = filePath.replace(/\.[^.]+$/, '') const filePathWithoutExt = filePath.replace(/\.[^.]+$/, '')
const cssChunks = new Set<CssResource>() const cssChunks = new Set<CssResource>()
const jsChunks = new Set<string>() const jsChunks = new Set<string>()
...@@ -39,5 +39,5 @@ export function getLinkAndScriptTags( ...@@ -39,5 +39,5 @@ export function getLinkAndScriptTags(
} }
} }
return { styles: [...cssChunks], scripts: [...jsChunks] } return { styles: cssChunks, scripts: jsChunks }
} }
...@@ -5,6 +5,7 @@ import { getAssetQueryString } from './get-asset-query-string' ...@@ -5,6 +5,7 @@ import { getAssetQueryString } from './get-asset-query-string'
import { encodeURIPath } from '../../shared/lib/encode-uri-path' import { encodeURIPath } from '../../shared/lib/encode-uri-path'
import type { PreloadCallbacks } from './types' import type { PreloadCallbacks } from './types'
import { renderCssResource } from './render-css-resource' import { renderCssResource } from './render-css-resource'
const EMPTY_SET: ReadonlySet<never> = new Set()
export function getLayerAssets({ export function getLayerAssets({
ctx, ctx,
...@@ -31,7 +32,7 @@ export function getLayerAssets({ ...@@ -31,7 +32,7 @@ export function getLayerAssets({
injectedJSWithCurrentLayout, injectedJSWithCurrentLayout,
true true
) )
: { styles: [], scripts: [] } : { styles: EMPTY_SET, scripts: EMPTY_SET }
const preloadedFontFiles = layoutOrPagePath const preloadedFontFiles = layoutOrPagePath
? getPreloadableFonts( ? getPreloadableFonts(
...@@ -76,20 +77,23 @@ export function getLayerAssets({ ...@@ -76,20 +77,23 @@ export function getLayerAssets({
const styles = renderCssResource(styleTags, ctx, preloadCallbacks) const styles = renderCssResource(styleTags, ctx, preloadCallbacks)
const scripts = scriptTags const scripts: React.ReactNode[] = []
? scriptTags.map((href, index) => { let scriptIndex = 0
const fullSrc = `${ctx.assetPrefix}/_next/${encodeURIPath( for (const href of scriptTags) {
href const fullSrc = `${ctx.assetPrefix}/_next/${encodeURIPath(
)}${getAssetQueryString(ctx, true)}` href
)}${getAssetQueryString(ctx, true)}`
return createElement('script', { scripts.push(
src: fullSrc, createElement('script', {
async: true, src: fullSrc,
key: `script-${index}`, async: true,
nonce: ctx.nonce, key: `script-${scriptIndex}`,
}) nonce: ctx.nonce,
}) })
: [] )
scriptIndex++
}
return styles.length || scripts.length ? [...styles, ...scripts] : null return styles.length || scripts.length ? [...styles, ...scripts] : null
} }
...@@ -10,14 +10,16 @@ import type { PreloadCallbacks } from './types' ...@@ -10,14 +10,16 @@ import type { PreloadCallbacks } from './types'
* For external CSS files, renders a <link> tag pointing to the CSS file. * For external CSS files, renders a <link> tag pointing to the CSS file.
*/ */
export function renderCssResource( export function renderCssResource(
entryCssFiles: CssResource[], entryCssFiles: Iterable<CssResource>,
ctx: AppRenderContext, ctx: AppRenderContext,
preloadCallbacks?: PreloadCallbacks preloadCallbacks?: PreloadCallbacks
) { ) {
const { const {
componentMod: { createElement }, componentMod: { createElement },
} = ctx } = ctx
return entryCssFiles.map((entryCssFile, index) => { const elements: React.ReactNode[] = []
let index = 0
for (const entryCssFile of entryCssFiles) {
// `Precedence` is an opt-in signal for React to handle resource // `Precedence` is an opt-in signal for React to handle resource
// loading and deduplication, etc. It's also used as the key to sort // loading and deduplication, etc. It's also used as the key to sort
// resources so they will be injected in the correct order. // resources so they will be injected in the correct order.
...@@ -40,33 +42,39 @@ export function renderCssResource( ...@@ -40,33 +42,39 @@ export function renderCssResource(
)}${getAssetQueryString(ctx, true)}` )}${getAssetQueryString(ctx, true)}`
if (entryCssFile.inlined && !ctx.parsedRequestHeaders.isRSCRequest) { if (entryCssFile.inlined && !ctx.parsedRequestHeaders.isRSCRequest) {
return createElement( elements.push(
'style', createElement(
{ 'style',
{
key: index,
nonce: ctx.nonce,
precedence: precedence,
href: fullHref,
},
entryCssFile.content
)
)
} else {
preloadCallbacks?.push(() => {
ctx.componentMod.preloadStyle(
fullHref,
ctx.renderOpts.crossOrigin,
ctx.nonce
)
})
elements.push(
createElement('link', {
key: index, key: index,
nonce: ctx.nonce, rel: 'stylesheet',
precedence: precedence,
href: fullHref, href: fullHref,
}, precedence: precedence,
entryCssFile.content crossOrigin: ctx.renderOpts.crossOrigin,
nonce: ctx.nonce,
})
) )
} }
index++
preloadCallbacks?.push(() => { }
ctx.componentMod.preloadStyle( return elements
fullHref,
ctx.renderOpts.crossOrigin,
ctx.nonce
)
})
return createElement('link', {
key: index,
rel: 'stylesheet',
href: fullHref,
precedence: precedence,
crossOrigin: ctx.renderOpts.crossOrigin,
nonce: ctx.nonce,
})
})
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment