Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getStaticPaths modules being cached in dev mode #10852

Merged
merged 2 commits into from Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/next/server/static-paths-worker.ts
@@ -1,8 +1,8 @@
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
Expand All @@ -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,
Expand Down
40 changes: 40 additions & 0 deletions test/integration/prerender/test/index.test.js
Expand Up @@ -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(
Expand Down