From 89a9de57d7165eaa3e75e399eb0cb46f19b07036 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sun, 23 Oct 2022 19:03:25 -0700 Subject: [PATCH] override request in polyfill --- packages/next/server/app-render.tsx | 16 +--------------- packages/next/server/node-polyfill-fetch.js | 13 ++++++++++++- packages/next/server/web/sandbox/context.ts | 2 ++ packages/next/types/global.d.ts | 6 +++++- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/packages/next/server/app-render.tsx b/packages/next/server/app-render.tsx index 4ded40e8753d5..c7071093ff5aa 100644 --- a/packages/next/server/app-render.tsx +++ b/packages/next/server/app-render.tsx @@ -195,14 +195,6 @@ function createErrorHandler( } } -class NextFetchRequestImpl extends globalThis.Request { - next?: NextFetchRequestConfig | undefined - constructor(input: RequestInfo | URL, init?: RequestInit | undefined) { - super(input, init) - this.next = init?.next - } -} - let isFetchPatched = false // we patch fetch to collect cache information used for @@ -216,12 +208,6 @@ function patchFetch(ComponentMod: any) { const staticGenerationAsyncStorage = ComponentMod.staticGenerationAsyncStorage - Object.defineProperty(globalThis, 'Request', { - get() { - return NextFetchRequestImpl - }, - }) - const originFetch = globalThis.fetch globalThis.fetch = async (input, init) => { const staticGenerationStore = @@ -244,7 +230,7 @@ function patchFetch(ComponentMod: any) { } const hasNextConfig = 'next' in init - const next = (hasNextConfig && init.next) || {} + const next = init.next || {} if ( typeof next.revalidate === 'number' && (typeof fetchRevalidate === 'undefined' || diff --git a/packages/next/server/node-polyfill-fetch.js b/packages/next/server/node-polyfill-fetch.js index 745c37d169870..9b2d359c107e3 100644 --- a/packages/next/server/node-polyfill-fetch.js +++ b/packages/next/server/node-polyfill-fetch.js @@ -6,6 +6,17 @@ if (!global.fetch) { ? require('next/dist/compiled/undici') : require('next/dist/compiled/node-fetch') } + + function getRequestImpl() { + const OriginRequest = getFetchImpl().Request + return class Request extends OriginRequest { + constructor(input, init) { + super(input, init) + this.next = init?.next + } + } + } + // Due to limitation of global configuration, we have to do this resolution at runtime global.fetch = (...args) => { const fetchImpl = getFetchImpl() @@ -44,7 +55,7 @@ if (!global.fetch) { }, Request: { get() { - return getFetchImpl().Request + return getRequestImpl() }, }, Response: { diff --git a/packages/next/server/web/sandbox/context.ts b/packages/next/server/web/sandbox/context.ts index 943b8e2076ff7..da0d334f979c5 100644 --- a/packages/next/server/web/sandbox/context.ts +++ b/packages/next/server/web/sandbox/context.ts @@ -265,6 +265,7 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), const __Request = context.Request context.Request = class extends __Request { + next?: NextFetchRequestConfig | undefined constructor(input: URL | RequestInfo, init?: RequestInit | undefined) { const url = typeof input !== 'string' && 'url' in input @@ -272,6 +273,7 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), : String(input) validateURL(url) super(url, init) + this.next = init?.next } } diff --git a/packages/next/types/global.d.ts b/packages/next/types/global.d.ts index 706c663991938..9e30d5a934a86 100644 --- a/packages/next/types/global.d.ts +++ b/packages/next/types/global.d.ts @@ -42,6 +42,10 @@ interface Window { __NEXT_HMR_CB?: null | ((message?: string) => void) } +interface NextFetchRequestConfig { + revalidate?: number +} + interface RequestInit { - next?: { revalidate?: number } | undefined + next?: NextFetchRequestConfig | undefined }