Unverified Commit a00de0c2 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure cache-control is correct for notFound: true with revalidate (#28973)

This ensures we have the correct `cache-control` header when `revalidate` is used with `notFound: true` on `getStaticProps` or `getServerSideProps` pages. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

Fixes: https://app.clubhouse.io/vercel/story/23445
parent 6674e977
......@@ -1819,8 +1819,10 @@ export default class Server {
typeof revalidate !== 'undefined' &&
(!this.renderOpts.dev || (hasServerProps && !isDataReq))
? {
// When the page is 404 cache-control should not be added
private: isPreviewMode || is404Page,
// When the page is 404 cache-control should not be added unless
// we are rendering the 404 page for notFound: true which should
// cache according to revalidate correctly
private: isPreviewMode || (is404Page && cachedData),
stateful: !isSSG,
revalidate,
}
......@@ -1835,10 +1837,15 @@ export default class Server {
res.end('{"notFound":true}')
return null
} else {
await this.render404(req, res, {
pathname,
query,
} as UrlWithParsedQuery)
await this.render404(
req,
res,
{
pathname,
query,
} as UrlWithParsedQuery,
false
)
return null
}
} else if (cachedData.kind === 'REDIRECT') {
......
import fs from 'fs'
import path from 'path'
export async function getStaticProps() {
const data = await fs.promises.readFile(
path.join(process.cwd(), 'data.txt'),
'utf8'
)
if (data.trim() === 'hide') {
return {
notFound: true,
revalidate: 1,
}
}
return {
props: {
hello: 'world',
data,
},
revalidate: 1,
}
}
export default function Page(props) {
return (
<>
<p id="gsp">getStaticProps page</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}
import fs from 'fs'
import path from 'path'
export async function getServerSideProps({ res }) {
res.setHeader('cache-control', 's-maxage=1, stale-while-revalidate')
const data = await fs.promises.readFile(
path.join(process.cwd(), 'data.txt'),
'utf8'
)
if (data.trim() === 'hide') {
return {
notFound: true,
}
}
return {
props: {
hello: 'world',
data,
},
}
}
export default function Page(props) {
return (
<>
<p id="gsp">getStaticProps page</p>
<p id="props">{JSON.stringify(props)}</p>
</>
)
}
......@@ -216,6 +216,50 @@ describe('Required Server Files', () => {
expect(await fs.exists(join(workDir, '.next/server'))).toBe(true)
})
it('should set correct SWR headers with notFound gsp', async () => {
await fs.writeFile(join(workDir, 'data.txt'), 'show')
const res = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual ',
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
await fs.writeFile(join(workDir, 'data.txt'), 'hide')
const res2 = await fetchViaHTTP(appPort, '/gsp', undefined, {
redirect: 'manual ',
})
expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
})
it('should set correct SWR headers with notFound gssp', async () => {
await fs.writeFile(join(workDir, 'data.txt'), 'show')
const res = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ',
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
await fs.writeFile(join(workDir, 'data.txt'), 'hide')
const res2 = await fetchViaHTTP(appPort, '/gssp', undefined, {
redirect: 'manual ',
})
expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
)
})
it('should render SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
......
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