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 { ...@@ -1819,8 +1819,10 @@ export default class Server {
typeof revalidate !== 'undefined' && typeof revalidate !== 'undefined' &&
(!this.renderOpts.dev || (hasServerProps && !isDataReq)) (!this.renderOpts.dev || (hasServerProps && !isDataReq))
? { ? {
// When the page is 404 cache-control should not be added // When the page is 404 cache-control should not be added unless
private: isPreviewMode || is404Page, // we are rendering the 404 page for notFound: true which should
// cache according to revalidate correctly
private: isPreviewMode || (is404Page && cachedData),
stateful: !isSSG, stateful: !isSSG,
revalidate, revalidate,
} }
...@@ -1835,10 +1837,15 @@ export default class Server { ...@@ -1835,10 +1837,15 @@ export default class Server {
res.end('{"notFound":true}') res.end('{"notFound":true}')
return null return null
} else { } else {
await this.render404(req, res, { await this.render404(
pathname, req,
query, res,
} as UrlWithParsedQuery) {
pathname,
query,
} as UrlWithParsedQuery,
false
)
return null return null
} }
} else if (cachedData.kind === 'REDIRECT') { } 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', () => { ...@@ -216,6 +216,50 @@ describe('Required Server Files', () => {
expect(await fs.exists(join(workDir, '.next/server'))).toBe(true) 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 () => { it('should render SSR page correctly', async () => {
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html) 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