Skip to content

Commit

Permalink
override request in polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 24, 2022
1 parent 43f401e commit 89a9de5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
16 changes: 1 addition & 15 deletions packages/next/server/app-render.tsx
Expand Up @@ -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
Expand All @@ -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 =
Expand All @@ -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' ||
Expand Down
13 changes: 12 additions & 1 deletion packages/next/server/node-polyfill-fetch.js
Expand Up @@ -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()
Expand Down Expand Up @@ -44,7 +55,7 @@ if (!global.fetch) {
},
Request: {
get() {
return getFetchImpl().Request
return getRequestImpl()
},
},
Response: {
Expand Down
2 changes: 2 additions & 0 deletions packages/next/server/web/sandbox/context.ts
Expand Up @@ -265,13 +265,15 @@ 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
? input.url
: String(input)
validateURL(url)
super(url, init)
this.next = init?.next
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/next/types/global.d.ts
Expand Up @@ -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
}

0 comments on commit 89a9de5

Please sign in to comment.