From d4fb85a43ff655edc339ec153fb9375847fa48dc Mon Sep 17 00:00:00 2001 From: Magnus Dahl Eide Date: Tue, 24 Jun 2025 22:48:06 +0200 Subject: [PATCH 1/7] fix: add Vary header to cache interceptor --- .changeset/large-ligers-decide.md | 5 +++++ packages/open-next/src/core/routing/cacheInterceptor.ts | 4 ++++ 2 files changed, 9 insertions(+) create mode 100644 .changeset/large-ligers-decide.md diff --git a/.changeset/large-ligers-decide.md b/.changeset/large-ligers-decide.md new file mode 100644 index 000000000..1beae0bf3 --- /dev/null +++ b/.changeset/large-ligers-decide.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/aws": patch +--- + +fix: add vary header to cache interceptor diff --git a/packages/open-next/src/core/routing/cacheInterceptor.ts b/packages/open-next/src/core/routing/cacheInterceptor.ts index 0a33e3824..5470c7740 100644 --- a/packages/open-next/src/core/routing/cacheInterceptor.ts +++ b/packages/open-next/src/core/routing/cacheInterceptor.ts @@ -13,6 +13,9 @@ import { generateMessageGroupId } from "./queue"; const CACHE_ONE_YEAR = 60 * 60 * 24 * 365; const CACHE_ONE_MONTH = 60 * 60 * 24 * 30; +const VARY_HEADER = + "RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch"; + async function computeCacheControl( path: string, body: string, @@ -127,6 +130,7 @@ async function generateResult( ...cacheControl, "content-type": type, ...cachedValue.meta?.headers, + vary: VARY_HEADER, }, }; } From 158236a35c47ca02112a0755d753265fcdc1582d Mon Sep 17 00:00:00 2001 From: Magnus Dahl Eide Date: Wed, 25 Jun 2025 17:14:01 +0200 Subject: [PATCH 2/7] review --- packages/open-next/src/core/routing/cacheInterceptor.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/open-next/src/core/routing/cacheInterceptor.ts b/packages/open-next/src/core/routing/cacheInterceptor.ts index 5470c7740..cf2d79872 100644 --- a/packages/open-next/src/core/routing/cacheInterceptor.ts +++ b/packages/open-next/src/core/routing/cacheInterceptor.ts @@ -13,6 +13,11 @@ import { generateMessageGroupId } from "./queue"; const CACHE_ONE_YEAR = 60 * 60 * 24 * 365; const CACHE_ONE_MONTH = 60 * 60 * 24 * 30; +/* + * We use this header to prevent Firefox (and possibly some CDNs) from incorrectly reusing the RSC responses during caching. + * This can especially happen when theres a redirect in the middleware as the `_rsc` query parameter is not visible there. + * So it will get dropped during the redirect, which results in the RSC response being cached instead of the actual HTML on the path `/`. + */ const VARY_HEADER = "RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch"; From c9d26b1dc4bbed881cd64c462c131ce3d762946c Mon Sep 17 00:00:00 2001 From: Magnus Dahl Eide Date: Wed, 25 Jun 2025 17:17:13 +0200 Subject: [PATCH 3/7] more comment --- packages/open-next/src/core/routing/cacheInterceptor.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/open-next/src/core/routing/cacheInterceptor.ts b/packages/open-next/src/core/routing/cacheInterceptor.ts index cf2d79872..dd988aa1d 100644 --- a/packages/open-next/src/core/routing/cacheInterceptor.ts +++ b/packages/open-next/src/core/routing/cacheInterceptor.ts @@ -17,6 +17,9 @@ const CACHE_ONE_MONTH = 60 * 60 * 24 * 30; * We use this header to prevent Firefox (and possibly some CDNs) from incorrectly reusing the RSC responses during caching. * This can especially happen when theres a redirect in the middleware as the `_rsc` query parameter is not visible there. * So it will get dropped during the redirect, which results in the RSC response being cached instead of the actual HTML on the path `/`. + * This value can be found in the routes manifest. + * They recompute it here in Next: + * https://github.com/vercel/next.js/blob/c5bf5bb4c8b01b1befbbfa7ad97a97476ee9d0d7/packages/next/src/server/base-server.ts#L2011 */ const VARY_HEADER = "RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch"; From 014a944c5da591eb951f0afacaa4a0c15b722e0b Mon Sep 17 00:00:00 2001 From: Magnus Dahl Eide Date: Wed, 25 Jun 2025 17:18:49 +0200 Subject: [PATCH 4/7] more comment --- packages/open-next/src/core/routing/cacheInterceptor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/open-next/src/core/routing/cacheInterceptor.ts b/packages/open-next/src/core/routing/cacheInterceptor.ts index dd988aa1d..f825eeb96 100644 --- a/packages/open-next/src/core/routing/cacheInterceptor.ts +++ b/packages/open-next/src/core/routing/cacheInterceptor.ts @@ -17,7 +17,7 @@ const CACHE_ONE_MONTH = 60 * 60 * 24 * 30; * We use this header to prevent Firefox (and possibly some CDNs) from incorrectly reusing the RSC responses during caching. * This can especially happen when theres a redirect in the middleware as the `_rsc` query parameter is not visible there. * So it will get dropped during the redirect, which results in the RSC response being cached instead of the actual HTML on the path `/`. - * This value can be found in the routes manifest. + * This value can be found in the routes manifest, under `rsc.varyHeader`. * They recompute it here in Next: * https://github.com/vercel/next.js/blob/c5bf5bb4c8b01b1befbbfa7ad97a97476ee9d0d7/packages/next/src/server/base-server.ts#L2011 */ From 2d969abc881204696b6b2e90eb0b5eeb0053e390 Mon Sep 17 00:00:00 2001 From: Magnus Date: Wed, 25 Jun 2025 20:04:27 +0200 Subject: [PATCH 5/7] Update packages/open-next/src/core/routing/cacheInterceptor.ts Co-authored-by: khuezy --- packages/open-next/src/core/routing/cacheInterceptor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/open-next/src/core/routing/cacheInterceptor.ts b/packages/open-next/src/core/routing/cacheInterceptor.ts index f825eeb96..71ca01782 100644 --- a/packages/open-next/src/core/routing/cacheInterceptor.ts +++ b/packages/open-next/src/core/routing/cacheInterceptor.ts @@ -15,7 +15,7 @@ const CACHE_ONE_MONTH = 60 * 60 * 24 * 30; /* * We use this header to prevent Firefox (and possibly some CDNs) from incorrectly reusing the RSC responses during caching. - * This can especially happen when theres a redirect in the middleware as the `_rsc` query parameter is not visible there. + * This can especially happen when there's a redirect in the middleware as the `_rsc` query parameter is not visible there. * So it will get dropped during the redirect, which results in the RSC response being cached instead of the actual HTML on the path `/`. * This value can be found in the routes manifest, under `rsc.varyHeader`. * They recompute it here in Next: From e4f1532a41b1f05ea5d81a71197b3c5da84fd67e Mon Sep 17 00:00:00 2001 From: Magnus Dahl Eide Date: Wed, 25 Jun 2025 21:14:22 +0200 Subject: [PATCH 6/7] next pr --- packages/open-next/src/core/routing/cacheInterceptor.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/open-next/src/core/routing/cacheInterceptor.ts b/packages/open-next/src/core/routing/cacheInterceptor.ts index 71ca01782..cdbd8980b 100644 --- a/packages/open-next/src/core/routing/cacheInterceptor.ts +++ b/packages/open-next/src/core/routing/cacheInterceptor.ts @@ -20,6 +20,7 @@ const CACHE_ONE_MONTH = 60 * 60 * 24 * 30; * This value can be found in the routes manifest, under `rsc.varyHeader`. * They recompute it here in Next: * https://github.com/vercel/next.js/blob/c5bf5bb4c8b01b1befbbfa7ad97a97476ee9d0d7/packages/next/src/server/base-server.ts#L2011 + * Also see this PR: https://github.com/vercel/next.js/pull/79426 */ const VARY_HEADER = "RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch"; From 64ee772d1d07cbf243dbda10d07243ceeff7afd5 Mon Sep 17 00:00:00 2001 From: Magnus Dahl Eide Date: Wed, 25 Jun 2025 21:31:20 +0200 Subject: [PATCH 7/7] add Next-Url header --- packages/open-next/src/core/routing/cacheInterceptor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/open-next/src/core/routing/cacheInterceptor.ts b/packages/open-next/src/core/routing/cacheInterceptor.ts index cdbd8980b..70dd083f8 100644 --- a/packages/open-next/src/core/routing/cacheInterceptor.ts +++ b/packages/open-next/src/core/routing/cacheInterceptor.ts @@ -23,7 +23,7 @@ const CACHE_ONE_MONTH = 60 * 60 * 24 * 30; * Also see this PR: https://github.com/vercel/next.js/pull/79426 */ const VARY_HEADER = - "RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch"; + "RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch, Next-Url"; async function computeCacheControl( path: string,