Skip to content

Commit

Permalink
Lift resetFetch into router-server
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Aug 6, 2024
1 parent b5d69c4 commit 326785d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
18 changes: 14 additions & 4 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -140,6 +149,7 @@ export async function initialize(opts: {
turbo: !!process.env.TURBOPACK,
port: opts.port,
onCleanup: opts.onCleanup,
resetFetch,
})
)

Expand Down Expand Up @@ -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

Expand Down
13 changes: 3 additions & 10 deletions packages/next/src/server/lib/router-utils/setup-dev-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -105,6 +104,7 @@ export type SetupOpts = {
nextConfig: NextConfigComplete
port: number
onCleanup: (listener: () => Promise<void>) => void
resetFetch: () => void
}

export type ServerFields = {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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, {
Expand All @@ -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()
Expand Down

0 comments on commit 326785d

Please sign in to comment.