Rework experimental preload entries handling (#64125)

Follow-up to https://github.com/vercel/next.js/pull/64084 this refactors
it a bit and renames the flag.

Closes NEXT-3018
This commit is contained in:
JJ Kasper 2024-04-05 10:38:39 -07:00 committed by GitHub
parent f6baf61b61
commit 2f567da817
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 28 deletions

View file

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

View file

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

View file

@ -228,31 +228,8 @@ 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 && this.nextConfig.experimental.preloadEntriesOnStart) {
this.unstable_preloadEntries()
}
if (!options.dev) {
@ -295,6 +272,32 @@ export default class NextNodeServer extends BaseServer {
}
}
public async unstable_preloadEntries(): Promise<void> {
const appPathsManifest = this.getAppPathsManifest()
const pagesManifest = this.getPagesManifest()
for (const page of Object.keys(pagesManifest || {})) {
await loadComponents({
distDir: this.distDir,
page,
isAppPath: false,
}).catch(() => {})
}
for (const page of Object.keys(appPathsManifest || {})) {
await loadComponents({ distDir: this.distDir, page, isAppPath: true })
.then(async ({ ComponentMod }) => {
const webpackRequire = ComponentMod.__next_app__.require
if (webpackRequire?.m) {
for (const id of Object.keys(webpackRequire.m)) {
await webpackRequire(id)
}
}
})
.catch(() => {})
}
}
protected async handleUpgrade(): Promise<void> {
// The web server does not support web sockets, it's only used for HMR in
// development.