-
Notifications
You must be signed in to change notification settings - Fork 28.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(HMR clients): Clean up tryApplyUpdates, reduce differences between app/pages versions #77219
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,23 +81,6 @@ export function waitForWebpackRuntimeHotUpdate() { | |
return pendingHotUpdateWebpack | ||
} | ||
|
||
function handleSuccessfulHotUpdateWebpack( | ||
dispatcher: Dispatcher, | ||
sendMessage: (message: string) => void, | ||
updatedModules: ReadonlyArray<string> | ||
) { | ||
resolvePendingHotUpdateWebpack() | ||
dispatcher.onBuildOk() | ||
reportHmrLatency( | ||
sendMessage, | ||
updatedModules, | ||
webpackStartMsSinceEpoch!, | ||
Date.now() | ||
) | ||
|
||
dispatcher.onRefresh() | ||
} | ||
|
||
// There is a newer version of the code available. | ||
function handleAvailableHash(hash: string) { | ||
// Update last known compilation hash. | ||
|
@@ -160,10 +143,8 @@ function performFullReload(err: any, sendMessage: any) { | |
} | ||
|
||
// Attempt to update code on the fly, fall back to a hard reload. | ||
function tryApplyUpdates( | ||
onBeforeUpdate: (hasUpdates: boolean) => void, | ||
onHotUpdateSuccess: (updatedModules: string[]) => void, | ||
sendMessage: any, | ||
function tryApplyUpdatesWebpack( | ||
sendMessage: (message: string) => void, | ||
dispatcher: Dispatcher | ||
) { | ||
if (!isUpdateAvailable() || !canApplyUpdates()) { | ||
|
@@ -173,8 +154,11 @@ function tryApplyUpdates( | |
return | ||
} | ||
|
||
function handleApplyUpdates(err: any, updatedModules: string[] | null) { | ||
if (err || RuntimeErrorHandler.hadRuntimeError || !updatedModules) { | ||
function handleApplyUpdates( | ||
err: any, | ||
updatedModules: (string | number)[] | null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type of https://github.com/webpack/webpack/blob/3aa6b6bc3a6/module.d.ts#L139-L142 |
||
) { | ||
if (err || RuntimeErrorHandler.hadRuntimeError || updatedModules == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
if (err) { | ||
console.warn(REACT_REFRESH_FULL_RELOAD) | ||
} else if (RuntimeErrorHandler.hadRuntimeError) { | ||
|
@@ -184,50 +168,50 @@ function tryApplyUpdates( | |
return | ||
} | ||
|
||
const hasUpdates = Boolean(updatedModules.length) | ||
if (typeof onHotUpdateSuccess === 'function') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This branch was always taken, based on the type signature. |
||
// Maybe we want to do something. | ||
onHotUpdateSuccess(updatedModules) | ||
} | ||
dispatcher.onBuildOk() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the start of the inlined version of onHotUpdateSuccess |
||
|
||
if (isUpdateAvailable()) { | ||
// While we were updating, there was a new update! Do it again. | ||
tryApplyUpdates( | ||
hasUpdates ? () => {} : onBeforeUpdate, | ||
hasUpdates ? () => dispatcher.onBuildOk() : onHotUpdateSuccess, | ||
sendMessage, | ||
dispatcher | ||
) | ||
} else { | ||
dispatcher.onBuildOk() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were calling this twice before. That became obvious when inlining |
||
if (process.env.__NEXT_TEST_MODE) { | ||
afterApplyUpdates(() => { | ||
if (self.__NEXT_HMR_CB) { | ||
self.__NEXT_HMR_CB() | ||
self.__NEXT_HMR_CB = null | ||
} | ||
}) | ||
} | ||
tryApplyUpdatesWebpack(sendMessage, dispatcher) | ||
Comment on lines
174
to
+175
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slight difference here... Old version: Run New version: Don't call
|
||
return | ||
} | ||
|
||
dispatcher.onRefresh() | ||
resolvePendingHotUpdateWebpack() | ||
reportHmrLatency( | ||
sendMessage, | ||
updatedModules, | ||
webpackStartMsSinceEpoch!, | ||
Date.now() | ||
) | ||
|
||
if (process.env.__NEXT_TEST_MODE) { | ||
afterApplyUpdates(() => { | ||
if (self.__NEXT_HMR_CB) { | ||
self.__NEXT_HMR_CB() | ||
self.__NEXT_HMR_CB = null | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// https://webpack.js.org/api/hot-module-replacement/#check | ||
module.hot | ||
.check(/* autoApply */ false) | ||
.then((updatedModules: any[] | null) => { | ||
if (!updatedModules) { | ||
.then((updatedModules: (string | number)[] | null) => { | ||
if (updatedModules == null) { | ||
return null | ||
} | ||
|
||
if (typeof onBeforeUpdate === 'function') { | ||
const hasUpdates = Boolean(updatedModules.length) | ||
onBeforeUpdate(hasUpdates) | ||
} | ||
// We should always handle an update, even if updatedModules is empty (but | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if there are actually cases where |
||
// non-null) for any reason. That's what webpack would normally do: | ||
// https://github.com/webpack/webpack/blob/3aa6b6bc3a64/lib/hmr/HotModuleReplacement.runtime.js#L296-L298 | ||
dispatcher.onBeforeRefresh() | ||
// https://webpack.js.org/api/hot-module-replacement/#apply | ||
return module.hot.apply() | ||
}) | ||
.then( | ||
(updatedModules: any[] | null) => { | ||
(updatedModules: (string | number)[] | null) => { | ||
handleApplyUpdates(null, updatedModules) | ||
}, | ||
(err: any) => { | ||
|
@@ -236,7 +220,7 @@ function tryApplyUpdates( | |
) | ||
} | ||
|
||
/** Handles messages from the sevrer for the App Router. */ | ||
/** Handles messages from the server for the App Router. */ | ||
function processMessage( | ||
obj: HMR_ACTION_TYPES, | ||
sendMessage: (message: string) => void, | ||
|
@@ -288,24 +272,7 @@ function processMessage( | |
} | ||
dispatcher.onBuildOk() | ||
} else { | ||
tryApplyUpdates( | ||
function onBeforeHotUpdate(hasUpdates: boolean) { | ||
if (hasUpdates) { | ||
dispatcher.onBeforeRefresh() | ||
} | ||
}, | ||
function onSuccessfulHotUpdate(webpackUpdatedModules: string[]) { | ||
// Only dismiss it when we're sure it's a hot update. | ||
// Otherwise it would flicker right before the reload. | ||
handleSuccessfulHotUpdateWebpack( | ||
dispatcher, | ||
sendMessage, | ||
webpackUpdatedModules | ||
) | ||
}, | ||
sendMessage, | ||
dispatcher | ||
) | ||
tryApplyUpdatesWebpack(sendMessage, dispatcher) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I inlined this, it only had one callsite.