Skip to content

Re-rendering on HMR fails if the initial rendering of a component in a portal fails #568

@tommie

Description

@tommie

I'm getting this error

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length')

at https://github.com/preactjs/preact/blob/bb804cd317ed641bad3527d2d39e24e1d948fa41/src/component.js#L100

if I modify a file that has a component rendered inside a portal and that threw an exception on initial render. The property in question is _children on VNode.

It looks like vnodesForComponent isn't cleared in catchError, and it seems _children isn't initialized if initial rendering fails. Not sure if this is a Preact or Prefresh issue, but since the component is unmounted on error, Prefresh shouldn't try to re-render it.

I can only seem to trigger this when the component in question is in a portal, though I haven't been able to figure out why that's special.

When I first tried to make a StackBlitz repro, this didn't trigger. After upgrading to the latest versions of Vite, Preact and preact-vite, the issue started showing.

Repro

https://stackblitz.com/edit/create-preact-starter-ttn2e8qn?file=src%2Fresource.jsx

  1. Load the StackBlitz.
  2. The "Error: Intended" will show in the console.
  3. Make a change in resoure.jsx and wait a bit for HMR.
  4. The "TypeError: Cannot read properties of undefined (reading 'length')" will show in the console.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions