Skip to content

Commit

Permalink
Do not reset patched fetch during HMR
Browse files Browse the repository at this point in the history
This should not be needed, now that React is not patching `fetch`
anymore (see facebook/react#28896). It was
previously done to fix a memory leak (see #43859).

This fixes a race condition where the original `fetch` was stored before
experimental test mode patched `fetch`, thus undoing the test mode
`fetch` patch when resetting `fetch` during HMR.
  • Loading branch information
unstubbable committed Aug 6, 2024
1 parent b5d69c4 commit 238ee67
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 20 deletions.
5 changes: 1 addition & 4 deletions packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ const isTestMode = !!(
export async function createHotReloaderTurbopack(
opts: SetupOpts,
serverFields: ServerFields,
distDir: string,
resetFetch: () => void
distDir: string
): Promise<NextJsHotReloaderInterface> {
const buildId = 'development'
const { nextConfig, dir } = opts
Expand Down Expand Up @@ -238,8 +237,6 @@ export async function createHotReloaderTurbopack(
}
}

resetFetch()

const hasAppPaths = writtenEndpoint.serverPaths.some(({ path: p }) =>
p.startsWith('server/app')
)
Expand Down
5 changes: 0 additions & 5 deletions packages/next/src/server/dev/hot-reloader-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
private pagesMapping: { [key: string]: string } = {}
private appDir?: string
private telemetry: Telemetry
private resetFetch: () => void
private versionInfo: VersionInfo = {
staleness: 'unknown',
installed: '0.0.0',
Expand All @@ -275,7 +274,6 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
rewrites,
appDir,
telemetry,
resetFetch,
}: {
config: NextConfigComplete
pagesDir?: string
Expand All @@ -286,7 +284,6 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
rewrites: CustomRoutes['rewrites']
appDir?: string
telemetry: Telemetry
resetFetch: () => void
}
) {
this.hasAmpEntrypoints = false
Expand All @@ -304,7 +301,6 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
this.edgeServerStats = null
this.serverPrevDocumentHash = null
this.telemetry = telemetry
this.resetFetch = resetFetch

this.config = config
this.previewProps = previewProps
Expand Down Expand Up @@ -1369,7 +1365,6 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
changedCSSImportPages.size ||
reloadAfterInvalidation
) {
this.resetFetch()
this.refreshServerComponents()
}

Expand Down
12 changes: 1 addition & 11 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 Down Expand Up @@ -123,7 +122,6 @@ export type ServerFields = {
typeof import('./filesystem').buildCustomRoute
>[]
setAppIsrStatus?: (key: string, value: false | number | null) => void
resetFetch?: () => void
}

async function verifyTypeScript(opts: SetupOpts) {
Expand Down Expand Up @@ -183,15 +181,8 @@ 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)
? await createHotReloaderTurbopack(opts, serverFields, distDir)
: new HotReloaderWebpack(opts.dir, {
appDir,
pagesDir,
Expand All @@ -202,7 +193,6 @@ async function startWatcher(opts: SetupOpts) {
telemetry: opts.telemetry,
rewrites: opts.fsChecker.rewrites,
previewProps: opts.fsChecker.prerenderManifest.preview,
resetFetch: resetFetch,
})

await hotReloader.start()
Expand Down

0 comments on commit 238ee67

Please sign in to comment.