@@ -104,6 +104,7 @@ import {
104104 NEXT_ROUTER_SEGMENT_PREFETCH_HEADER ,
105105 NEXT_DID_POSTPONE_HEADER ,
106106 NEXT_URL ,
107+ NEXT_ROUTER_STATE_TREE_HEADER ,
107108 NEXT_IS_PRERENDER_HEADER ,
108109} from '../client/components/app-router-headers'
109110import type {
@@ -1981,16 +1982,21 @@ export default abstract class Server<
19811982 isAppPath : boolean ,
19821983 resolvedPathname : string
19831984 ) : void {
1985+ const baseVaryHeader = `${ RSC_HEADER } , ${ NEXT_ROUTER_STATE_TREE_HEADER } , ${ NEXT_ROUTER_PREFETCH_HEADER } , ${ NEXT_ROUTER_SEGMENT_PREFETCH_HEADER } `
1986+ const isRSCRequest = getRequestMeta ( req , 'isRSCRequest' ) ?? false
1987+
19841988 let addedNextUrlToVary = false
19851989
19861990 if ( isAppPath && this . pathCouldBeIntercepted ( resolvedPathname ) ) {
19871991 // Interception route responses can vary based on the `Next-URL` header.
19881992 // We use the Vary header to signal this behavior to the client to properly cache the response.
1989- res . appendHeader ( 'vary' , `${ NEXT_URL } ` )
1993+ res . appendHeader ( 'vary' , `${ baseVaryHeader } , ${ NEXT_URL } ` )
19901994 addedNextUrlToVary = true
1995+ } else if ( isAppPath || isRSCRequest ) {
1996+ // We don't need to include `Next-URL` in the Vary header for non-interception routes since it won't affect the response.
1997+ // We also set this header for pages to avoid caching issues when navigating between pages and app.
1998+ res . appendHeader ( 'vary' , baseVaryHeader )
19911999 }
1992- // For other cases such as App Router requests or RSC requests we don't need to set vary header since we already
1993- // have the _rsc query with the unique hash value.
19942000
19952001 if ( ! addedNextUrlToVary ) {
19962002 // Remove `Next-URL` from the request headers we determined it wasn't necessary to include in the Vary header.
0 commit comments