Unverified Commit 5de6f46d authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Support next option for Request (#41614)

Follow up for #41505. Addding `next` option to Request options
parent 496b2eb7
...@@ -208,8 +208,8 @@ function patchFetch(ComponentMod: any) { ...@@ -208,8 +208,8 @@ function patchFetch(ComponentMod: any) {
const staticGenerationAsyncStorage = ComponentMod.staticGenerationAsyncStorage const staticGenerationAsyncStorage = ComponentMod.staticGenerationAsyncStorage
const origFetch = globalThis.fetch const originFetch = globalThis.fetch
globalThis.fetch = async (url, opts) => { globalThis.fetch = async (input, init) => {
const staticGenerationStore = const staticGenerationStore =
'getStore' in staticGenerationAsyncStorage 'getStore' in staticGenerationAsyncStorage
? staticGenerationAsyncStorage.getStore() ? staticGenerationAsyncStorage.getStore()
...@@ -219,18 +219,18 @@ function patchFetch(ComponentMod: any) { ...@@ -219,18 +219,18 @@ function patchFetch(ComponentMod: any) {
staticGenerationStore || {} staticGenerationStore || {}
if (staticGenerationStore && isStaticGeneration) { if (staticGenerationStore && isStaticGeneration) {
if (opts && typeof opts === 'object') { if (init && typeof init === 'object') {
if (opts.cache === 'no-store') { if (init.cache === 'no-store') {
staticGenerationStore.revalidate = 0 staticGenerationStore.revalidate = 0
// TODO: ensure this error isn't logged to the user // TODO: ensure this error isn't logged to the user
// seems it's slipping through currently // seems it's slipping through currently
throw new DynamicServerError( throw new DynamicServerError(
`no-store fetch ${url}${pathname ? ` ${pathname}` : ''}` `no-store fetch ${input}${pathname ? ` ${pathname}` : ''}`
) )
} }
const hasNextConfig = 'next' in opts const hasNextConfig = 'next' in init
const next = (hasNextConfig && opts.next) || {} const next = init.next || {}
if ( if (
typeof next.revalidate === 'number' && typeof next.revalidate === 'number' &&
(typeof fetchRevalidate === 'undefined' || (typeof fetchRevalidate === 'undefined' ||
...@@ -241,15 +241,15 @@ function patchFetch(ComponentMod: any) { ...@@ -241,15 +241,15 @@ function patchFetch(ComponentMod: any) {
// TODO: ensure this error isn't logged to the user // TODO: ensure this error isn't logged to the user
// seems it's slipping through currently // seems it's slipping through currently
throw new DynamicServerError( throw new DynamicServerError(
`revalidate: ${next.revalidate} fetch ${url}${ `revalidate: ${next.revalidate} fetch ${input}${
pathname ? ` ${pathname}` : '' pathname ? ` ${pathname}` : ''
}` }`
) )
} }
if (hasNextConfig) delete opts.next if (hasNextConfig) delete init.next
} }
} }
return origFetch(url, opts) return originFetch(input, init)
} }
} }
......
...@@ -6,6 +6,17 @@ if (!global.fetch) { ...@@ -6,6 +6,17 @@ if (!global.fetch) {
? require('next/dist/compiled/undici') ? require('next/dist/compiled/undici')
: require('next/dist/compiled/node-fetch') : 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 // Due to limitation of global configuration, we have to do this resolution at runtime
global.fetch = (...args) => { global.fetch = (...args) => {
const fetchImpl = getFetchImpl() const fetchImpl = getFetchImpl()
...@@ -44,7 +55,7 @@ if (!global.fetch) { ...@@ -44,7 +55,7 @@ if (!global.fetch) {
}, },
Request: { Request: {
get() { get() {
return getFetchImpl().Request return getRequestImpl()
}, },
}, },
Response: { Response: {
......
...@@ -265,6 +265,7 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), ...@@ -265,6 +265,7 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`),
const __Request = context.Request const __Request = context.Request
context.Request = class extends __Request { context.Request = class extends __Request {
next?: NextFetchRequestConfig | undefined
constructor(input: URL | RequestInfo, init?: RequestInit | undefined) { constructor(input: URL | RequestInfo, init?: RequestInit | undefined) {
const url = const url =
typeof input !== 'string' && 'url' in input typeof input !== 'string' && 'url' in input
...@@ -272,6 +273,7 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), ...@@ -272,6 +273,7 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`),
: String(input) : String(input)
validateURL(url) validateURL(url)
super(url, init) super(url, init)
this.next = init?.next
} }
} }
......
...@@ -12,6 +12,10 @@ declare namespace NodeJS { ...@@ -12,6 +12,10 @@ declare namespace NodeJS {
interface ProcessEnv { interface ProcessEnv {
readonly NODE_ENV: 'development' | 'production' | 'test' readonly NODE_ENV: 'development' | 'production' | 'test'
} }
interface RequestInit extends globalThis.RequestInit {
next?: NextFetchRequestConfig | undefined
}
} }
declare module '*.module.css' { declare module '*.module.css' {
...@@ -34,11 +38,10 @@ interface Window { ...@@ -34,11 +38,10 @@ interface Window {
__NEXT_HMR_CB?: null | ((message?: string) => void) __NEXT_HMR_CB?: null | ((message?: string) => void)
} }
type NextFetchRequestConfig = { interface NextFetchRequestConfig {
revalidate?: number revalidate?: number
} }
declare function fetch( interface RequestInit {
url: RequestInfo, next?: NextFetchRequestConfig | undefined
opts: RequestInit & { next?: NextFetchRequestConfig } }
): Promise<Response>
// Typing test
// eslint-disable-next-line
function noop() {
fetch('/button', { next: { revalidate: 0 } })
const request = new Request('/button', { next: { revalidate: 0 } })
fetch(request)
}
export default function page() {
return 'typing'
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment