Skip to content

Commit f2cf3dc

Browse files
authored
[next-server] skip setting vary header for basic routes (#77797)
### What After vercel/vercel#13011 landed on Vercel CLI, noticing that we're still checking the `vary` header in the tests, which is obselete now. In this PR we delete the handling for setting the `vary` header but still keep the cases of interception routes since it's still needed. ### Why Since v13.4.6 we added the `_rsc` query with the unique hash, `vary` header becomes not required anymore. Closes NDX-1010
1 parent 689038b commit f2cf3dc

File tree

3 files changed

+5
-42
lines changed

3 files changed

+5
-42
lines changed

Diff for: packages/next/src/server/base-server.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ import {
104104
NEXT_ROUTER_SEGMENT_PREFETCH_HEADER,
105105
NEXT_DID_POSTPONE_HEADER,
106106
NEXT_URL,
107-
NEXT_ROUTER_STATE_TREE_HEADER,
108107
NEXT_IS_PRERENDER_HEADER,
109108
} from '../client/components/app-router-headers'
110109
import type {
@@ -1978,21 +1977,16 @@ export default abstract class Server<
19781977
isAppPath: boolean,
19791978
resolvedPathname: string
19801979
): void {
1981-
const baseVaryHeader = `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE_HEADER}, ${NEXT_ROUTER_PREFETCH_HEADER}, ${NEXT_ROUTER_SEGMENT_PREFETCH_HEADER}`
1982-
const isRSCRequest = getRequestMeta(req, 'isRSCRequest') ?? false
1983-
19841980
let addedNextUrlToVary = false
19851981

19861982
if (isAppPath && this.pathCouldBeIntercepted(resolvedPathname)) {
19871983
// Interception route responses can vary based on the `Next-URL` header.
19881984
// We use the Vary header to signal this behavior to the client to properly cache the response.
1989-
res.appendHeader('vary', `${baseVaryHeader}, ${NEXT_URL}`)
1985+
res.appendHeader('vary', `${NEXT_URL}`)
19901986
addedNextUrlToVary = true
1991-
} else if (isAppPath || isRSCRequest) {
1992-
// We don't need to include `Next-URL` in the Vary header for non-interception routes since it won't affect the response.
1993-
// We also set this header for pages to avoid caching issues when navigating between pages and app.
1994-
res.appendHeader('vary', baseVaryHeader)
19951987
}
1988+
// For other cases such as App Router requests or RSC requests we don't need to set vary header since we already
1989+
// have the _rsc query with the unique hash value.
19961990

19971991
if (!addedNextUrlToVary) {
19981992
// Remove `Next-URL` from the request headers we determined it wasn't necessary to include in the Vary header.

Diff for: test/e2e/app-dir/app/index.test.ts

-21
Original file line numberDiff line numberDiff line change
@@ -323,27 +323,6 @@ describe('app dir - basic', () => {
323323
expect(res.headers.get('Content-Type')).toBe('text/x-component')
324324
})
325325

326-
it('should return the `vary` header from edge runtime', async () => {
327-
const res = await next.fetch('/dashboard')
328-
expect(res.headers.get('x-edge-runtime')).toBe('1')
329-
expect(res.headers.get('vary')).toBe(
330-
'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch'
331-
)
332-
})
333-
334-
it('should return the `vary` header from pages for flight requests', async () => {
335-
const res = await next.fetch('/', {
336-
headers: {
337-
['RSC'.toString()]: '1',
338-
},
339-
})
340-
expect(res.headers.get('vary')).toBe(
341-
isNextDeploy
342-
? 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch'
343-
: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch, Accept-Encoding'
344-
)
345-
})
346-
347326
it('should pass props from getServerSideProps in root layout', async () => {
348327
const $ = await next.render$('/dashboard')
349328
expect($('title').first().text()).toBe('hello world')

Diff for: test/e2e/vary-header/test/index.test.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,16 @@ describe('Vary Header Tests', () => {
1212
expect(res.headers.get('vary')).toContain('Custom-Header')
1313
})
1414

15-
it('should preserve custom vary header and append RSC headers in app route handlers', async () => {
15+
it('should preserve custom vary header', async () => {
1616
const res = await next.fetch('/normal')
1717
const varyHeader = res.headers.get('vary')
1818

1919
// Custom header is preserved
2020
expect(varyHeader).toContain('User-Agent')
2121
expect(res.headers.get('cache-control')).toBe('s-maxage=3600')
22-
23-
// Next.js internal headers are appended
24-
expect(varyHeader).toContain('RSC')
25-
expect(varyHeader).toContain('Next-Router-State-Tree')
26-
expect(varyHeader).toContain('Next-Router-Prefetch')
2722
})
2823

29-
it('should preserve middleware vary header in combination with route handlers', async () => {
24+
it('should preserve middleware vary header', async () => {
3025
const res = await next.fetch('/normal')
3126
const varyHeader = res.headers.get('vary')
3227
const customHeader = res.headers.get('my-custom-header')
@@ -37,10 +32,5 @@ describe('Vary Header Tests', () => {
3732
// Both middleware and route handler vary headers are preserved
3833
expect(varyHeader).toContain('my-custom-header')
3934
expect(varyHeader).toContain('User-Agent')
40-
41-
// Next.js internal headers are still present
42-
expect(varyHeader).toContain('RSC')
43-
expect(varyHeader).toContain('Next-Router-State-Tree')
44-
expect(varyHeader).toContain('Next-Router-Prefetch')
4535
})
4636
})

0 commit comments

Comments
 (0)