Skip to content

Commit 416f66c

Browse files
authoredMar 19, 2024
fix: limit amount of concurrent handling of prerendered content (#356)
* fix: limit amount of concurrent handling of prerendered content * perf: don't set tag manifest on init span * perf: restore tagManifest span attribute, but make it performant
1 parent 6312ac2 commit 416f66c

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed
 

‎src/build/content/prerendered.ts

+34-26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { dirname, join } from 'node:path'
55
import { glob } from 'fast-glob'
66
import type { CacheHandlerValue } from 'next/dist/server/lib/incremental-cache/index.js'
77
import type { IncrementalCacheValue } from 'next/dist/server/response-cache/types.js'
8+
import pLimit from 'p-limit'
89

910
import { encodeBlobKey } from '../../shared/blobkey.js'
1011
import 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

‎src/run/handlers/server.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ export default async (request: Request) => {
5555
tagsManifest = await getTagsManifest()
5656
span.setAttributes(
5757
Object.entries(tagsManifest).reduce(
58-
(acc, [key, value]) => ({ ...acc, [`tagsManifest.${key}`]: value }),
59-
{},
58+
(acc, [key, value]) => {
59+
acc[`tagsManifest.${key}`] = value
60+
return acc
61+
},
62+
{} as Record<string, string>,
6063
),
6164
)
6265

0 commit comments

Comments
 (0)
Please sign in to comment.