Unverified Commit 013844c0 authored by Colin's avatar Colin Committed by GitHub

Fix "apply() is only allowed in ready status (state: idle)" HMR errors (#43242)

This is a follow-up to https://github.com/vercel/next.js/pull/43145 that
fixes [this
issue](https://github.com/vercel/next.js/pull/43145#issuecomment-1323782302):

```
warn  - Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/basic-features/fast-refresh#how-it-works
Error: apply() is only allowed in ready status (state: idle)
    at https://app.airplane.so:5000/_next/static/chunks/webpack.js?ts=1669127927262:615:21
```

## Bug

- [X] Related issues linked using `fixes #number` - n/a
- [X] Integration tests added - n/a
- [X] Errors have a helpful link attached, see `contributing.md` - n/a
parent 2085dac9
...@@ -144,7 +144,7 @@ function tryApplyUpdates( ...@@ -144,7 +144,7 @@ function tryApplyUpdates(
return return
} }
const hasUpdates = Boolean(updatedModules?.length) const hasUpdates = Boolean(updatedModules.length)
if (typeof onHotUpdateSuccess === 'function') { if (typeof onHotUpdateSuccess === 'function') {
// Maybe we want to do something. // Maybe we want to do something.
onHotUpdateSuccess(hasUpdates) onHotUpdateSuccess(hasUpdates)
...@@ -176,8 +176,12 @@ function tryApplyUpdates( ...@@ -176,8 +176,12 @@ function tryApplyUpdates(
module.hot module.hot
.check(/* autoApply */ false) .check(/* autoApply */ false)
.then((updatedModules: any[] | null) => { .then((updatedModules: any[] | null) => {
const hasUpdates = Boolean(updatedModules?.length) if (!updatedModules) {
return null
}
if (typeof onBeforeUpdate === 'function') { if (typeof onBeforeUpdate === 'function') {
const hasUpdates = Boolean(updatedModules.length)
onBeforeUpdate(hasUpdates) onBeforeUpdate(hasUpdates)
} }
// https://webpack.js.org/api/hot-module-replacement/#apply // https://webpack.js.org/api/hot-module-replacement/#apply
...@@ -185,7 +189,7 @@ function tryApplyUpdates( ...@@ -185,7 +189,7 @@ function tryApplyUpdates(
return module.hot.apply() return module.hot.apply()
}) })
.then( .then(
(updatedModules: any) => { (updatedModules: any[] | null) => {
handleApplyUpdates(null, updatedModules) handleApplyUpdates(null, updatedModules)
}, },
(err: any) => { (err: any) => {
......
...@@ -337,7 +337,7 @@ function tryApplyUpdates(onBeforeHotUpdate, onHotUpdateSuccess) { ...@@ -337,7 +337,7 @@ function tryApplyUpdates(onBeforeHotUpdate, onHotUpdateSuccess) {
return return
} }
const hasUpdates = Boolean(updatedModules?.length) const hasUpdates = Boolean(updatedModules.length)
if (typeof onHotUpdateSuccess === 'function') { if (typeof onHotUpdateSuccess === 'function') {
// Maybe we want to do something. // Maybe we want to do something.
onHotUpdateSuccess(hasUpdates) onHotUpdateSuccess(hasUpdates)
...@@ -367,8 +367,12 @@ function tryApplyUpdates(onBeforeHotUpdate, onHotUpdateSuccess) { ...@@ -367,8 +367,12 @@ function tryApplyUpdates(onBeforeHotUpdate, onHotUpdateSuccess) {
module.hot module.hot
.check(/* autoApply */ false) .check(/* autoApply */ false)
.then((updatedModules) => { .then((updatedModules) => {
if (!updatedModules) {
return null
}
if (typeof onBeforeHotUpdate === 'function') { if (typeof onBeforeHotUpdate === 'function') {
const hasUpdates = Boolean(updatedModules?.length) const hasUpdates = Boolean(updatedModules.length)
onBeforeHotUpdate(hasUpdates) onBeforeHotUpdate(hasUpdates)
} }
return module.hot.apply() return module.hot.apply()
......
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