Skip to content

Commit

Permalink
Update to prevent re-using workers for getStaticPaths in dev mode (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Mar 25, 2020
1 parent 67fd72b commit 2ba6db6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
17 changes: 7 additions & 10 deletions packages/next/server/static-paths-worker.ts
@@ -1,8 +1,7 @@
import { buildStaticPaths } from '../build/utils'
import { loadComponents } from '../next-server/server/load-components'

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

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

const components = await loadComponents(
distDir,
Expand All @@ -37,5 +33,6 @@ export async function loadStaticPaths(
)
}

workerWasUsed = true
return buildStaticPaths(pathname, components.getStaticPaths)
}
1 change: 1 addition & 0 deletions test/integration/prerender/pages/blog/[post]/index.js
@@ -1,6 +1,7 @@
import React from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'
import 'firebase/firestore'

export async function getStaticPaths() {
return {
Expand Down
29 changes: 28 additions & 1 deletion test/integration/prerender/test/index.test.js
Expand Up @@ -1065,6 +1065,16 @@ describe('SSG Prerender', () => {
await killApp(app)
})

it('should work with firebase import and getStaticPaths', async () => {
const html = await renderViaHTTP(appPort, '/blog/post-1')
expect(html).toContain('post-1')
expect(html).not.toContain('Error: Failed to load')

const html2 = await renderViaHTTP(appPort, '/blog/post-1')
expect(html2).toContain('post-1')
expect(html2).not.toContain('Error: Failed to load')
})

it('should not cache getStaticPaths errors', async () => {
const errMsg = /The `fallback` key must be returned from getStaticPaths/
await check(() => renderViaHTTP(appPort, '/blog/post-1'), /post-1/)
Expand All @@ -1088,7 +1098,21 @@ describe('SSG Prerender', () => {
})

describe('serverless mode', () => {
const blogPagePath = join(appDir, 'pages/blog/[post]/index.js')
let origBlogPageContent

beforeAll(async () => {
// remove firebase import since it breaks in legacy serverless mode
origBlogPageContent = await fs.readFile(blogPagePath, 'utf8')

await fs.writeFile(
blogPagePath,
origBlogPageContent.replace(
`import 'firebase/firestore'`,
`// import 'firebase/firestore'`
)
)

await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`,
Expand All @@ -1106,7 +1130,10 @@ describe('SSG Prerender', () => {
distPagesDir = join(appDir, '.next/serverless/pages')
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(() => killApp(app))
afterAll(async () => {
await fs.writeFile(blogPagePath, origBlogPageContent)
await killApp(app)
})

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

0 comments on commit 2ba6db6

Please sign in to comment.