Unverified Commit 250370a0 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure pages/500 handles cache-control as expected (#72050)

When `pages/500` uses `getStaticProps` we can honor statically
generating the page but we can't send a `Cache-Control` with
`stale-while-revalidate` as we shouldn't be caching errored pages ever.
This ensures we handle that case and add a regression test for it.
parent 5d972b59
...@@ -3305,6 +3305,8 @@ export default abstract class Server< ...@@ -3305,6 +3305,8 @@ export default abstract class Server<
const notFoundRevalidate = getRequestMeta(req, 'notFoundRevalidate') const notFoundRevalidate = getRequestMeta(req, 'notFoundRevalidate')
revalidate = revalidate =
typeof notFoundRevalidate === 'undefined' ? 0 : notFoundRevalidate typeof notFoundRevalidate === 'undefined' ? 0 : notFoundRevalidate
} else if (is500Page) {
revalidate = 0
} }
// If the cache entry has a revalidate value that's a number, use it. // If the cache entry has a revalidate value that's a number, use it.
......
...@@ -120,6 +120,50 @@ describe('500 Page Support', () => { ...@@ -120,6 +120,50 @@ describe('500 Page Support', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode 2', 'production mode 2',
() => { () => {
it('should have correct cache control for 500 page with getStaticProps', async () => {
const orig500 = await fs.readFile(pages500, 'utf8')
try {
await fs.writeFile(
pages500,
`
export default function Page() {
return (
<p>custom 500</p>
)
}
export function getStaticProps() {
return {
props: {
now: Date.now(),
}
}
}
`
)
await fs.remove(join(appDir, '.next'))
const { code } = await nextBuild(appDir, [], {
stderr: true,
stdout: true,
})
expect(code).toBe(0)
const appPort = await findPort()
const app = await nextStart(appDir, appPort)
const res = await fetchViaHTTP(appPort, '/err')
await killApp(app)
expect(res.status).toBe(500)
expect(res.headers.get('cache-control')).toBe(
'private, no-cache, no-store, max-age=0, must-revalidate'
)
} finally {
await fs.writeFile(pages500, orig500)
}
})
it('does not build 500 statically with getInitialProps in _app', async () => { it('does not build 500 statically with getInitialProps in _app', async () => {
await fs.writeFile( await fs.writeFile(
pagesApp, pagesApp,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment