Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/large-ligers-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix: add vary header to cache interceptor
12 changes: 12 additions & 0 deletions packages/open-next/src/core/routing/cacheInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ 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 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:
* 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";

async function computeCacheControl(
path: string,
body: string,
Expand Down Expand Up @@ -127,6 +138,7 @@ async function generateResult(
...cacheControl,
"content-type": type,
...cachedValue.meta?.headers,
vary: VARY_HEADER,
},
};
}
Expand Down
Loading