Skip to content

Commit f2c86ca

Browse files
sommeeeerkhuezy
andauthored
fix: add Vary header to cache interceptor (#916)
* fix: add Vary header to cache interceptor * review * more comment * more comment * Update packages/open-next/src/core/routing/cacheInterceptor.ts Co-authored-by: khuezy <[email protected]> * next pr * add Next-Url header --------- Co-authored-by: khuezy <[email protected]>
1 parent 7d60df5 commit f2c86ca

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

.changeset/large-ligers-decide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix: add vary header to cache interceptor

packages/open-next/src/core/routing/cacheInterceptor.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ import { generateMessageGroupId } from "./queue";
1313
const CACHE_ONE_YEAR = 60 * 60 * 24 * 365;
1414
const CACHE_ONE_MONTH = 60 * 60 * 24 * 30;
1515

16+
/*
17+
* We use this header to prevent Firefox (and possibly some CDNs) from incorrectly reusing the RSC responses during caching.
18+
* This can especially happen when there's a redirect in the middleware as the `_rsc` query parameter is not visible there.
19+
* So it will get dropped during the redirect, which results in the RSC response being cached instead of the actual HTML on the path `/`.
20+
* This value can be found in the routes manifest, under `rsc.varyHeader`.
21+
* They recompute it here in Next:
22+
* https://github.com/vercel/next.js/blob/c5bf5bb4c8b01b1befbbfa7ad97a97476ee9d0d7/packages/next/src/server/base-server.ts#L2011
23+
* Also see this PR: https://github.com/vercel/next.js/pull/79426
24+
*/
25+
const VARY_HEADER =
26+
"RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch, Next-Url";
27+
1628
async function computeCacheControl(
1729
path: string,
1830
body: string,
@@ -127,6 +139,7 @@ async function generateResult(
127139
...cacheControl,
128140
"content-type": type,
129141
...cachedValue.meta?.headers,
142+
vary: VARY_HEADER,
130143
},
131144
};
132145
}

0 commit comments

Comments
 (0)