Unverified Commit 3f164abd authored by Shohei Maeda's avatar Shohei Maeda Committed by GitHub

Add `cache-control` header on 304 response (#50408)

Next.js currently does not return `cache-control` header on 304 response. Accordingly to RFC, Next.js should include it as well: https://www.rfc-editor.org/rfc/rfc9110#section-15.4.5

> The server generating a 304 response MUST generate any of the following header fields that would have been sent in a [200 (OK)](https://www.rfc-editor.org/rfc/rfc9110#status.200) response to the same request:
> 
> - [Content-Location](https://www.rfc-editor.org/rfc/rfc9110#field.content-location), [Date](https://www.rfc-editor.org/rfc/rfc9110#field.date), [ETag](https://www.rfc-editor.org/rfc/rfc9110#field.etag), and [Vary](https://www.rfc-editor.org/rfc/rfc9110#field.vary)
> - **_Cache-Control_** and Expires (see [[CACHING](https://www.rfc-editor.org/rfc/rfc9110#CACHING)])
parent 5d8d1816
...@@ -63,6 +63,10 @@ export async function sendRenderResult({ ...@@ -63,6 +63,10 @@ export async function sendRenderResult({
res.setHeader('X-Powered-By', 'Next.js') res.setHeader('X-Powered-By', 'Next.js')
} }
if (options != null) {
setRevalidateHeaders(res, options)
}
const payload = result.isDynamic() ? null : await result.toUnchunkedString() const payload = result.isDynamic() ? null : await result.toUnchunkedString()
if (payload !== null) { if (payload !== null) {
...@@ -91,10 +95,6 @@ export async function sendRenderResult({ ...@@ -91,10 +95,6 @@ export async function sendRenderResult({
res.setHeader('Content-Length', Buffer.byteLength(payload)) res.setHeader('Content-Length', Buffer.byteLength(payload))
} }
if (options != null) {
setRevalidateHeaders(res, options)
}
if (req.method === 'HEAD') { if (req.method === 'HEAD') {
res.end(null) res.end(null)
} else if (payload !== null) { } else if (payload !== null) {
......
export async function getStaticProps() {
return {
props: {
world: 'world',
},
revalidate: 10,
}
}
const Page = ({ world }) => {
return (
<div>
<p>hello {world}</p>
</div>
)
}
export default Page
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
renderViaHTTP, renderViaHTTP,
waitFor, waitFor,
getPageFileFromPagesManifest, getPageFileFromPagesManifest,
fetchViaHTTP,
} from 'next-test-utils' } from 'next-test-utils'
import { join } from 'path' import { join } from 'path'
...@@ -79,6 +80,18 @@ describe('SSG Prerender Revalidate', () => { ...@@ -79,6 +80,18 @@ describe('SSG Prerender Revalidate', () => {
runTests('/named', '/named') runTests('/named', '/named')
runTests('/nested', '/nested') runTests('/nested', '/nested')
runTests('/nested/named', '/nested/named') runTests('/nested/named', '/nested/named')
it('should return cache-control header on 304 status', async () => {
const url = `http://localhost:${appPort}`
const res1 = await fetchViaHTTP(url, '/static')
const cacheControl200 = res1.headers.get('Cache-Control')
const etag = res1.headers.get('ETag')
const headers = { 'If-None-Match': etag }
const res2 = await fetchViaHTTP(url, '/static', undefined, { headers })
const cacheControl304 = res2.headers.get('Cache-Control')
expect(cacheControl304).toEqual(cacheControl200)
})
}) })
// Regression test for https://github.com/vercel/next.js/issues/24806 // Regression test for https://github.com/vercel/next.js/issues/24806
......
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