Skip to content

bug: Store changes doesn't trigger re-render of a component on dist-custom-elements build #4135

Description

@shtolcers

Prerequisites

Stencil Version

3.0.0

Current Behavior

The component is not being re-rendered when the state value of @stencil/store changes (example from here: https://stenciljs.com/docs/stencil-store)

Expected Behavior

Store's state changes trigger the re-render of the components when necessary regardless of what the output target's type is or whether the component uses a local state.

System Info

System: Ubuntu 20.04.5 LTS
Browsers: Chrome 108.0.5359.124 (Official Build) (64-bit) and FF 110.0.1 (64-bit)


> npm -v
9.2.0

> node -v
v16.13.2

> npx stencil info
      System: node 16.13.2
     Plaform: linux (5.15.0-60-generic)
   CPU Model: AMD Ryzen 7 PRO 4750U with Radeon Graphics (16 cpus)
    Compiler: /home/asu/Desktop/tutorials/_bug/test-component/node_modules/@stencil/core/compiler/stencil.js
       Build: 20230228172721
     Stencil: 3.1.0 🍕
  TypeScript: 4.9.5
      Rollup: 2.42.3
      Parse5: 7.1.2
      Sizzle: 2.42.3
      Terser: 5.16.4

Steps to Reproduce

  1. Build a component that uses store with the output target's type set to dist-custom-elements
    OR set the type to dist, without(!) using the local @State variable in components.
  2. Include the library in the HTML file with a script tag

Provided Code Reproduction URL contains 3 different Stencil projects to be sure all stores are independent and don't affect components in some unexpected way.

Code Reproduction URL

https://github.com/shtolcers/demo-stencil-app

Additional Information

I could work around the issue by using dist build and adding a local dummy state. Unfortunately, the component library built using Stencil is used by a third-party Angular app. That app needs to manage assets' location. Since setAssetPath is exported only in dist-custom-elements build, dist is a no-go.

If you have any ideas for workarounds or temporary solutions please share them, and I'll gladly try them out.
Thank You!

Activity

  1. alicewriteswrongs commented on Mar 8, 2023

    @alicewriteswrongs
    Contributor

    @shtolcers thank you for providing the detailed and thorough reproduction, it really helps to figure out what's going on!

    I was able to confirm the issue is present, I'm going to label this so it gets ingested into our internal backlog for prioritization and whatnot.

    One question - was this previously not happening in your project with earlier versions of Stencil? It would be good to know if this is a regression or a longer-standing bug.

  2. added
    Bug: Validated
    on Mar 8, 2023
  3. shtolcers commented on Mar 9, 2023

    @shtolcers
    Author

    @alicewriteswrongs Thank You! And yes, the bug was noticed on the project with "@stencil/core": "^2.17.4" and "@stencil/store": "^2.0.0", The first obvious approach was to try upgrading Stencil and store to the latest.

  4. EdgarsJoja commented on Mar 13, 2023

    @EdgarsJoja

    Hey, @alicewriteswrongs do you have some rough idea, when a fix for this could become available? We would like to understand whether we can afford to wait for a fix or we need to drop store for our particular functionality and create some workaround.

  5. malarahfelipe commented on Apr 21, 2023

    @malarahfelipe

    news on this? @rwaskiewicz

  6. TheRoadSouth commented on Oct 24, 2024

    @TheRoadSouth

    Any update on this? I think we are bumping into a similar issue.

  7. rjborba commented on Nov 5, 2024

    @rjborba

    This is very crucial and impacts pretty much all minimally large Stencil components.
    A position from the team would be very appreciated. Maybe community can help with this one, but as @EdgarsJoja said, we may want to drop stencil and use something else.

  8. rjborba commented on Nov 6, 2024

    @rjborba

    FYI Issue only happens when externalRuntime is set to false

  9. crrobinson14 commented on Nov 20, 2024

    @crrobinson14

    Confirmed we are having the same issue. It never occurred to me until I saw @rjborba 's comment about externalRuntime but setting this to false breaks the store's reactivity for us as well. That at least solves one puzzle - we had recently upgraded our React output target to take advantage of the fix posted by @christian-bromann in #4135, but that update now throws an error that externalRuntime must now be set to false there. Unfortunately that means the fix in #4135 is not actually usable yet, despite being released. I would report this there but that thread is now locked.

    It looks like until this is resolved we will have to version-pin to an older release:

    1. Pin @stencil/react-output-target to 0.6.0 in main StencilJS project.
    2. Remove @stencil/react-output-target version ^0.7.4 from React output target project.
    3. Revert reactOutputTarget config settings changes back to the old broken (but tolerable) settings:
        ...
        reactOutputTarget({
          // Broken in 0.7.4:
          // outDir: '../verdocs-web-sdk-react/src',
          // Works in 0.6.0, with hand editing between builds:
          componentCorePackage: '@verdocs/web-sdk',
          proxiesFile: '../verdocs-web-sdk-react/src/components.ts',
          includeImportCustomElements: true,
          customElementsDir: 'dist/components',
        }),
        {
          ...
          // Breaks @stencil/store in recent StencilJS releases, when used with reactOutputTarget:
          // externalRuntime: false,
        },
        ...
    
  10. rjborba commented on Dec 25, 2024

    @rjborba

    Should this one be closed indeed? I believe we still have this problem

  11. christian-bromann commented on Dec 25, 2024

    @christian-bromann
    Member

    @rjborba mind raising a new issue providing a reproduction case? Happy to take a look.

  12. 7 remaining items

  13. rjborba commented on Jan 15, 2025

    @rjborba

    I’ve made a solution to fix that, I’ll share shortly
    Basically created a decorator in the components that share stores and this
    decorator became responsible to subscribe to the events and then send the
    update using forceUpdate(this.host) ( element created reference)
    Done that a long time ago, sorry not getting back at you earlier

    Felipe MalaraSoftware Developer
    …

    @malarahfelipe I'm re-reading it now, so this solution is a workaround or something that will be integrated in Stencil core code?

  14. christian-bromann commented on Jan 15, 2025

    @christian-bromann
    Member

    @rjborba we would appreciated any contributions in this area. @malarahfelipe can you maybe share an example of your workaround?

  15. crrobinson14 commented on Jan 15, 2025

    @crrobinson14

    I wouldn't call this a fix or workaround, but just reporting back, we "solved" this by dropping the Stencil Store completely and using a different store. Our use-case was simple so fortunately this wasn't a difficult change. We used https://tinybase.org/ for this, and "subscribe" to it in componentDidMount and "unsubscribe" in disconnectedCallback. I wouldn't call this a very "Stencil-y" way but it's working well for us so far.

    I mention this only because Tinybase is a pretty sophisticated library with some additional nice-to-have functionality. Somebody with more time might perhaps come up with a decorator-style approach to provide a more streamlined integration for it in Stencil? Perhaps some day it might even replace the need to maintain the extra stencil-store library - one less burden for the core team. Just a thought.

  16. rjborba commented on Jan 16, 2025

    @rjborba

    @christian-bromann Hi! I would love to look into it, but I'll be unable to do so for at least next month. Looks like the right approach for us will be unfortunately get rid of Stencil Store.

    If this issue won't be fixed til next month, I'll start digging into it.

  17. crrobinson14 commented on Jan 24, 2025

    @crrobinson14

    @christian-bromann Should this be re-marked "will not implement" or similar to assist future devs in understanding what is going on? This is not completed/fixed.

  18. christian-bromann commented on Jan 24, 2025

    @christian-bromann
    Member

    This is not completed/fixed.

    Can you elaborate on this?

  19. johnjenkins commented on Jan 24, 2025

    @johnjenkins
    Contributor

    is it 'cos the fix hasn't been released yet?

  20. crrobinson14 commented on Jan 24, 2025

    @crrobinson14

    Ah maybe. Sorry, will re-check against the next release.

  21. christian-bromann commented on Jan 25, 2025

    @christian-bromann
    Member

    A fix for this has been released today as part of Stencil v4.25.1!

  22. glennverschooren commented on Mar 4, 2025

    @glennverschooren

    I'm on version 4.27.1 and still having this issue, am I the only one?

  23. christian-bromann commented on Mar 4, 2025

    @christian-bromann
    Member

    @glennverschooren can you share a minimal reproducible example?

  24. jurienhamaker commented on Aug 13, 2025

    @jurienhamaker

    Still not fixed... Any updates on this?

  25. johnjenkins commented on Aug 13, 2025

    @johnjenkins
    Contributor

    @jurienhamaker the original reproduction use-case was fixed.
    Can you open a new issue with a new repro? 🙏

  26. stachow commented on Sep 3, 2025

    @stachow

    We have hit this issue (or at least a closely-related effect) and have found an acceptable workaround. We use the dist build but we've checked the workaround still works with dist-custom-elements.

    Our diagnosis:

    • The Stencil store guidance suggests statically initializing the store in the top-level scope of the module, example). The store is therefore created at module-load time.
    • The store internals are set up immediately upon creation. A check made as part of this fails as getRenderingRef returns an empty object because renderingRef is not yet populated. This means that the subscriptions required to enable rerendering on store updates are never created.
    • Our assumption is that there is a race going on, and store initialization is occurring before callRender starts and/or completes.

    Our workaround:

    • We delay initialization of the store until the start of globalScript execution.
    // store.ts
    import { createStore } from "@stencil/store";
    
    let store: ReturnType<typeof createStore<State>>;
    
    export const initialiseStore = () => {
      store = createStore<State>(() => initialState);
    };
    
    // global-script.ts
    import { initialiseStore } from "./store";
    
    export default () => {
      initialiseStore();
    
      // ...
    };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Bug: ValidatedHelp Wanted

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions