rsnext/packages/next/client/components
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
..
react-dev-overlay Fix HMR error: "Cannot read properties of null (reading 'length')" (#43145) 2022-11-21 16:50:03 -08:00
app-router-headers.ts Avoid bundling appDir rendering into pages edge SSR bundle (#43184) 2022-11-21 14:09:46 -08:00
app-router.tsx types: better type definition for internal utils (#43070) 2022-11-19 16:09:13 -08:00
dynamic.tsx Alias next/dynamic to lazy impl for appDir (#41216) 2022-10-06 17:41:27 +02:00
error-boundary.tsx Colocate styles with special entries (#42506) 2022-11-08 14:49:13 +01:00
error.tsx Add default not found template (#41750) 2022-10-24 15:46:59 -07:00
headers.ts Add force-static handling for app dir (#43061) 2022-11-18 09:47:44 -08:00
hooks-server-context.ts Upgrade react@experimental (#40885) 2022-09-25 21:35:05 +02:00
infinite-promise.ts Handle redirects in new router (#40396) 2022-09-20 15:28:07 +02:00
layout-router.tsx Handle head.js on client-side navigation (#42904) 2022-11-16 18:28:04 +01:00
match-segments.ts Flush styles effects (#39268) 2022-08-03 16:21:20 +00:00
navigation.ts Add force-static handling for app dir (#43061) 2022-11-18 09:47:44 -08:00
not-found.ts Add never return type for redirect() and notFound() (#42009) 2022-10-27 15:07:59 -07:00
redirect.ts Add never return type for redirect() and notFound() (#42009) 2022-10-27 15:07:59 -07:00
reducer.ts types: better type definition for internal utils (#43070) 2022-11-19 16:09:13 -08:00
render-from-template-context.tsx Client component directive: use client (#41333) 2022-10-11 10:26:45 -07:00
request-async-storage.ts Replace global with globalThis (#42627) 2022-11-08 14:45:30 +01:00
static-generation-async-storage.ts Add force-static handling for app dir (#43061) 2022-11-18 09:47:44 -08:00
static-generation-bailout.ts Add force-static handling for app dir (#43061) 2022-11-18 09:47:44 -08:00
use-reducer-with-devtools.ts misc: make useReducerWithDevtools noop on server (#41019) 2022-09-29 10:08:44 +00:00