Skip to content

bug: regression for disconnected components #6148

Closed

Description

@k1w1

Prerequisites

Stencil Version

4.25.0

Current Behavior

In #6087 a regression was introduced when a component is temporarily removed from the DOM.

This PR appears to introduce a new bug on this line: https://github.com/ionic-team/stencil/pull/6087/files#diff-248f811e203ca495795884ef5d47f9e83f428ffa0ce37472bc1b552195706676R112. If a component is removed from the DOM, but still being used, then any call to set an attribute of the component while it is not in the DOM will crash because getHostRef will not find the component.

This only happens for the dist-custom-elements output target.

Expected Behavior

The host ref should not be released when a component is removed from the DOM.

System Info

Steps to Reproduce

Use the linked repo. See the README.md

Code Reproduction URL

https://github.com/k1w1/disconnected-set-bug

Additional Information

No response

Activity

  1. added
    Bug: Validated
    and removed on Feb 6, 2025
  2. danielleroux commented on Feb 6, 2025

    @danielleroux
    Contributor

    Just thought about the issue and always I looked into the implementation I ask myself: "Why is the WeakMap necessary at all?"
    Why not simply store the hostRef directly on the Node?

    e.g

    const getHostRef = (ref) => {
      return ref['$$$hostRef'];
    };
    const registerHost = (hostElement, cmpMeta) => {
      // registerHost logic
      hostElement['$$$hostRef'] = hostRef;
      return ref;
    };
  3. johnjenkins commented on Feb 6, 2025

    @johnjenkins
    Contributor

    @danielleroux because Stencil, for the dist output, needs a way to connect lazy loaded class instances with their corresponding elements.
    Your proposal could potentially work for the dist-custom-elements output though as the class instance and element is 1:1 but might end up with causing more overall complexity idk

  4. danielleroux commented on Feb 6, 2025

    @danielleroux
    Contributor

    @danielleroux because Stencil, for the dist output, needs a way to connect lazy loaded class instances with their corresponding elements. Your proposal could potentially work for the dist-custom-elements output though as the class instance and element is 1:1 but might end up with causing more overall complexity idk

    Got it, but could we do some similar hand over the hostRef after the class instance is loaded? Regarding the complexity you might be correct 😶‍🌫️

  5. johnjenkins commented on Feb 6, 2025

    @johnjenkins
    Contributor

    Got it, but could we do some similar hand over the hostRef after the class instance is loaded? Regarding the complexity you might be correct 😶‍🌫️

    I think the connection needs to be 'live' to enable stuff like:

    • some internal state updates a class instance's @Prop() which needs to be reflected - we need to find the corresponding element to update or (vice versa) ....
    • some element.setAttribute(...) occurs on the element, we need to find the corresponding class instance to update it's internal state.

    ^ in these cases, the map acts as a live bridge to keep the 2 in-sync.

  6. danielleroux commented on Feb 6, 2025

    @danielleroux
    Contributor

    Got it, but could we do some similar hand over the hostRef after the class instance is loaded? Regarding the complexity you might be correct 😶‍🌫️

    I think the connection needs to be 'live' to enable stuff like:

    • some internal state updates a class instance's @Prop() which needs to be reflected - we need to find the corresponding element to update or (vice versa) ....
    • some element.setAttribute(...) occurs on the element, we need to find the corresponding class instance to update it's internal state.

    ^ in these cases, the map acts as a live bridge to keep the 2 in-sync.

    Good call have to check this. Just for testing around I made some quick and dirty changes (main...danielleroux:stencil:poc-host-refs). Works with default scenarios (dist and dist-custom-elements) but dont checked the memory if we have some leaks there now again)

  7. johnjenkins commented on Feb 6, 2025

    @johnjenkins
    Contributor

    Good call have to check this. Just for testing around I made some quick and dirty changes (main...danielleroux:stencil:poc-host-refs). Works with default scenarios (dist and dist-custom-elements) but dont checked the memory if we have some leaks there now again)

    Nice! I think it looks sensible / should work ... in my head it should anyway 😅

  8. danielleroux commented on Feb 11, 2025

    @danielleroux
    Contributor

    Good call have to check this. Just for testing around I made some quick and dirty changes (main...danielleroux:stencil:poc-host-refs). Works with default scenarios (dist and dist-custom-elements) but dont checked the memory if we have some leaks there now again)

    Nice! I think it looks sensible / should work ... in my head it should anyway 😅

    Any advice to proceed? Should I fix the typings (remove any casts) and open a PR or do we need any further analysis regarding ionic-framework? I checked the code for detached DOM nodes but it looks fine.

    We are facing the error also during testing with react output target (dist-custom-elements) and testing-framework. All tests are failing because of the undefined instance reference.

  9. johnjenkins commented on Feb 11, 2025

    @johnjenkins
    Contributor

    @danielleroux - awesome!
    Next steps - tidy up, do all the admin (prettier, lint, run spec tests (npm run test.watch), wdio tests (npm run test.wdio) e2e tests (npm run test.end-to-end)) and make a PR - we'll see if anyone has any feedback and can take it for a test-drive :)

  10. christian-bromann commented on Feb 11, 2025

    @christian-bromann
    Member

    @johnjenkins let me know if you want me to make dev releases of certain branches for testing.

  11. johnjenkins commented on Feb 11, 2025

    @johnjenkins
    Contributor

    good to know! (@danielleroux ^)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug: ValidatedHelp Wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions