@@ -118,12 +118,13 @@ export async function evalManifestWithRetries<T extends object>(
118118
119119async function loadClientReferenceManifest (
120120 manifestPath : string ,
121- entryName : string
121+ entryName : string ,
122+ attempts ?: number
122123) {
123124 try {
124125 const context = await evalManifestWithRetries < {
125126 __RSC_MANIFEST : { [ key : string ] : ClientReferenceManifest }
126- } > ( manifestPath )
127+ } > ( manifestPath , attempts )
127128 return context . __RSC_MANIFEST [ entryName ]
128129 } catch ( err ) {
129130 return undefined
@@ -134,10 +135,12 @@ async function loadComponentsImpl<N = any>({
134135 distDir,
135136 page,
136137 isAppPath,
138+ isDev,
137139} : {
138140 distDir : string
139141 page : string
140142 isAppPath : boolean
143+ isDev : boolean
141144} ) : Promise < LoadComponentsReturnType < N > > {
142145 let DocumentMod = { }
143146 let AppMod = { }
@@ -151,6 +154,12 @@ async function loadComponentsImpl<N = any>({
151154 // Make sure to avoid loading the manifest for metadata route handlers.
152155 const hasClientManifest = isAppPath && ! isMetadataRoute ( page )
153156
157+ // In dev mode we retry loading a manifest file to handle a race condition
158+ // that can occur while app and pages are compiling at the same time, and the
159+ // build-manifest is still being written to disk while an app path is
160+ // attempting to load.
161+ const manifestLoadAttempts = isDev ? 3 : 1
162+
154163 // Load the manifest files first
155164 const [
156165 buildManifest ,
@@ -159,15 +168,20 @@ async function loadComponentsImpl<N = any>({
159168 clientReferenceManifest ,
160169 serverActionsManifest ,
161170 ] = await Promise . all ( [
162- loadManifestWithRetries < BuildManifest > ( join ( distDir , BUILD_MANIFEST ) ) ,
171+ loadManifestWithRetries < BuildManifest > (
172+ join ( distDir , BUILD_MANIFEST ) ,
173+ manifestLoadAttempts
174+ ) ,
163175 loadManifestWithRetries < ReactLoadableManifest > (
164- join ( distDir , REACT_LOADABLE_MANIFEST )
176+ join ( distDir , REACT_LOADABLE_MANIFEST ) ,
177+ manifestLoadAttempts
165178 ) ,
166179 // This manifest will only exist in Pages dir && Production && Webpack.
167180 isAppPath || process . env . TURBOPACK
168181 ? undefined
169182 : loadManifestWithRetries < DynamicCssManifest > (
170- join ( distDir , `${ DYNAMIC_CSS_MANIFEST } .json` )
183+ join ( distDir , `${ DYNAMIC_CSS_MANIFEST } .json` ) ,
184+ manifestLoadAttempts
171185 ) . catch ( ( ) => undefined ) ,
172186 hasClientManifest
173187 ? loadClientReferenceManifest (
@@ -177,12 +191,14 @@ async function loadComponentsImpl<N = any>({
177191 'app' ,
178192 page . replace ( / % 5 F / g, '_' ) + '_' + CLIENT_REFERENCE_MANIFEST + '.js'
179193 ) ,
180- page . replace ( / % 5 F / g, '_' )
194+ page . replace ( / % 5 F / g, '_' ) ,
195+ manifestLoadAttempts
181196 )
182197 : undefined ,
183198 isAppPath
184199 ? loadManifestWithRetries < ActionManifest > (
185- join ( distDir , 'server' , SERVER_REFERENCE_MANIFEST + '.json' )
200+ join ( distDir , 'server' , SERVER_REFERENCE_MANIFEST + '.json' ) ,
201+ manifestLoadAttempts
186202 ) . catch ( ( ) => null )
187203 : null ,
188204 ] )
0 commit comments