Unverified Commit 24fe5ca2 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Retry manifest file loading only in dev mode (#73900)

In #73891 we added another manifest to be loaded in `loadComponents`
(initially unconditionally). This uncovered a flakiness in prod mode
when attempting to load an optional manifest. The non-existent manifest
is attempted to be loaded three times with 100ms delay between attempts,
before giving up. For some reason the increased loading time leads to
more test flakiness.

To mitigate this, we're limiting the retry behaviour to the dev mode,
which matches the original intention when this was introduced in #45244.
parent 241a340c
...@@ -1614,6 +1614,7 @@ export async function isPageStatic({ ...@@ -1614,6 +1614,7 @@ export async function isPageStatic({
distDir, distDir,
page: originalAppPath || page, page: originalAppPath || page,
isAppPath: pageType === 'app', isAppPath: pageType === 'app',
isDev: false,
}) })
} }
const Comp = componentsResult.Component as NextComponentType | undefined const Comp = componentsResult.Component as NextComponentType | undefined
...@@ -1869,6 +1870,7 @@ export async function hasCustomGetInitialProps({ ...@@ -1869,6 +1870,7 @@ export async function hasCustomGetInitialProps({
distDir, distDir,
page: page, page: page,
isAppPath: false, isAppPath: false,
isDev: false,
}) })
let mod = components.ComponentMod let mod = components.ComponentMod
...@@ -1895,6 +1897,7 @@ export async function getDefinedNamedExports({ ...@@ -1895,6 +1897,7 @@ export async function getDefinedNamedExports({
distDir, distDir,
page: page, page: page,
isAppPath: false, isAppPath: false,
isDev: false,
}) })
return Object.keys(components.ComponentMod).filter((key) => { return Object.keys(components.ComponentMod).filter((key) => {
......
...@@ -233,6 +233,7 @@ async function exportPageImpl( ...@@ -233,6 +233,7 @@ async function exportPageImpl(
distDir, distDir,
page, page,
isAppPath: isAppDir, isAppPath: isAppDir,
isDev: false,
}) })
// Handle App Routes. // Handle App Routes.
......
...@@ -83,6 +83,7 @@ export async function loadStaticPaths({ ...@@ -83,6 +83,7 @@ export async function loadStaticPaths({
// In `pages/`, the page is the same as the pathname. // In `pages/`, the page is the same as the pathname.
page: page || pathname, page: page || pathname,
isAppPath, isAppPath,
isDev: true,
}) })
if (isAppPath) { if (isAppPath) {
......
...@@ -118,12 +118,13 @@ export async function evalManifestWithRetries<T extends object>( ...@@ -118,12 +118,13 @@ export async function evalManifestWithRetries<T extends object>(
async function loadClientReferenceManifest( async function loadClientReferenceManifest(
manifestPath: string, manifestPath: string,
entryName: string entryName: string,
attempts?: number
) { ) {
try { try {
const context = await evalManifestWithRetries<{ const context = await evalManifestWithRetries<{
__RSC_MANIFEST: { [key: string]: ClientReferenceManifest } __RSC_MANIFEST: { [key: string]: ClientReferenceManifest }
}>(manifestPath) }>(manifestPath, attempts)
return context.__RSC_MANIFEST[entryName] return context.__RSC_MANIFEST[entryName]
} catch (err) { } catch (err) {
return undefined return undefined
...@@ -134,10 +135,12 @@ async function loadComponentsImpl<N = any>({ ...@@ -134,10 +135,12 @@ async function loadComponentsImpl<N = any>({
distDir, distDir,
page, page,
isAppPath, isAppPath,
isDev,
}: { }: {
distDir: string distDir: string
page: string page: string
isAppPath: boolean isAppPath: boolean
isDev: boolean
}): Promise<LoadComponentsReturnType<N>> { }): Promise<LoadComponentsReturnType<N>> {
let DocumentMod = {} let DocumentMod = {}
let AppMod = {} let AppMod = {}
...@@ -151,6 +154,12 @@ async function loadComponentsImpl<N = any>({ ...@@ -151,6 +154,12 @@ async function loadComponentsImpl<N = any>({
// Make sure to avoid loading the manifest for metadata route handlers. // Make sure to avoid loading the manifest for metadata route handlers.
const hasClientManifest = isAppPath && !isMetadataRoute(page) const hasClientManifest = isAppPath && !isMetadataRoute(page)
// In dev mode we retry loading a manifest file to handle a race condition
// that can occur while app and pages are compiling at the same time, and the
// build-manifest is still being written to disk while an app path is
// attempting to load.
const manifestLoadAttempts = isDev ? 3 : 1
// Load the manifest files first // Load the manifest files first
const [ const [
buildManifest, buildManifest,
...@@ -159,15 +168,20 @@ async function loadComponentsImpl<N = any>({ ...@@ -159,15 +168,20 @@ async function loadComponentsImpl<N = any>({
clientReferenceManifest, clientReferenceManifest,
serverActionsManifest, serverActionsManifest,
] = await Promise.all([ ] = await Promise.all([
loadManifestWithRetries<BuildManifest>(join(distDir, BUILD_MANIFEST)), loadManifestWithRetries<BuildManifest>(
join(distDir, BUILD_MANIFEST),
manifestLoadAttempts
),
loadManifestWithRetries<ReactLoadableManifest>( loadManifestWithRetries<ReactLoadableManifest>(
join(distDir, REACT_LOADABLE_MANIFEST) join(distDir, REACT_LOADABLE_MANIFEST),
manifestLoadAttempts
), ),
// This manifest will only exist in Pages dir && Production && Webpack. // This manifest will only exist in Pages dir && Production && Webpack.
isAppPath || process.env.TURBOPACK isAppPath || process.env.TURBOPACK
? undefined ? undefined
: loadManifestWithRetries<DynamicCssManifest>( : loadManifestWithRetries<DynamicCssManifest>(
join(distDir, `${DYNAMIC_CSS_MANIFEST}.json`) join(distDir, `${DYNAMIC_CSS_MANIFEST}.json`),
manifestLoadAttempts
).catch(() => undefined), ).catch(() => undefined),
hasClientManifest hasClientManifest
? loadClientReferenceManifest( ? loadClientReferenceManifest(
...@@ -177,12 +191,14 @@ async function loadComponentsImpl<N = any>({ ...@@ -177,12 +191,14 @@ async function loadComponentsImpl<N = any>({
'app', 'app',
page.replace(/%5F/g, '_') + '_' + CLIENT_REFERENCE_MANIFEST + '.js' page.replace(/%5F/g, '_') + '_' + CLIENT_REFERENCE_MANIFEST + '.js'
), ),
page.replace(/%5F/g, '_') page.replace(/%5F/g, '_'),
manifestLoadAttempts
) )
: undefined, : undefined,
isAppPath isAppPath
? loadManifestWithRetries<ActionManifest>( ? loadManifestWithRetries<ActionManifest>(
join(distDir, 'server', SERVER_REFERENCE_MANIFEST + '.json') join(distDir, 'server', SERVER_REFERENCE_MANIFEST + '.json'),
manifestLoadAttempts
).catch(() => null) ).catch(() => null)
: null, : null,
]) ])
......
...@@ -178,11 +178,14 @@ export default class NextNodeServer extends BaseServer< ...@@ -178,11 +178,14 @@ export default class NextNodeServer extends BaseServer<
protected cleanupListeners = new AsyncCallbackSet() protected cleanupListeners = new AsyncCallbackSet()
protected internalWaitUntil: WaitUntil | undefined protected internalWaitUntil: WaitUntil | undefined
private isDev: boolean
constructor(options: Options) { constructor(options: Options) {
// Initialize super class // Initialize super class
super(options) super(options)
this.isDev = options.dev ?? false
/** /**
* This sets environment variable to be used at the time of SSR by head.tsx. * This sets environment variable to be used at the time of SSR by head.tsx.
* Using this from process.env allows targeting SSR by calling * Using this from process.env allows targeting SSR by calling
...@@ -214,11 +217,13 @@ export default class NextNodeServer extends BaseServer< ...@@ -214,11 +217,13 @@ export default class NextNodeServer extends BaseServer<
distDir: this.distDir, distDir: this.distDir,
page: '/_document', page: '/_document',
isAppPath: false, isAppPath: false,
isDev: this.isDev,
}).catch(() => {}) }).catch(() => {})
loadComponents({ loadComponents({
distDir: this.distDir, distDir: this.distDir,
page: '/_app', page: '/_app',
isAppPath: false, isAppPath: false,
isDev: this.isDev,
}).catch(() => {}) }).catch(() => {})
} }
...@@ -279,11 +284,17 @@ export default class NextNodeServer extends BaseServer< ...@@ -279,11 +284,17 @@ export default class NextNodeServer extends BaseServer<
distDir: this.distDir, distDir: this.distDir,
page, page,
isAppPath: false, isAppPath: false,
isDev: this.isDev,
}).catch(() => {}) }).catch(() => {})
} }
for (const page of Object.keys(appPathsManifest || {})) { for (const page of Object.keys(appPathsManifest || {})) {
await loadComponents({ distDir: this.distDir, page, isAppPath: true }) await loadComponents({
distDir: this.distDir,
page,
isAppPath: true,
isDev: this.isDev,
})
.then(async ({ ComponentMod }) => { .then(async ({ ComponentMod }) => {
// we need to ensure fetch is patched before we require the page, // we need to ensure fetch is patched before we require the page,
// otherwise if the fetch is patched by user code, we will be patching it // otherwise if the fetch is patched by user code, we will be patching it
...@@ -793,6 +804,7 @@ export default class NextNodeServer extends BaseServer< ...@@ -793,6 +804,7 @@ export default class NextNodeServer extends BaseServer<
distDir: this.distDir, distDir: this.distDir,
page: pagePath, page: pagePath,
isAppPath, isAppPath,
isDev: this.isDev,
}) })
if ( if (
......
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