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
for (const href of jsHrefs) {
scripts.push(
createElement('script', { createElement('script', {
src: `${ctx.assetPrefix}/_next/${encodeURIPath(href)}${getAssetQueryString(ctx, true)}`, src: `${ctx.assetPrefix}/_next/${encodeURIPath(href)}${getAssetQueryString(ctx, true)}`,
async: true, async: true,
key: `script-${index}`, 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
for (const href of scriptTags) {
const fullSrc = `${ctx.assetPrefix}/_next/${encodeURIPath( const fullSrc = `${ctx.assetPrefix}/_next/${encodeURIPath(
href href
)}${getAssetQueryString(ctx, true)}` )}${getAssetQueryString(ctx, true)}`
return createElement('script', { scripts.push(
createElement('script', {
src: fullSrc, src: fullSrc,
async: true, async: true,
key: `script-${index}`, key: `script-${scriptIndex}`,
nonce: ctx.nonce, 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,7 +42,8 @@ export function renderCssResource( ...@@ -40,7 +42,8 @@ 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(
createElement(
'style', 'style',
{ {
key: index, key: index,
...@@ -50,8 +53,8 @@ export function renderCssResource( ...@@ -50,8 +53,8 @@ export function renderCssResource(
}, },
entryCssFile.content entryCssFile.content
) )
} )
} else {
preloadCallbacks?.push(() => { preloadCallbacks?.push(() => {
ctx.componentMod.preloadStyle( ctx.componentMod.preloadStyle(
fullHref, fullHref,
...@@ -60,7 +63,8 @@ export function renderCssResource( ...@@ -60,7 +63,8 @@ export function renderCssResource(
) )
}) })
return createElement('link', { elements.push(
createElement('link', {
key: index, key: index,
rel: 'stylesheet', rel: 'stylesheet',
href: fullHref, href: fullHref,
...@@ -68,5 +72,9 @@ export function renderCssResource( ...@@ -68,5 +72,9 @@ export function renderCssResource(
crossOrigin: ctx.renderOpts.crossOrigin, crossOrigin: ctx.renderOpts.crossOrigin,
nonce: ctx.nonce, nonce: ctx.nonce,
}) })
}) )
}
index++
}
return elements
} }
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