Commit 26084997 authored by Luke Sandberg's avatar Luke Sandberg

fix: wait for webpack HMR to complete before refreshing server components

During HMR, the dev server sends BUILT (triggering async module.hot.apply())
before SERVER_COMPONENT_CHANGES (triggering hmrRefresh()). Previously,
hmrRefresh() fired immediately without waiting for the webpack update to
finish, creating a race where __webpack_require__ could be called for modules
that were temporarily absent from __webpack_modules__ during the swap.

This was previously masked because the resulting TypeError was caught by
React's error boundary, but webpack 5.104+ added an explicit module existence
check that turns this into a hard "Cannot find module" error.

Fix by awaiting the pendingHotUpdateWebpack promise before calling
hmrRefresh(), ensuring all webpack modules are fully in place before the RSC
response tries to reference them.
parent 4b217d39
...@@ -419,17 +419,25 @@ export function processMessage( ...@@ -419,17 +419,25 @@ export function processMessage(
return window.location.reload() return window.location.reload()
} }
startTransition(() => { // Wait for any pending webpack HMR to finish before refreshing server
publicAppRouterInstance.hmrRefresh() // components. The BUILT message (which triggers module.hot.apply()) is
dispatcher.onRefresh() // sent before SERVER_COMPONENT_CHANGES, so there may be a pending
}) // webpack update in progress. If we call hmrRefresh() before modules
// are fully applied, __webpack_require__ can fail for modules that are
// temporarily absent from __webpack_modules__ during the swap.
pendingHotUpdateWebpack.then(() => {
startTransition(() => {
publicAppRouterInstance.hmrRefresh()
dispatcher.onRefresh()
})
if (process.env.__NEXT_TEST_MODE) { if (process.env.__NEXT_TEST_MODE) {
if (self.__NEXT_HMR_CB) { if (self.__NEXT_HMR_CB) {
self.__NEXT_HMR_CB() self.__NEXT_HMR_CB()
self.__NEXT_HMR_CB = null self.__NEXT_HMR_CB = null
}
} }
} })
return return
} }
......
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