Skip to content

Using the context API in standalone components loaded at runtime returns undefined #3615

@ghost

Description

Edit: I've found this issue too, which I think looks like the same problem I'm describing here: #3422

Describe the bug
I'm trying to build a system that loads external standalone components at runtime, and I want to use the getContext and setContext functions to share a store between all of the components.

I'm using webpack to build the main application which contains an App component that serves as the root of the application, and then I'm using a completely separate project to build another external standalone component (both an SSR version and a DOM versions), which I then load in at runtime using require on the Node server-side, and a <script> tag on the client-side within the main App component.

The external standalone component does load and render correctly, however within the component any calls to getContext return undefined.

I'm not familiar with the Svelte codebase, but I assume this is due to the standalone component being built as a separate project and getting its own versions of the context related variables/functions at build time, rather than sharing the same variables etc. from the parent.

I assume this is a bug, but if I'm just doing something wrong please let me know.

Logs
No logs other than getContext printing undefined in the child standalone component.

To Reproduce

  • Build a simple Svelte application (doesn't matter if it uses SSR or not)
  • Load a standalone component built from another project (using a <script> tag or something)
  • Render the component inside a component from the main application using <svelte:component this={MyStandaloneComponent} />
  • See getContext in the standalone component return undefined because it (I think) doesn't have the same context

Expected behavior
I expect to see getContext in the standalone component return data from the same context as the parent component from the main application.

Information about your Svelte project:

  • This happens in all browsers
  • This happens in all OSes
  • Svelte version is 3.12.1
  • Webpack using svelte-loader 2.13.6

Severity
I can get around this issue by passing the store I want to share as a prop to the child standalone component, but it's not ideal, especially in large projects where the same store may need to be shared amonst hundreds of standalone components.

This problem is more one of code maintenance/tech debt in my specific case, so I'd much prefer to have getContext work correctly where the standalone child component should inherit the context from the parent component in the main application.

Although in my specifc case I can easily work around the issue using props instead, I can imagine that for applications sharing much more than a simple store this could be prohibitive, so I'd say it's a fairly severe issue.

Additional context
I've tried various build options using Webpack, such as changing libraryTarget, or changing Svelte build flags, like changing customElements to true or false.

No matter the build options this problem always seems to occur.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions