diff --git a/packages/next/src/server/lib/router-server.ts b/packages/next/src/server/lib/router-server.ts index fc9dfd76a0678..ef46b9d1f949c 100644 --- a/packages/next/src/server/lib/router-server.ts +++ b/packages/next/src/server/lib/router-server.ts @@ -46,6 +46,7 @@ import { type AppIsrManifestAction, } from '../dev/hot-reloader-types' import { normalizedAssetPrefix } from '../../shared/lib/normalized-asset-prefix' +import { NEXT_PATCH_SYMBOL } from './patch-fetch' const debug = setupDebug('next:router-server:main') const isNextFont = (pathname: string | null) => @@ -111,6 +112,8 @@ export async function initialize(opts: { let devBundlerService: DevBundlerService | undefined + let originalFetch = globalThis.fetch + if (opts.dev) { const { Telemetry } = require('../../telemetry/storage') as typeof import('../../telemetry/storage') @@ -123,6 +126,12 @@ export async function initialize(opts: { const { setupDevBundler } = require('./router-utils/setup-dev-bundler') as typeof import('./router-utils/setup-dev-bundler') + const resetFetch = () => { + global.fetch = originalFetch + // @ts-ignore + global[NEXT_PATCH_SYMBOL] = false + } + const setupDevBundlerSpan = opts.startServerSpan ? opts.startServerSpan.traceChild('setup-dev-bundler') : trace('setup-dev-bundler') @@ -140,6 +149,7 @@ export async function initialize(opts: { turbo: !!process.env.TURBOPACK, port: opts.port, onCleanup: opts.onCleanup, + resetFetch, }) ) @@ -593,12 +603,12 @@ export async function initialize(opts: { let requestHandler: WorkerRequestHandler = requestHandlerImpl if (config.experimental.testProxy) { // Intercept fetch and other testmode apis. - const { - wrapRequestHandlerWorker, - interceptTestApis, - } = require('next/dist/experimental/testmode/server') + const { wrapRequestHandlerWorker, interceptTestApis } = + require('next/dist/experimental/testmode/server') as typeof import('next/src/experimental/testmode/server') requestHandler = wrapRequestHandlerWorker(requestHandler) interceptTestApis() + // We treat the intercepted fetch as "original" fetch that should be reset to during HMR. + originalFetch = globalThis.fetch } requestHandlers[opts.dir] = requestHandler diff --git a/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts b/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts index 09fddf90adcca..465de90fe8184 100644 --- a/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts +++ b/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts @@ -89,7 +89,6 @@ import { normalizeMetadataPageToRoute } from '../../../lib/metadata/get-metadata import { createEnvDefinitions } from '../experimental/create-env-definitions' import { JsConfigPathsPlugin } from '../../../build/webpack/plugins/jsconfig-paths-plugin' import { store as consoleStore } from '../../../build/output/store' -import { NEXT_PATCH_SYMBOL } from '../patch-fetch' export type SetupOpts = { renderServer: LazyRenderServerInstance @@ -105,6 +104,7 @@ export type SetupOpts = { nextConfig: NextConfigComplete port: number onCleanup: (listener: () => Promise) => void + resetFetch: () => void } export type ServerFields = { @@ -154,7 +154,7 @@ export async function propagateServerField( } async function startWatcher(opts: SetupOpts) { - const { nextConfig, appDir, pagesDir, dir } = opts + const { nextConfig, appDir, pagesDir, dir, resetFetch } = opts const { useFileSystemPublicRoutes } = nextConfig const usingTypeScript = await verifyTypeScript(opts) @@ -183,13 +183,6 @@ async function startWatcher(opts: SetupOpts) { logging: nextConfig.logging !== false, }) - const originalFetch = global.fetch - const resetFetch = () => { - global.fetch = originalFetch - // @ts-ignore - global[NEXT_PATCH_SYMBOL] = false - } - const hotReloader: NextJsHotReloaderInterface = opts.turbo ? await createHotReloaderTurbopack(opts, serverFields, distDir, resetFetch) : new HotReloaderWebpack(opts.dir, { @@ -202,7 +195,7 @@ async function startWatcher(opts: SetupOpts) { telemetry: opts.telemetry, rewrites: opts.fsChecker.rewrites, previewProps: opts.fsChecker.prerenderManifest.preview, - resetFetch: resetFetch, + resetFetch, }) await hotReloader.start()