rsnext/packages
Colin ed2aa9fd75
Fix HMR error: "Cannot read properties of null (reading 'length')" (#43145)
After upgrading to Next.js 13, we started seeing the following HMR
errors:

```sh
$ next dev
# ...
warn  - Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/basic-features/fast-refresh#how-it-works
TypeError: Cannot read properties of null (reading 'length')
    at eval (webpack-internal:///./node_modules/next/dist/client/dev/error-overlay/hot-dev-client.js:262:55)
```

This error appears related to the changes made in
https://github.com/vercel/next.js/pull/42350 (cc @alexkirsz). It appears
that `module.hot.check` will pass `null` when there are no updates:

```ts
  /**
   * Throws an exceptions if status() is not idle.
   * Check all currently loaded modules for updates and apply updates if found.
   * If no update was found, the callback is called with null.
   * If autoApply is truthy the callback will be called with all modules that were disposed.
   * apply() is automatically called with autoApply as options parameter.
   * If autoApply is not set the callback will be called with all modules that will be disposed on apply().
   * @param autoApply
   */
  check(autoApply?: boolean): Promise<null|ModuleId[]>;
```

When `updatedModules` is `null`, we skip the `apply()` call as this was
producing `apply() is only allowed in ready status (state: idle)`
errors. This matches [the prior behavior when `autoApply` was
enabled](8241da7f1e/lib/hmr/HotModuleReplacement.runtime.js (L266-L272)).

Fixes #43143. Also reported on Stack Overflow: 
-
https://stackoverflow.com/questions/74415937/nextjs-typeerror-cannot-read-properties-of-null-reading-length
-
https://stackoverflow.com/questions/74504229/nextjs-v-13-typeerror-cannot-read-properties-of-null-reading-length

I tested this change locally and no longer see these HMR errors.

## Bug

- [X] Related issues linked using `fixes #number` - no related issues
- [X] Integration tests added - there aren't any existing tests, afaict
- [X] Errors have a helpful link attached, see `contributing.md` - N/A

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-11-21 16:50:03 -08:00
..
create-next-app v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
eslint-config-next v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
eslint-plugin-next v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
font v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
next Fix HMR error: "Cannot read properties of null (reading 'length')" (#43145) 2022-11-21 16:50:03 -08:00
next-bundle-analyzer v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
next-codemod v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
next-env v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
next-mdx v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
next-plugin-storybook v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
next-polyfill-module v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
next-polyfill-nomodule v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
next-swc fix(next-swc/relay): make pages directory optional (#43116) 2022-11-20 12:31:26 -08:00
react-dev-overlay v13.0.5-canary.3 2022-11-19 20:12:30 -08:00
react-refresh-utils v13.0.5-canary.3 2022-11-19 20:12:30 -08:00