Skip to content

Ability to access $app/stores at any time in the browser #6043

@Conduitry

Description

@Conduitry

Describe the problem

There is sometimes code that I know will only ever be run on the browser (for example, if it's only called from an event handler), and having to jump through the same hoops as I do on the server in order to associate that with the appropriate (and only) instances of the stores is annoying.

Describe the proposed solution

Store values on the browser should live in a singleton module (or something similar) rather than in the Svelte context for the root component. getStores() should be able to be called at any time, not just during component initialization. If it wouldn't cause too much of a divergence between browser and server code, we could also remove the transparent calling of getStores() during the autosubscription to any of the individual stores.

Alternatives considered

Right now, I'm using a fairly ugly workaround where I serialize the values I need to a data-foo attribute in my root layout, which I then read and deserialize elsewhere.

Another option presumably would be to have an onMount call in my root layout that sets the store(s) as a global on window.

Importance

would make my life easier

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions