Skip to content

Commit

Permalink
Fix getStaticPaths modules being cached in dev mode (vercel#10852)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer authored and chibicode committed Mar 6, 2020
1 parent 0557415 commit e69a361
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
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

0 comments on commit e69a361

Please sign in to comment.