Skip to content

Commit 6ce25be

Browse files
committed
Remove the cache property from calls to new Request(...) in React
1 parent f150546 commit 6ce25be

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

.changeset/odd-buses-lay.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudflare/next-on-pages': patch
3+
---
4+
5+
Remove `cache` from React's `new Request(...)` cache.

.changeset/tame-islands-arrive.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudflare/next-on-pages': patch
3+
---
4+
5+
Strip the `cache` property from our patched fetch.

packages/next-on-pages/src/buildApplication/processVercelFunctions/dedupeEdgeFunctions.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,27 @@ function fixFunctionContents(contents: string): string {
491491

492492
// The workers runtime does not implement `cache` on RequestInit. This is used in Next.js' patched fetch.
493493
// Due to this, we remove the `cache` property from those that Next.js adds to RequestInit.
494-
// https://github.com/vercel/next.js/blob/269114b5cc583f0c91e687c1aeb61503ef681b91/packages/next/src/server/lib/patch-fetch.ts#L304
494+
// https://github.com/vercel/next.js/blob/9ec37c12/packages/next/src/server/lib/patch-fetch.ts#L504
495495
contents = contents.replace(
496496
/"cache",("credentials","headers","integrity","keepalive","method","mode","redirect","referrer")/gm,
497497
'$1',
498498
);
499499

500+
// React's server code creates new Request objects that can contain the `cache` property in RequestInit.
501+
// This is not supported on workerd, so we need to replace their ternary statement with a way to strip
502+
// the `cache` property from the `RequestInit` object.
503+
// https://github.com/vercel/next.js/blob/9ec37c12/packages/next/src/compiled/react/cjs/react.react-server.production.js#L87
504+
contents = contents.replace(
505+
/([\w$]+) instanceof URL\?new Request\([\w$]+,([\w$]+)\):[\w$]+;/gm,
506+
`(() => {
507+
if ($1 instanceof URL) {
508+
const { cache, ...init } = $2 ?? {};
509+
return new Request($1, init);
510+
}
511+
return $1;
512+
})();`,
513+
);
514+
500515
// TODO: Remove once https://github.com/vercel/next.js/issues/58265 is fixed.
501516
// This resolves a critical issue in Next.js 14.0.2 that breaks edge runtime rendering due to the assumption
502517
// that the the passed internal request is of type `NodeNextRequest` and never `WebNextRequest`.

packages/next-on-pages/templates/_worker.js/utils/fetch.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ export function patchFetch(): void {
1717
function applyPatch() {
1818
const originalFetch = globalThis.fetch;
1919

20-
globalThis.fetch = async (...args) => {
21-
const request = new Request(...args);
20+
globalThis.fetch = async (input, requestInit) => {
21+
// @ts-expect-error - `cache` exists on `RequestInit` in Node.js, but not workerd.
22+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
23+
const { cache, ...init } = requestInit ?? {};
24+
const request = new Request(input, init);
2225

2326
let response = await handleInlineAssetRequest(request);
2427
if (response) return response;

0 commit comments

Comments
 (0)