ionic-team / stencil

A toolchain for building scalable, enterprise-ready component systems on top of TypeScript and Web Component standards. Stencil components can be distributed natively to React, Angular, Vue, and traditional web developers from a single, framework-agnostic codebase.
https://stenciljs.com
Other
12.6k stars 791 forks source link

bug: hydrate crashes if component uses @Listen #5869

Closed mayerraphael closed 5 months ago

mayerraphael commented 5 months ago

Prerequisites

Stencil Version

4.19.0

Current Behavior

If a component has a @Listen() function (tested with keydown) Stencil hydrate crashes and does not serialize the component.

image

Culprit is the hydrateComponent method:

async function hydrateComponent(win2, results, tagName, elm, waitingElements) {
  tagName = tagName.toLowerCase();
  const Cstr = loadModule(
    {
      $tagName$: tagName,
      $flags$: null
    });
  if (Cstr != null) {
    const cmpMeta = Cstr.cmpMeta;
    if (cmpMeta != null) {
      waitingElements.add(elm);
      const hostRef = getHostRef(this);
      addHostEventListeners(this, hostRef, cmpMeta.$listeners$); // --------------------------------- "this" is undefined

Expected Behavior

For components to correctly serialize with @Listen() methods.

System Info

No response

Steps to Reproduce

See repository.

Code Reproduction URL

https://github.com/mayerraphael/stencil-dsd-ssr-playground

Additional Information

The my-whatever-component has an @Listen() method.

Affects both serializeShadowRoot true and false.

christian-bromann commented 5 months ago

@mayerraphael thanks for raising this issue and helping us make Stencil better.

tested with keydown

Can you please describe how you did trigger the event? If I change the eventName to click , everything works fine when clicking on it.

christian-bromann commented 5 months ago

Actually after some more testing, I am able to reproduce this issue and can investigate/work on a solution.

christian-bromann commented 5 months ago

A bug fix has been proposed in #5877 and a dev release (4.19.1-dev.1719875079.a06f64c) confirmed that it fixes the double rendering in the provided reproduction case.