Unverified Commit 3597978d authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Modify low priority files in manifest (#16181)

This pull request edits the `BuildManifest` that is sent to `/_document` instead of modifying a single input array to decouple its implementation details.

Optimally, we'd eliminate the `files` key all together.

---

Related to #16182
parent 39e00a3f
...@@ -17,6 +17,8 @@ import { getSortedRoutes } from '../../../next-server/lib/router/utils' ...@@ -17,6 +17,8 @@ import { getSortedRoutes } from '../../../next-server/lib/router/utils'
const isWebpack5 = parseInt(webpack.version!) === 5 const isWebpack5 = parseInt(webpack.version!) === 5
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
export type ClientBuildManifest = Record<string, string[]> export type ClientBuildManifest = Record<string, string[]>
// This function takes the asset map generated in BuildManifestPlugin and creates a // This function takes the asset map generated in BuildManifestPlugin and creates a
...@@ -102,7 +104,7 @@ export default class BuildManifestPlugin { ...@@ -102,7 +104,7 @@ export default class BuildManifestPlugin {
createAssets(compilation: any, assets: any) { createAssets(compilation: any, assets: any) {
const namedChunks: Map<string, CompilationType.Chunk> = const namedChunks: Map<string, CompilationType.Chunk> =
compilation.namedChunks compilation.namedChunks
const assetMap: BuildManifest = { const assetMap: DeepMutable<BuildManifest> = {
polyfillFiles: [], polyfillFiles: [],
devFiles: [], devFiles: [],
ampDevFiles: [], ampDevFiles: [],
......
import { normalizePagePath, denormalizePagePath } from './normalize-page-path' import { normalizePagePath, denormalizePagePath } from './normalize-page-path'
export type BuildManifest = { export type BuildManifest = {
devFiles: string[] devFiles: readonly string[]
ampDevFiles: string[] ampDevFiles: readonly string[]
polyfillFiles: string[] polyfillFiles: readonly string[]
lowPriorityFiles: string[] lowPriorityFiles: readonly string[]
pages: { pages: {
'/_app': string[] '/_app': readonly string[]
[page: string]: string[] [page: string]: readonly string[]
} }
ampFirstPages: string[] ampFirstPages: readonly string[]
} }
export function getPageFiles( export function getPageFiles(
buildManifest: BuildManifest, buildManifest: BuildManifest,
page: string page: string
): string[] { ): readonly string[] {
const normalizedPage = denormalizePagePath(normalizePagePath(page)) const normalizedPage = denormalizePagePath(normalizePagePath(page))
let files = buildManifest.pages[normalizedPage] let files = buildManifest.pages[normalizedPage]
......
...@@ -27,6 +27,7 @@ import { HeadManagerContext } from '../lib/head-manager-context' ...@@ -27,6 +27,7 @@ import { HeadManagerContext } from '../lib/head-manager-context'
import Loadable from '../lib/loadable' import Loadable from '../lib/loadable'
import { LoadableContext } from '../lib/loadable-context' import { LoadableContext } from '../lib/loadable-context'
import mitt, { MittEmitter } from '../lib/mitt' import mitt, { MittEmitter } from '../lib/mitt'
import postProcess from '../lib/post-process'
import { RouterContext } from '../lib/router-context' import { RouterContext } from '../lib/router-context'
import { NextRouter } from '../lib/router/router' import { NextRouter } from '../lib/router/router'
import { isDynamicRoute } from '../lib/router/utils/is-dynamic' import { isDynamicRoute } from '../lib/router/utils/is-dynamic'
...@@ -42,11 +43,12 @@ import { ...@@ -42,11 +43,12 @@ import {
RenderPage, RenderPage,
} from '../lib/utils' } from '../lib/utils'
import { tryGetPreviewData, __ApiPreviewProps } from './api-utils' import { tryGetPreviewData, __ApiPreviewProps } from './api-utils'
import { denormalizePagePath } from './denormalize-page-path'
import { FontManifest, getFontDefinitionFromManifest } from './font-utils'
import { getPageFiles } from './get-page-files' import { getPageFiles } from './get-page-files'
import { LoadComponentsReturnType, ManifestItem } from './load-components' import { LoadComponentsReturnType, ManifestItem } from './load-components'
import { normalizePagePath } from './normalize-page-path'
import optimizeAmp from './optimize-amp' import optimizeAmp from './optimize-amp'
import postProcess from '../lib/post-process'
import { FontManifest, getFontDefinitionFromManifest } from './font-utils'
function noRouter() { function noRouter() {
const message = const message =
...@@ -675,21 +677,27 @@ export async function renderToHTML( ...@@ -675,21 +677,27 @@ export async function renderToHTML(
// we preload the buildManifest for auto-export dynamic pages // we preload the buildManifest for auto-export dynamic pages
// to speed up hydrating query values // to speed up hydrating query values
let filteredBuildManifest = buildManifest let filteredBuildManifest = buildManifest
const additionalFiles: string[] = []
if (isAutoExport && pageIsDynamic) { if (isAutoExport && pageIsDynamic) {
const page = denormalizePagePath(normalizePagePath(pathname))
// This code would be much cleaner using `immer` and directly pushing into
// the result from `getPageFiles`, we could maybe consider that in the
// future.
if (page in filteredBuildManifest.pages) {
filteredBuildManifest = { filteredBuildManifest = {
...buildManifest, ...filteredBuildManifest,
lowPriorityFiles: buildManifest.lowPriorityFiles.filter((file) => { pages: {
if ( ...filteredBuildManifest.pages,
file.endsWith('_buildManifest.js') || [page]: [
file.endsWith('_buildManifest.module.js') ...filteredBuildManifest.pages[page],
) { ...filteredBuildManifest.lowPriorityFiles.filter((f) =>
additionalFiles.push(file) f.includes('_buildManifest')
return false ),
],
},
lowPriorityFiles: filteredBuildManifest.lowPriorityFiles.filter(
(f) => !f.includes('_buildManifest')
),
} }
return true
}),
} }
} }
...@@ -698,12 +706,11 @@ export async function renderToHTML( ...@@ -698,12 +706,11 @@ export async function renderToHTML(
? [] ? []
: [ : [
...new Set([ ...new Set([
...getPageFiles(buildManifest, '/_app'), ...getPageFiles(filteredBuildManifest, '/_app'),
...(pathname !== '/_error' ...(pathname !== '/_error'
? getPageFiles(buildManifest, pathname) ? getPageFiles(filteredBuildManifest, pathname)
: []), : []),
]), ]),
...additionalFiles,
] ]
const renderPage: RenderPage = ( const renderPage: RenderPage = (
......
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