Skip to content

Commit 2ba6db6

Browse files
authored
Update to prevent re-using workers for getStaticPaths in dev mode (#11347)
1 parent 67fd72b commit 2ba6db6

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

packages/next/server/static-paths-worker.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { buildStaticPaths } from '../build/utils'
22
import { loadComponents } from '../next-server/server/load-components'
33

4-
// store initial require modules so we don't clear them below
5-
const initialCache = new Set(Object.keys(require.cache))
4+
let workerWasUsed = false
65

76
// we call getStaticPaths in a separate process to ensure
87
// side-effects aren't relied on in dev that will break
@@ -13,14 +12,11 @@ export async function loadStaticPaths(
1312
pathname: string,
1413
serverless: boolean
1514
) {
16-
// we need to clear any modules manually here since the
17-
// require-cache-hot-loader doesn't affect require cache here
18-
// since we're in a separate process
19-
Object.keys(require.cache).forEach(mod => {
20-
if (!initialCache.has(mod)) {
21-
delete require.cache[mod]
22-
}
23-
})
15+
// we only want to use each worker once to prevent any invalid
16+
// caches
17+
if (workerWasUsed) {
18+
process.exit(1)
19+
}
2420

2521
const components = await loadComponents(
2622
distDir,
@@ -37,5 +33,6 @@ export async function loadStaticPaths(
3733
)
3834
}
3935

36+
workerWasUsed = true
4037
return buildStaticPaths(pathname, components.getStaticPaths)
4138
}

test/integration/prerender/pages/blog/[post]/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react'
22
import Link from 'next/link'
33
import { useRouter } from 'next/router'
4+
import 'firebase/firestore'
45

56
export async function getStaticPaths() {
67
return {

test/integration/prerender/test/index.test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,16 @@ describe('SSG Prerender', () => {
10651065
await killApp(app)
10661066
})
10671067

1068+
it('should work with firebase import and getStaticPaths', async () => {
1069+
const html = await renderViaHTTP(appPort, '/blog/post-1')
1070+
expect(html).toContain('post-1')
1071+
expect(html).not.toContain('Error: Failed to load')
1072+
1073+
const html2 = await renderViaHTTP(appPort, '/blog/post-1')
1074+
expect(html2).toContain('post-1')
1075+
expect(html2).not.toContain('Error: Failed to load')
1076+
})
1077+
10681078
it('should not cache getStaticPaths errors', async () => {
10691079
const errMsg = /The `fallback` key must be returned from getStaticPaths/
10701080
await check(() => renderViaHTTP(appPort, '/blog/post-1'), /post-1/)
@@ -1088,7 +1098,21 @@ describe('SSG Prerender', () => {
10881098
})
10891099

10901100
describe('serverless mode', () => {
1101+
const blogPagePath = join(appDir, 'pages/blog/[post]/index.js')
1102+
let origBlogPageContent
1103+
10911104
beforeAll(async () => {
1105+
// remove firebase import since it breaks in legacy serverless mode
1106+
origBlogPageContent = await fs.readFile(blogPagePath, 'utf8')
1107+
1108+
await fs.writeFile(
1109+
blogPagePath,
1110+
origBlogPageContent.replace(
1111+
`import 'firebase/firestore'`,
1112+
`// import 'firebase/firestore'`
1113+
)
1114+
)
1115+
10921116
await fs.writeFile(
10931117
nextConfig,
10941118
`module.exports = { target: 'serverless' }`,
@@ -1106,7 +1130,10 @@ describe('SSG Prerender', () => {
11061130
distPagesDir = join(appDir, '.next/serverless/pages')
11071131
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
11081132
})
1109-
afterAll(() => killApp(app))
1133+
afterAll(async () => {
1134+
await fs.writeFile(blogPagePath, origBlogPageContent)
1135+
await killApp(app)
1136+
})
11101137

11111138
it('renders data correctly', async () => {
11121139
const port = await findPort()

0 commit comments

Comments
 (0)