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) { ...@@ -25,8 +25,6 @@ if (process.env.NEXT_PREBUNDLED_REACT) {
overrideBuiltInReactPackages() overrideBuiltInReactPackages()
} }
let workerWasUsed = false
// expose AsyncLocalStorage on globalThis for react usage // expose AsyncLocalStorage on globalThis for react usage
const { AsyncLocalStorage } = require('async_hooks') const { AsyncLocalStorage } = require('async_hooks')
;(globalThis as any).AsyncLocalStorage = AsyncLocalStorage ;(globalThis as any).AsyncLocalStorage = AsyncLocalStorage
...@@ -69,12 +67,6 @@ export async function loadStaticPaths({ ...@@ -69,12 +67,6 @@ export async function loadStaticPaths({
encodedPaths?: string[] encodedPaths?: string[]
fallback?: boolean | 'blocking' 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 // update work memory runtime-config
require('../../shared/lib/runtime-config').setConfig(config) require('../../shared/lib/runtime-config').setConfig(config)
setHttpClientAndAgentOptions({ setHttpClientAndAgentOptions({
...@@ -96,8 +88,8 @@ export async function loadStaticPaths({ ...@@ -96,8 +88,8 @@ export async function loadStaticPaths({
`Invariant: failed to load page with getStaticPaths for ${pathname}` `Invariant: failed to load page with getStaticPaths for ${pathname}`
) )
} }
workerWasUsed = true
try {
if (isAppPath) { if (isAppPath) {
const userland: AppRouteUserlandModule | undefined = const userland: AppRouteUserlandModule | undefined =
components.ComponentMod.routeModule?.userland components.ComponentMod.routeModule?.userland
...@@ -115,7 +107,7 @@ export async function loadStaticPaths({ ...@@ -115,7 +107,7 @@ export async function loadStaticPaths({
] ]
: await collectGenerateParams(components.ComponentMod.tree) : await collectGenerateParams(components.ComponentMod.tree)
return buildAppStaticPaths({ return await buildAppStaticPaths({
page: pathname, page: pathname,
generateParams, generateParams,
configFileName: config.configFileName, configFileName: config.configFileName,
...@@ -130,11 +122,18 @@ export async function loadStaticPaths({ ...@@ -130,11 +122,18 @@ export async function loadStaticPaths({
}) })
} }
return buildStaticPaths({ return await buildStaticPaths({
page: pathname, page: pathname,
getStaticPaths: components.getStaticPaths, getStaticPaths: components.getStaticPaths,
configFileName: config.configFileName, configFileName: config.configFileName,
locales, locales,
defaultLocale, defaultLocale,
}) })
} finally {
setTimeout(() => {
// we only want to use each worker once to prevent any invalid
// caches
process.exit(1)
})
}
} }
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