Skip to content

Commit c5896f2

Browse files
timneutkensijjk
andauthored
Add vary header to fix incorrectly caching RSC as HTML response (#41479)
Add failing test for the back button download bug. Created it as a new app given that adding it to the existing `app` suite did not reproduce the issue for some reason. The underlying reason is that we need to add `Vary: __rsc__, __next_router_prefetch__` to ensure Chrome does not cache the response and serve it on back button. The download was caused by the `application/octet-stream` content type, but that was just a consequence of serving the wrong response. <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change that you're making: --> ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) Co-authored-by: JJ Kasper <jj@jjsweb.site>
1 parent 6101bf6 commit c5896f2

File tree

8 files changed

+106
-2
lines changed

8 files changed

+106
-2
lines changed

packages/next/server/base-server.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1054,8 +1054,13 @@ export default abstract class Server<ServerOptions extends Options = Options> {
10541054
) &&
10551055
(isSSG || hasServerProps)
10561056

1057-
if (isAppPath && req.headers['__rsc__']) {
1058-
if (isSSG) {
1057+
if (isAppPath) {
1058+
res.setHeader(
1059+
'vary',
1060+
'__rsc__, __next_router_state_tree__, __next_router_prefetch__'
1061+
)
1062+
1063+
if (isSSG && req.headers['__rsc__']) {
10591064
isDataReq = true
10601065
// strip header so we generate HTML still
10611066
if (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { createNext, FileRef } from 'e2e-utils'
2+
import { NextInstance } from 'test/lib/next-modes/base'
3+
import path from 'path'
4+
import webdriver from 'next-webdriver'
5+
6+
describe('app-dir back button download bug', () => {
7+
if ((global as any).isNextDeploy) {
8+
it('should skip next deploy for now', () => {})
9+
return
10+
}
11+
12+
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
13+
it('should skip for react v17', () => {})
14+
return
15+
}
16+
let next: NextInstance
17+
18+
beforeAll(async () => {
19+
next = await createNext({
20+
files: new FileRef(path.join(__dirname, 'back-button-download-bug')),
21+
dependencies: {
22+
react: 'experimental',
23+
'react-dom': 'experimental',
24+
},
25+
skipStart: true,
26+
})
27+
28+
await next.start()
29+
})
30+
afterAll(() => next.destroy())
31+
32+
it('should redirect route when clicking link', async () => {
33+
const browser = await webdriver(next.url, '/')
34+
const text = await browser
35+
.elementByCss('#to-post-1')
36+
.click()
37+
.waitForElementByCss('#post-page')
38+
.text()
39+
expect(text).toBe('This is the post page')
40+
41+
await browser.back()
42+
43+
expect(await browser.waitForElementByCss('#home-page').text()).toBe('Home!')
44+
})
45+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function RootLayout({ children }) {
2+
return (
3+
<html lang="en">
4+
<head></head>
5+
<body>{children}</body>
6+
</html>
7+
)
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Link from 'next/link'
2+
3+
export default function Home() {
4+
return (
5+
<>
6+
<h1 id="home-page">Home!</h1>
7+
<Link href="/post/1">
8+
<a id="to-post-1">To post 1</a>
9+
</Link>
10+
<Link href="/">To /</Link>
11+
</>
12+
)
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** @type {import('next').NextConfig} */
2+
module.exports = {
3+
reactStrictMode: true,
4+
experimental: {
5+
appDir: true,
6+
},
7+
images: {
8+
domains: ['res.cloudinary.com', 'avatars.githubusercontent.com'],
9+
},
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function MyApp({ Component, pageProps }) {
2+
return <Component {...pageProps} />
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Document, { Head, Html, Main, NextScript } from 'next/document'
2+
3+
class MyDocument extends Document {
4+
render() {
5+
return (
6+
<Html lang="en">
7+
<Head />
8+
<body>
9+
<Main />
10+
<NextScript />
11+
</body>
12+
</Html>
13+
)
14+
}
15+
}
16+
17+
export default MyDocument
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Post() {
2+
return <h1 id="post-page">This is the post page</h1>
3+
}

0 commit comments

Comments
 (0)