Skip to content

Component context is not initialized when rendering server components #15508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
oettingerj opened this issue Mar 13, 2025 · 2 comments
Closed

Comments

@oettingerj
Copy link

Describe the bug

I have a Svelte component in Repo A, with a Vite server configured to emit code for the server:

svelte({
	dynamicCompileOptions() {
		return { generate: 'server' }
	}
})

I'm using dynamicCompileOptions here because generate is not typically configurable within the svelte Vite plugin, and the only other way I'm aware of to trigger the compilation of SSR code is to use ssrLoadModule, which doesn't fit my use case.

Repo A exports an index.js file with render() and mount() functions.
Repo B is a SvelteKit site, which dynamically imports the module from Repo A over http using an import hook.

So far so good. However, when I call render() within the SvelteKit site, I get lifecycle_outside_component errors any time I try to set/get context or call onMount during component initialization. Digging further, it seems set_component_context is not called before rendering, resulting in component_context being null within internal/client/context.js.

Notwithstanding how nonstandard this architecture is, I'd expect onMount and set/getContext to work properly.

Reproduction

REPO

Logs

System Info

System:
    OS: macOS 15.3.1
    CPU: (16) arm64 Apple M4 Max
    Memory: 3.87 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 23.4.0 - ~/.local/share/mise/installs/node/23.4.0/bin/node
    npm: 10.9.2 - ~/.local/share/mise/installs/node/23.4.0/bin/npm
  Browsers:
    Safari: 18.3
  npmPackages:
    svelte: ^5.0.0 => 5.23.0

Severity

annoyance

@paoloricciuti
Copy link
Member

This is a bit of a weird setup...the problem you are facing is that you are generating a server svelte component...this is meant to be used in server runtimes where onMount is actually a noop. However with your setup you are generating the server code but you are accessing with an http request so the import { onMount } from 'svelte'; is resolved by vite to the browser version which can't work with the server version of the component.

What are you trying to actually achieve here?

@oettingerj
Copy link
Author

Makes sense. I'm working on a micro-frontend architecture in which one repo exports components to be loaded dynamically for SSR by a separate SvelteKit server. I eventually realized this was not the direction to go, and am now blocked by #13186 instead. This can be closed.

@oettingerj oettingerj closed this as not planned Won't fix, can't repro, duplicate, stale Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants