Commit 9ce2a539 authored by Will Binns-Smith's avatar Will Binns-Smith

Revert multicast HMR registry to a direct globalThis assignment

With a single shared NodeJsChunkingContext there is exactly one server
runtime file, so the __turbopack_server_hmr_handlers__ Map and its
chunkPrefix-based routing dispatcher are no longer needed.

Replace with a direct assignment:
  globalThis.__turbopack_server_hmr_apply__ = __turbopack_server_hmr_apply__

Remove the corresponding Map reset from hot-reloader-turbopack.ts and
the type declaration from nodejs-globals.d.ts.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent d530b19e
...@@ -1846,11 +1846,6 @@ export async function createHotReloaderTurbopack( ...@@ -1846,11 +1846,6 @@ export async function createHotReloaderTurbopack(
__next__clear_chunk_cache__() __next__clear_chunk_cache__()
} }
// Reset the server HMR handler registry. All server runtime chunks are
// cleared from require.cache above; when they're next required they'll
// re-register into this Map and reinstall the routing dispatcher.
;(globalThis as any).__turbopack_server_hmr_handlers__ = new Map()
// Clear all edge contexts // Clear all edge contexts
await clearAllModuleContexts() await clearAllModuleContexts()
......
...@@ -33,65 +33,8 @@ function __turbopack_server_hmr_apply__(update: NodeJsHmrPayload): boolean { ...@@ -33,65 +33,8 @@ function __turbopack_server_hmr_apply__(update: NodeJsHmrPayload): boolean {
} }
} }
// Turbopack produces one server runtime per chunking context (e.g. // All Node.js server endpoints share a single chunking context and therefore a
// server/chunks/ssr/ for pages, server/chunks/ for route handlers), each with // single runtime file — install the handler directly on globalThis. When the
// its own moduleFactories. We keep a globalThis Map from __filename to handler // runtime is re-evaluated after require.cache eviction, the assignment simply
// so updates are routed only to runtimes whose chunkPrefix matches the update's // reinstalls the fresh handler.
// chunk paths, skipping unnecessary eval() calls. Map.set() naturally replaces globalThis.__turbopack_server_hmr_apply__ = __turbopack_server_hmr_apply__
// stale entries when a runtime file is re-evaluated after require.cache eviction.
type HmrHandlerEntry = {
handler: (update: NodeJsHmrPayload) => boolean
/** Output directory relative to RUNTIME_ROOT, e.g. "server/chunks/ssr". */
chunkPrefix: string
}
const handlers: Map<string, HmrHandlerEntry> =
globalThis.__turbopack_server_hmr_handlers__ ?? new Map()
const chunkPrefix = path.relative(RUNTIME_ROOT, path.dirname(__filename))
if (handlers.size === 0) {
// First registration in this generation: install the routing dispatcher.
globalThis.__turbopack_server_hmr_apply__ = (
update: NodeJsHmrPayload
): boolean => {
const registry: Map<string, HmrHandlerEntry> =
globalThis.__turbopack_server_hmr_handlers__ ?? new Map()
const updateChunkPaths = Object.keys(update.instruction?.chunks ?? {})
const toCall: HmrHandlerEntry[] = []
if (updateChunkPaths.length === 0) {
for (const entry of registry.values()) toCall.push(entry)
} else {
const seen = new Set<string>()
for (const chunkPath of updateChunkPaths) {
const dir = path.dirname(chunkPath)
for (const [key, entry] of registry) {
if (dir === entry.chunkPrefix && !seen.has(key)) {
seen.add(key)
toCall.push(entry)
}
}
}
}
let applied = false
for (const { handler } of toCall) {
try {
if (handler(update)) applied = true
} catch (err) {
console.error('[Server HMR] Handler error:', err)
}
}
return applied
}
}
globalThis.__turbopack_server_hmr_handlers__ = handlers
handlers.set(__filename, {
handler: __turbopack_server_hmr_apply__,
chunkPrefix,
})
...@@ -9,18 +9,6 @@ declare global { ...@@ -9,18 +9,6 @@ declare global {
var __turbopack_module_factories__: ModuleFactories var __turbopack_module_factories__: ModuleFactories
var __turbopack_module_cache__: Record<ModuleId, any> var __turbopack_module_cache__: Record<ModuleId, any>
var __turbopack_runtime_modules__: Set<ModuleId> var __turbopack_runtime_modules__: Set<ModuleId>
/**
* Shared registry of per-runtime server HMR handler entries, keyed by the
* runtime file's __filename. Map.set() naturally replaces stale entries when
* a runtime file is re-evaluated. Reset to undefined by the hot-reloader on
* full cache reset.
*/
// NodeJsHmrPayload is only in scope inside the concatenated runtime chunk, not
// in this .d.ts file, so the handler parameter must use any here.
var __turbopack_server_hmr_handlers__:
| Map<string, { handler: (update: any) => boolean; chunkPrefix: string }>
| undefined
} }
export {} export {}
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