Unverified Commit 9e8e44e5 authored by Dima Voytenko's avatar Dima Voytenko Committed by GitHub

Dev Server: Preserve globals overwrites in the initialization hook (#60796)

The original dev server implementation records globals in the server
constructor and thus overwrites the values initialized in the
instrumentation hook. This is a major delta from the `next start` where
the instrumentation values are preserved.

Fixes: https://github.com/vercel/next.js/issues/47596
Fixes: https://github.com/vercel/next.js/issues/53407
parent fd59a007
...@@ -102,7 +102,7 @@ export default class DevServer extends Server { ...@@ -102,7 +102,7 @@ export default class DevServer extends Server {
private actualMiddlewareFile?: string private actualMiddlewareFile?: string
private actualInstrumentationHookFile?: string private actualInstrumentationHookFile?: string
private middleware?: MiddlewareRoutingItem private middleware?: MiddlewareRoutingItem
private originalFetch: typeof fetch private originalFetch?: typeof fetch
private readonly bundlerService: DevBundlerService private readonly bundlerService: DevBundlerService
private staticPathsCache: LRUCache< private staticPathsCache: LRUCache<
string, string,
...@@ -152,7 +152,7 @@ export default class DevServer extends Server { ...@@ -152,7 +152,7 @@ export default class DevServer extends Server {
this.bundlerService = options.bundlerService this.bundlerService = options.bundlerService
this.startServerSpan = this.startServerSpan =
options.startServerSpan ?? trace('start-next-dev-server') options.startServerSpan ?? trace('start-next-dev-server')
this.originalFetch = global.fetch this.storeGlobals()
this.renderOpts.dev = true this.renderOpts.dev = true
this.renderOpts.appDirDevErrorLogger = (err: any) => this.renderOpts.appDirDevErrorLogger = (err: any) =>
this.logErrorWithOriginalStack(err, 'app-dir') this.logErrorWithOriginalStack(err, 'app-dir')
...@@ -281,6 +281,9 @@ export default class DevServer extends Server { ...@@ -281,6 +281,9 @@ export default class DevServer extends Server {
.traceAsyncFn(() => this.runInstrumentationHookIfAvailable()) .traceAsyncFn(() => this.runInstrumentationHookIfAvailable())
await this.matchers.reload() await this.matchers.reload()
// Store globals again to preserve changes made by the instrumentation hook.
this.storeGlobals()
this.ready?.resolve() this.ready?.resolve()
this.ready = undefined this.ready = undefined
...@@ -754,8 +757,12 @@ export default class DevServer extends Server { ...@@ -754,8 +757,12 @@ export default class DevServer extends Server {
return nextInvoke as NonNullable<typeof result> return nextInvoke as NonNullable<typeof result>
} }
private storeGlobals(): void {
this.originalFetch = global.fetch
}
private restorePatchedGlobals(): void { private restorePatchedGlobals(): void {
global.fetch = this.originalFetch global.fetch = this.originalFetch ?? global.fetch
} }
protected async ensurePage(opts: { protected async ensurePage(opts: {
......
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