Commit eeb629f2 authored by Will Binns-Smith's avatar Will Binns-Smith

Enable auto-accept for server HMR at root modules

Adds serverHmrAutoAccept flag to the Node.js dev runtime that enables
automatic acceptance of HMR updates for root modules (modules with no parents).

This allows server component updates to be applied without requiring
explicit module.hot.accept() calls in every server entrypoint:

- Root modules (no parents) are treated as self-accepting when auto-accept is enabled
- Default is enabled (true) for server-side HMR
- Modified getAffectedModuleEffects() to continue traversal instead of rejecting
- Modified computeOutdatedSelfAcceptedModules() to include auto-accepted roots

This makes server-side HMR "just work" for RSC pages since their output
is serialized anyway and re-rendering produces a fresh payload.
parent e62258be
...@@ -177,6 +177,15 @@ const queuedInvalidatedModules: Set<ModuleId> = new Set() ...@@ -177,6 +177,15 @@ const queuedInvalidatedModules: Set<ModuleId> = new Set()
*/ */
const runtimeModules: Set<ModuleId> = new Set() const runtimeModules: Set<ModuleId> = new Set()
/**
* When true, modules that bubble up to the root without being accepted
* will be auto-accepted. This is useful for server-side HMR where all
* server components should accept updates by default.
*
* This can be enabled/disabled via globalThis.__turbopack_server_hmr_auto_accept__
*/
let serverHmrAutoAccept: boolean = true
// ============================================================================ // ============================================================================
// Utility Functions // Utility Functions
// ============================================================================ // ============================================================================
...@@ -655,8 +664,13 @@ function getAffectedModuleEffects(moduleId: ModuleId): AffectedModuleEffect { ...@@ -655,8 +664,13 @@ function getAffectedModuleEffects(moduleId: ModuleId): AffectedModuleEffect {
}) })
} }
// If no parents and not a runtime module, it's unaccepted // If no parents and not a runtime module, check if we should auto-accept
if (module.parents.length === 0) { if (module.parents.length === 0) {
if (serverHmrAutoAccept) {
// In server HMR mode with auto-accept enabled, treat root modules as self-accepting
// This allows server component updates to be applied without explicit module.hot.accept()
continue
}
return { type: 'unaccepted', dependencyChain: newDependencyChain } return { type: 'unaccepted', dependencyChain: newDependencyChain }
} }
} }
...@@ -735,11 +749,19 @@ function computeOutdatedSelfAcceptedModules( ...@@ -735,11 +749,19 @@ function computeOutdatedSelfAcceptedModules(
for (const moduleId of outdatedModules) { for (const moduleId of outdatedModules) {
const module = devModuleCache[moduleId] const module = devModuleCache[moduleId]
const hotState = moduleHotState.get(module)! if (!module) continue
if (module && hotState.selfAccepted && !hotState.selfInvalidated) {
const hotState = moduleHotState.get(module)
const isSelfAccepted = hotState?.selfAccepted && !hotState?.selfInvalidated
// In server HMR auto-accept mode, treat root modules (no parents) as self-accepting
const isAutoAcceptedRoot =
serverHmrAutoAccept && module.parents.length === 0
if (isSelfAccepted || isAutoAcceptedRoot) {
outdatedSelfAcceptedModules.push({ outdatedSelfAcceptedModules.push({
moduleId, moduleId,
errorHandler: hotState.selfAccepted, errorHandler: hotState?.selfAccepted || true,
}) })
} }
} }
......
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