Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent caching page with 304 status #57737

Merged
merged 6 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
return
}
const { req, res } = ctx
const originalStatus = res.statusCode
const { body, type } = payload
let { revalidate } = payload
if (!res.sent) {
Expand All @@ -1502,13 +1503,14 @@ export default abstract class Server<ServerOptions extends Options = Options> {
revalidate = undefined
}

return this.sendRenderResult(req, res, {
await this.sendRenderResult(req, res, {
result: body,
type,
generateEtags,
poweredByHeader,
revalidate,
})
res.statusCode = originalStatus
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/production/app-dir-prevent-304-caching/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
9 changes: 9 additions & 0 deletions test/production/app-dir-prevent-304-caching/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default async function Page() {
const time = await new Promise((resolve) => {
setTimeout(500, resolve(new Date().getTime()))
})

return <div>Time: {time}</div>
}

export const revalidate = 1
60 changes: 60 additions & 0 deletions test/production/app-dir-prevent-304-caching/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { join } from 'path'
import { fetchViaHTTP, waitFor } from 'next-test-utils'

describe('app-dir-prevent-304-caching', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
'next.config.js': new FileRef(join(__dirname, 'next.config.js')),
app: new FileRef(join(__dirname, 'app')),
},
})
})
afterAll(() => next.destroy())

// https://github.com/vercel/next.js/issues/56580
it('should not cache 304 status', async () => {
// Fresh call
const rFresh = await fetchViaHTTP(next.url, '/')
expect(rFresh.status).toBe(200)

await waitFor(500)

// Cache HIT but still 200
const rHit = await fetchViaHTTP(
next.url,
'/',
{},
{
headers: {
'If-None-Match': rFresh.headers.get('etag'),
},
}
)
expect(rHit.status).toBe(200)
await waitFor(500)

// Here happens the race condition
const r304 = await fetchViaHTTP(
next.url,
'/',
{},
{
headers: {
'If-None-Match': rHit.headers.get('etag'),
},
}
)
expect(r304.status).toBe(304)
// ... Postponed but should not save 304 ...
await waitFor(1000)

// Now without cache headers should 200
const rStillFresh = await fetchViaHTTP(next.url, '/')
expect(rStillFresh.status).toBe(200)
})
})
4 changes: 4 additions & 0 deletions test/production/app-dir-prevent-304-caching/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import("next").NextConfig} */
const nextConfig = {}

module.exports = nextConfig
Loading