@@ -5,6 +5,7 @@ import { dirname, join } from 'node:path'
5
5
import { glob } from 'fast-glob'
6
6
import type { CacheHandlerValue } from 'next/dist/server/lib/incremental-cache/index.js'
7
7
import type { IncrementalCacheValue } from 'next/dist/server/response-cache/types.js'
8
+ import pLimit from 'p-limit'
8
9
9
10
import { encodeBlobKey } from '../../shared/blobkey.js'
10
11
import type { PluginContext } from '../plugin-context.js'
@@ -83,35 +84,42 @@ export const copyPrerenderedContent = async (ctx: PluginContext): Promise<void>
83
84
// read prerendered content and build JSON key/values for the blob store
84
85
const manifest = await ctx . getPrerenderManifest ( )
85
86
87
+ const limitConcurrentPrerenderContentHandling = pLimit ( 10 )
88
+
86
89
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 } ` )
100
118
}
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
- }
112
119
113
- await writeCacheEntry ( key , value , lastModified , ctx )
114
- } ) ,
120
+ await writeCacheEntry ( key , value , lastModified , ctx )
121
+ } ) ,
122
+ ) ,
115
123
)
116
124
117
125
// app router 404 pages are not in the prerender manifest
0 commit comments