Skip to content

Commit

Permalink
Add cache-control header on 304 response (vercel#50408)
Browse files Browse the repository at this point in the history
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)])
  • Loading branch information
smaeda-ks authored and hydRAnger committed Jun 12, 2023
1 parent 53944f4 commit 8a62462
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/next/src/server/send-payload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export async function sendRenderResult({
res.setHeader('X-Powered-By', 'Next.js')
}

if (options != null) {
setRevalidateHeaders(res, options)
}

const payload = result.isDynamic() ? null : await result.toUnchunkedString()

if (payload !== null) {
Expand Down Expand Up @@ -91,10 +95,6 @@ export async function sendRenderResult({
res.setHeader('Content-Length', Buffer.byteLength(payload))
}

if (options != null) {
setRevalidateHeaders(res, options)
}

if (req.method === 'HEAD') {
res.end(null)
} else if (payload !== null) {
Expand Down
18 changes: 18 additions & 0 deletions test/integration/prerender-revalidate/pages/static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export async function getStaticProps() {
return {
props: {
world: 'world',
},
revalidate: 10,
}
}

const Page = ({ world }) => {
return (
<div>
<p>hello {world}</p>
</div>
)
}

export default Page
13 changes: 13 additions & 0 deletions test/integration/prerender-revalidate/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
renderViaHTTP,
waitFor,
getPageFileFromPagesManifest,
fetchViaHTTP,
} from 'next-test-utils'
import { join } from 'path'

Expand Down Expand Up @@ -79,6 +80,18 @@ describe('SSG Prerender Revalidate', () => {
runTests('/named', '/named')
runTests('/nested', '/nested')
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
Expand Down

0 comments on commit 8a62462

Please sign in to comment.