Skip to content

Commit 9203f80

Browse files
committed
[next-server] skip setting vary header for basic routes
1 parent 95b2c19 commit 9203f80

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,6 @@ export default abstract class Server<
19791979
resolvedPathname: string
19801980
): void {
19811981
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
19831982

19841983
let addedNextUrlToVary = false
19851984

@@ -1988,11 +1987,9 @@ export default abstract class Server<
19881987
// We use the Vary header to signal this behavior to the client to properly cache the response.
19891988
res.appendHeader('vary', `${baseVaryHeader}, ${NEXT_URL}`)
19901989
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)
19951990
}
1991+
// For other cases such as App Router requests or RSC requests we don't need to set vary header since we already
1992+
// have the _rsc query with the unique hash value.
19961993

19971994
if (!addedNextUrlToVary) {
19981995
// 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

+4-10
Original file line numberDiff line numberDiff line change
@@ -323,25 +323,19 @@ 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 () => {
326+
it('should not return the `vary` header from edge runtime', async () => {
327327
const res = await next.fetch('/dashboard')
328328
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-
)
329+
expect(res.headers.get('vary')).toBe(null)
332330
})
333331

334-
it('should return the `vary` header from pages for flight requests', async () => {
332+
it('should not return the `vary` header from pages for flight requests', async () => {
335333
const res = await next.fetch('/', {
336334
headers: {
337335
['RSC'.toString()]: '1',
338336
},
339337
})
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-
)
338+
expect(res.headers.get('vary')).toBe('Accept-Encoding')
345339
})
346340

347341
it('should pass props from getServerSideProps in root layout', async () => {

0 commit comments

Comments
 (0)