@@ -81,23 +81,6 @@ export function waitForWebpackRuntimeHotUpdate() {
81
81
return pendingHotUpdateWebpack
82
82
}
83
83
84
- function handleSuccessfulHotUpdateWebpack (
85
- dispatcher : Dispatcher ,
86
- sendMessage : ( message : string ) => void ,
87
- updatedModules : ReadonlyArray < string >
88
- ) {
89
- resolvePendingHotUpdateWebpack ( )
90
- dispatcher . onBuildOk ( )
91
- reportHmrLatency (
92
- sendMessage ,
93
- updatedModules ,
94
- webpackStartMsSinceEpoch ! ,
95
- Date . now ( )
96
- )
97
-
98
- dispatcher . onRefresh ( )
99
- }
100
-
101
84
// There is a newer version of the code available.
102
85
function handleAvailableHash ( hash : string ) {
103
86
// Update last known compilation hash.
@@ -160,10 +143,8 @@ function performFullReload(err: any, sendMessage: any) {
160
143
}
161
144
162
145
// Attempt to update code on the fly, fall back to a hard reload.
163
- function tryApplyUpdates (
164
- onBeforeUpdate : ( hasUpdates : boolean ) => void ,
165
- onHotUpdateSuccess : ( updatedModules : string [ ] ) => void ,
166
- sendMessage : any ,
146
+ function tryApplyUpdatesWebpack (
147
+ sendMessage : ( message : string ) => void ,
167
148
dispatcher : Dispatcher
168
149
) {
169
150
if ( ! isUpdateAvailable ( ) || ! canApplyUpdates ( ) ) {
@@ -173,8 +154,11 @@ function tryApplyUpdates(
173
154
return
174
155
}
175
156
176
- function handleApplyUpdates ( err : any , updatedModules : string [ ] | null ) {
177
- if ( err || RuntimeErrorHandler . hadRuntimeError || ! updatedModules ) {
157
+ function handleApplyUpdates (
158
+ err : any ,
159
+ updatedModules : ( string | number ) [ ] | null
160
+ ) {
161
+ if ( err || RuntimeErrorHandler . hadRuntimeError || updatedModules == null ) {
178
162
if ( err ) {
179
163
console . warn ( REACT_REFRESH_FULL_RELOAD )
180
164
} else if ( RuntimeErrorHandler . hadRuntimeError ) {
@@ -184,50 +168,50 @@ function tryApplyUpdates(
184
168
return
185
169
}
186
170
187
- const hasUpdates = Boolean ( updatedModules . length )
188
- if ( typeof onHotUpdateSuccess === 'function' ) {
189
- // Maybe we want to do something.
190
- onHotUpdateSuccess ( updatedModules )
191
- }
171
+ dispatcher . onBuildOk ( )
192
172
193
173
if ( isUpdateAvailable ( ) ) {
194
174
// While we were updating, there was a new update! Do it again.
195
- tryApplyUpdates (
196
- hasUpdates ? ( ) => { } : onBeforeUpdate ,
197
- hasUpdates ? ( ) => dispatcher . onBuildOk ( ) : onHotUpdateSuccess ,
198
- sendMessage ,
199
- dispatcher
200
- )
201
- } else {
202
- dispatcher . onBuildOk ( )
203
- if ( process . env . __NEXT_TEST_MODE ) {
204
- afterApplyUpdates ( ( ) => {
205
- if ( self . __NEXT_HMR_CB ) {
206
- self . __NEXT_HMR_CB ( )
207
- self . __NEXT_HMR_CB = null
208
- }
209
- } )
210
- }
175
+ tryApplyUpdatesWebpack ( sendMessage , dispatcher )
176
+ return
177
+ }
178
+
179
+ dispatcher . onRefresh ( )
180
+ resolvePendingHotUpdateWebpack ( )
181
+ reportHmrLatency (
182
+ sendMessage ,
183
+ updatedModules ,
184
+ webpackStartMsSinceEpoch ! ,
185
+ Date . now ( )
186
+ )
187
+
188
+ if ( process . env . __NEXT_TEST_MODE ) {
189
+ afterApplyUpdates ( ( ) => {
190
+ if ( self . __NEXT_HMR_CB ) {
191
+ self . __NEXT_HMR_CB ( )
192
+ self . __NEXT_HMR_CB = null
193
+ }
194
+ } )
211
195
}
212
196
}
213
197
214
198
// https://webpack.js.org/api/hot-module-replacement/#check
215
199
module . hot
216
200
. check ( /* autoApply */ false )
217
- . then ( ( updatedModules : any [ ] | null ) => {
218
- if ( ! updatedModules ) {
201
+ . then ( ( updatedModules : ( string | number ) [ ] | null ) => {
202
+ if ( updatedModules == null ) {
219
203
return null
220
204
}
221
205
222
- if ( typeof onBeforeUpdate === 'function' ) {
223
- const hasUpdates = Boolean ( updatedModules . length )
224
- onBeforeUpdate ( hasUpdates )
225
- }
206
+ // We should always handle an update, even if updatedModules is empty (but
207
+ // non-null) for any reason. That's what webpack would normally do:
208
+ // https://github.com/webpack/webpack/blob/3aa6b6bc3a64/lib/hmr/HotModuleReplacement.runtime.js#L296-L298
209
+ dispatcher . onBeforeRefresh ( )
226
210
// https://webpack.js.org/api/hot-module-replacement/#apply
227
211
return module . hot . apply ( )
228
212
} )
229
213
. then (
230
- ( updatedModules : any [ ] | null ) => {
214
+ ( updatedModules : ( string | number ) [ ] | null ) => {
231
215
handleApplyUpdates ( null , updatedModules )
232
216
} ,
233
217
( err : any ) => {
@@ -236,7 +220,7 @@ function tryApplyUpdates(
236
220
)
237
221
}
238
222
239
- /** Handles messages from the sevrer for the App Router. */
223
+ /** Handles messages from the server for the App Router. */
240
224
function processMessage (
241
225
obj : HMR_ACTION_TYPES ,
242
226
sendMessage : ( message : string ) => void ,
@@ -288,24 +272,7 @@ function processMessage(
288
272
}
289
273
dispatcher . onBuildOk ( )
290
274
} else {
291
- tryApplyUpdates (
292
- function onBeforeHotUpdate ( hasUpdates : boolean ) {
293
- if ( hasUpdates ) {
294
- dispatcher . onBeforeRefresh ( )
295
- }
296
- } ,
297
- function onSuccessfulHotUpdate ( webpackUpdatedModules : string [ ] ) {
298
- // Only dismiss it when we're sure it's a hot update.
299
- // Otherwise it would flicker right before the reload.
300
- handleSuccessfulHotUpdateWebpack (
301
- dispatcher ,
302
- sendMessage ,
303
- webpackUpdatedModules
304
- )
305
- } ,
306
- sendMessage ,
307
- dispatcher
308
- )
275
+ tryApplyUpdatesWebpack ( sendMessage , dispatcher )
309
276
}
310
277
}
311
278
0 commit comments