Unverified Commit 5ad1b777 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Restart static paths worker after finishing task (#47716)

Noticed this optimization when debugging something else - we restart
this worker if it's not fresh, to make sure module instances are new
created. This has some perf overhead and it happens right before running
the task and it's blocking the request. This PR changes that to be after
the old task, and the difference is noticeable on dev (~20ms).

Before:

<img width="1363" alt="CleanShot-2023-03-30-jTT9W48H@2x"
src="https://user-images.githubusercontent.com/3676859/228965542-e0d6892f-3989-4da5-96a4-5fc8b4155041.png">

After:

<img width="1363" alt="CleanShot-2023-03-30-PRrYQMg7@2x"
src="https://user-images.githubusercontent.com/3676859/228965566-42e04f8d-0cc3-447c-adc8-3840b23b9fa5.png">
parent 247ea65e
......@@ -25,8 +25,6 @@ if (process.env.NEXT_PREBUNDLED_REACT) {
overrideBuiltInReactPackages()
}
let workerWasUsed = false
// expose AsyncLocalStorage on globalThis for react usage
const { AsyncLocalStorage } = require('async_hooks')
;(globalThis as any).AsyncLocalStorage = AsyncLocalStorage
......@@ -69,12 +67,6 @@ export async function loadStaticPaths({
encodedPaths?: string[]
fallback?: boolean | 'blocking'
}> {
// we only want to use each worker once to prevent any invalid
// caches
if (workerWasUsed) {
process.exit(1)
}
// update work memory runtime-config
require('../../shared/lib/runtime-config').setConfig(config)
setHttpClientAndAgentOptions({
......@@ -96,45 +88,52 @@ export async function loadStaticPaths({
`Invariant: failed to load page with getStaticPaths for ${pathname}`
)
}
workerWasUsed = true
if (isAppPath) {
const userland: AppRouteUserlandModule | undefined =
components.ComponentMod.routeModule?.userland
const generateParams: GenerateParams = userland
? [
{
config: {
revalidate: userland.revalidate,
dynamic: userland.dynamic,
dynamicParams: userland.dynamicParams,
try {
if (isAppPath) {
const userland: AppRouteUserlandModule | undefined =
components.ComponentMod.routeModule?.userland
const generateParams: GenerateParams = userland
? [
{
config: {
revalidate: userland.revalidate,
dynamic: userland.dynamic,
dynamicParams: userland.dynamicParams,
},
generateStaticParams: userland.generateStaticParams,
segmentPath: pathname,
},
generateStaticParams: userland.generateStaticParams,
segmentPath: pathname,
},
]
: await collectGenerateParams(components.ComponentMod.tree)
]
: await collectGenerateParams(components.ComponentMod.tree)
return await buildAppStaticPaths({
page: pathname,
generateParams,
configFileName: config.configFileName,
distDir,
requestHeaders,
incrementalCacheHandlerPath,
serverHooks,
staticGenerationAsyncStorage,
isrFlushToDisk,
fetchCacheKeyPrefix,
maxMemoryCacheSize,
})
}
return buildAppStaticPaths({
return await buildStaticPaths({
page: pathname,
generateParams,
getStaticPaths: components.getStaticPaths,
configFileName: config.configFileName,
distDir,
requestHeaders,
incrementalCacheHandlerPath,
serverHooks,
staticGenerationAsyncStorage,
isrFlushToDisk,
fetchCacheKeyPrefix,
maxMemoryCacheSize,
locales,
defaultLocale,
})
} finally {
setTimeout(() => {
// we only want to use each worker once to prevent any invalid
// caches
process.exit(1)
})
}
return buildStaticPaths({
page: pathname,
getStaticPaths: components.getStaticPaths,
configFileName: config.configFileName,
locales,
defaultLocale,
})
}
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