Add cache control header for prefetch empty responses (#54732)

Fix #45301

If you deploy Next.js with in front Cloudflare, empty prefetch responses will be cached because there is no `cache-control` header and the extension is .json, Cloudflare considers these requests cacheable assets

If you want a test for this I would like to know if there is an existing one where I can add the header check
This commit is contained in:
Tommaso De Rossi 2023-08-29 22:18:25 +02:00 committed by GitHub
parent fc48fc8c4c
commit 20d05958ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1614,6 +1614,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
!(is404Page || pathname === '/_error')
) {
res.setHeader('x-middleware-skip', '1')
res.setHeader(
'cache-control',
'private, no-cache, no-store, max-age=0, must-revalidate'
)
res.body('{}').send()
return null
}