From ae1baae51a71056c3fba202fbe62d4b793b8c513 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Thu, 30 Mar 2023 23:09:27 +0200 Subject: [PATCH 1/2] exit static paths worker after finishing task --- .../src/server/dev/static-paths-worker.ts | 83 +++++++++---------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/packages/next/src/server/dev/static-paths-worker.ts b/packages/next/src/server/dev/static-paths-worker.ts index 53fc78ce0718..df9ecf572b19 100644 --- a/packages/next/src/server/dev/static-paths-worker.ts +++ b/packages/next/src/server/dev/static-paths-worker.ts @@ -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.userland - const generateParams: GenerateParams = userland - ? [ - { - config: { - revalidate: userland.revalidate, - dynamic: userland.dynamic, - dynamicParams: userland.dynamicParams, + try { + if (isAppPath) { + const userland: AppRouteUserlandModule | undefined = + components.ComponentMod.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 buildAppStaticPaths({ + page: pathname, + generateParams, + configFileName: config.configFileName, + distDir, + requestHeaders, + incrementalCacheHandlerPath, + serverHooks, + staticGenerationAsyncStorage, + isrFlushToDisk, + fetchCacheKeyPrefix, + maxMemoryCacheSize, + }) + } - return buildAppStaticPaths({ + return 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, - }) } From 3003593ae4853e55b8bc2a73be90f4baec1fa7ea Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Thu, 30 Mar 2023 23:56:30 +0200 Subject: [PATCH 2/2] make sure to await --- packages/next/src/server/dev/static-paths-worker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next/src/server/dev/static-paths-worker.ts b/packages/next/src/server/dev/static-paths-worker.ts index df9ecf572b19..795d6bb6c355 100644 --- a/packages/next/src/server/dev/static-paths-worker.ts +++ b/packages/next/src/server/dev/static-paths-worker.ts @@ -107,7 +107,7 @@ export async function loadStaticPaths({ ] : await collectGenerateParams(components.ComponentMod.tree) - return buildAppStaticPaths({ + return await buildAppStaticPaths({ page: pathname, generateParams, configFileName: config.configFileName, @@ -122,7 +122,7 @@ export async function loadStaticPaths({ }) } - return buildStaticPaths({ + return await buildStaticPaths({ page: pathname, getStaticPaths: components.getStaticPaths, configFileName: config.configFileName,