Add flag for preloading all server chunks (#64084)

This adds an experimental flag to allow testing preloading all server
chunks including webpack chunks during the server initialization. It is
disabled by default and only opt-in for experimenting.

Closes NEXT-3005
This commit is contained in:
JJ Kasper 2024-04-04 14:13:13 -07:00 committed by GitHub
parent c502308bca
commit a9f85d14c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 0 deletions

View file

@ -236,6 +236,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
experimental: z
.strictObject({
appDocumentPreloading: z.boolean().optional(),
allServerChunksPreloading: z.boolean().optional(),
adjustFontFallbacks: z.boolean().optional(),
adjustFontFallbacksWithSizeAdjust: z.boolean().optional(),
allowedRevalidateHeaderKeys: z.array(z.string()).optional(),

View file

@ -184,6 +184,7 @@ export interface ExperimentalConfig {
linkNoTouchStart?: boolean
caseSensitiveRoutes?: boolean
appDocumentPreloading?: boolean
allServerChunksPreloading?: boolean
strictNextHead?: boolean
clientRouterFilter?: boolean
clientRouterFilterRedirects?: boolean
@ -881,6 +882,7 @@ export const defaultConfig: NextConfig = {
linkNoTouchStart: false,
caseSensitiveRoutes: false,
appDocumentPreloading: undefined,
allServerChunksPreloading: undefined,
clientRouterFilter: true,
clientRouterFilterRedirects: false,
fetchCacheKeyPrefix: '',

View file

@ -228,6 +228,33 @@ export default class NextNodeServer extends BaseServer {
}).catch(() => {})
}
if (
!options.dev &&
this.nextConfig.experimental.allServerChunksPreloading
) {
const appPathsManifest = this.getAppPathsManifest()
const pagesManifest = this.getPagesManifest()
for (const page of Object.keys(pagesManifest || {})) {
loadComponents({ distDir: this.distDir, page, isAppPath: false }).catch(
() => {}
)
}
for (const page of Object.keys(appPathsManifest || {})) {
loadComponents({ distDir: this.distDir, page, isAppPath: true })
.then(({ ComponentMod }) => {
const webpackRequire = ComponentMod.__next_app__.require
if (webpackRequire?.m) {
for (const id of Object.keys(webpackRequire.m)) {
webpackRequire(id)
}
}
})
.catch(() => {})
}
}
if (!options.dev) {
const { dynamicRoutes = [] } = this.getRoutesManifest() ?? {}
this.dynamicRoutes = dynamicRoutes.map((r) => {