@@ -5,6 +5,7 @@ import { dirname, join } from 'node:path'
55import { glob } from 'fast-glob'
66import type { CacheHandlerValue } from 'next/dist/server/lib/incremental-cache/index.js'
77import type { IncrementalCacheValue } from 'next/dist/server/response-cache/types.js'
8+ import pLimit from 'p-limit'
89
910import { encodeBlobKey } from '../../shared/blobkey.js'
1011import type { PluginContext } from '../plugin-context.js'
@@ -83,35 +84,42 @@ export const copyPrerenderedContent = async (ctx: PluginContext): Promise<void>
8384 // read prerendered content and build JSON key/values for the blob store
8485 const manifest = await ctx . getPrerenderManifest ( )
8586
87+ const limitConcurrentPrerenderContentHandling = pLimit ( 10 )
88+
8689 await Promise . all (
87- Object . entries ( manifest . routes ) . map ( async ( [ route , meta ] ) : Promise < void > => {
88- const lastModified = meta . initialRevalidateSeconds ? Date . now ( ) - 31536000000 : Date . now ( )
89- const key = routeToFilePath ( route )
90- let value : IncrementalCacheValue
91- switch ( true ) {
92- // Parallel route default layout has no prerendered page
93- case meta . dataRoute ?. endsWith ( '/default.rsc' ) &&
94- ! existsSync ( join ( ctx . publishDir , 'server/app' , `${ key } .html` ) ) :
95- return
96- case meta . dataRoute ?. endsWith ( '.json' ) :
97- if ( manifest . notFoundRoutes . includes ( route ) ) {
98- // if pages router returns 'notFound: true', build won't produce html and json files
99- return
90+ Object . entries ( manifest . routes ) . map (
91+ ( [ route , meta ] ) : Promise < void > =>
92+ limitConcurrentPrerenderContentHandling ( async ( ) => {
93+ const lastModified = meta . initialRevalidateSeconds
94+ ? Date . now ( ) - 31536000000
95+ : Date . now ( )
96+ const key = routeToFilePath ( route )
97+ let value : IncrementalCacheValue
98+ switch ( true ) {
99+ // Parallel route default layout has no prerendered page
100+ case meta . dataRoute ?. endsWith ( '/default.rsc' ) &&
101+ ! existsSync ( join ( ctx . publishDir , 'server/app' , `${ key } .html` ) ) :
102+ return
103+ case meta . dataRoute ?. endsWith ( '.json' ) :
104+ if ( manifest . notFoundRoutes . includes ( route ) ) {
105+ // if pages router returns 'notFound: true', build won't produce html and json files
106+ return
107+ }
108+ value = await buildPagesCacheValue ( join ( ctx . publishDir , 'server/pages' , key ) )
109+ break
110+ case meta . dataRoute ?. endsWith ( '.rsc' ) :
111+ value = await buildAppCacheValue ( join ( ctx . publishDir , 'server/app' , key ) )
112+ break
113+ case meta . dataRoute === null :
114+ value = await buildRouteCacheValue ( join ( ctx . publishDir , 'server/app' , key ) )
115+ break
116+ default :
117+ throw new Error ( `Unrecognized content: ${ route } ` )
100118 }
101- value = await buildPagesCacheValue ( join ( ctx . publishDir , 'server/pages' , key ) )
102- break
103- case meta . dataRoute ?. endsWith ( '.rsc' ) :
104- value = await buildAppCacheValue ( join ( ctx . publishDir , 'server/app' , key ) )
105- break
106- case meta . dataRoute === null :
107- value = await buildRouteCacheValue ( join ( ctx . publishDir , 'server/app' , key ) )
108- break
109- default :
110- throw new Error ( `Unrecognized content: ${ route } ` )
111- }
112119
113- await writeCacheEntry ( key , value , lastModified , ctx )
114- } ) ,
120+ await writeCacheEntry ( key , value , lastModified , ctx )
121+ } ) ,
122+ ) ,
115123 )
116124
117125 // app router 404 pages are not in the prerender manifest
0 commit comments