未验证 提交 5dfc7da2 编写于 作者: J Joe Haddad 提交者: GitHub

Fix getStaticPaths modules being cached in dev mode (#10852)

上级 69ba7934
import { join } from 'path'
import { buildStaticPaths } from '../build/utils'
import { getPagePath } from '../next-server/server/require'
import { loadComponents } from '../next-server/server/load-components'
import { PAGES_MANIFEST, SERVER_DIRECTORY } from '../next-server/lib/constants'
// store initial require modules so we don't clear them below
const initialCache = new Set(Object.keys(require.cache))
// we call getStaticPaths in a separate process to ensure
// side-effects aren't relied on in dev that will break
......@@ -16,10 +16,11 @@ export async function loadStaticPaths(
// 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
delete require.cache[join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST)]
const pagePath = await getPagePath(pathname, distDir, serverless, true)
delete require.cache[pagePath]
Object.keys(require.cache).forEach(mod => {
if (!initialCache.has(mod)) {
delete require.cache[mod]
}
})
const components = await loadComponents(
distDir,
......
......@@ -983,6 +983,46 @@ describe('SSG Prerender', () => {
runTests(true)
})
describe('dev mode getStaticPaths', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
// we set cpus to 1 so that we make sure the requests
// aren't being cached at the jest-worker level
`module.exports = { experimental: { cpus: 1 } }`,
'utf8'
)
await fs.remove(join(appDir, '.next'))
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await fs.remove(nextConfig)
await killApp(app)
})
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/)
const blogPage = join(appDir, 'pages/blog/[post]/index.js')
const origContent = await fs.readFile(blogPage, 'utf8')
await fs.writeFile(
blogPage,
origContent.replace('fallback: true,', '/* fallback: true, */')
)
try {
await check(() => renderViaHTTP(appPort, '/blog/post-1'), errMsg)
await fs.writeFile(blogPage, origContent)
await check(() => renderViaHTTP(appPort, '/blog/post-1'), /post-1/)
} finally {
await fs.writeFile(blogPage, origContent)
}
})
})
describe('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册