未验证 提交 2ba6db65 编写于 作者: J JJ Kasper 提交者: GitHub

Update to prevent re-using workers for getStaticPaths in dev mode (#11347)

上级 67fd72b7
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
......@@ -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,
......@@ -37,5 +33,6 @@ export async function loadStaticPaths(
)
}
workerWasUsed = true
return buildStaticPaths(pathname, components.getStaticPaths)
}
import React from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'
import 'firebase/firestore'
export async function getStaticPaths() {
return {
......
......@@ -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/)
......@@ -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' }`,
......@@ -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()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册