Regression in SSR context handling requires understanding of Svelte's lifecycle and bundler integration.
A regression since Svelte 5.39.0 causes SSR builds with Rollup/Rolldown to throw lifecycle errors when using setContext. The issue appears related to context management during SSR rendering and requires investigation of the bundler integration. The maintainer comment suggests this involves shared globals between render and compiled code.
Since [email protected], when building a component that uses setContext with Rolldown (or Rollup) for SSR, the build succeeds but the component throws a lifecycle_outside_component error at runtime. This error occurs when trying to render the component server-side with render from svelte/server.
This is a regression that started in Svelte 5.39.0 - downgrading to 5.38.0 resolves the issue.
This was probably introduced in #16748
repo: https://github.com/jackgdll/svelte-5-rolldown-ssr-error
svelte@>=5.39.0setContextimport { defineConfig } from "rolldown";
import svelte from "rollup-plugin-svelte";
import { sveltePreprocess } from "svelte-preprocess";
export default defineConfig({
input: "src/main.svelte",
output: {
file: "dist/rolldown.js",
},
plugins: [
svelte({
compilerOptions: {
generate: "server",
},
preprocess: sveltePreprocess(),
}),
],
});
render from svelte/serverlifecycle_outside_component error will be thrownFAIL test/ssr.test.js > SSR > renders Rolldown bundled component
Svelte error: lifecycle_outside_component
`setContext(...)` can only be used during component initialisation
https://svelte.dev/e/lifecycle_outside_component
❯ lifecycle_outside_component dist/rolldown.js:102:33
100| function lifecycle_outside_component(name) {
101| if (dev_fallback_default) {
102| const error = /* @__PURE__ */ new Error(`lifecycle_outside_component\n\`${name}(...)\` can only be used during component initialisation\nhttps://svelte.dev/e/lifecycle_outside_component`);
| ^
103| error.name = "Svelte error";
104| throw error;
❯ get_or_init_context_map dist/rolldown.js:3405:28
❯ setContext dist/rolldown.js:3397:2
❯ dist/rolldown.js:
Claim this issue to let others know you're working on it. You'll earn 10 points when you complete it!