sveltejs/svelte · error · Error
set_context_after_init
set_context_after_init
Error message
set_context_after_init `setContext` must be called when a component first initializes, not in a subsequent effect or after an `await` expression https://svelte.dev/e/set_context_after_init
What it means
Thrown at internal/client/context.js:138 (only in async mode) when setContext is called outside the synchronous component-initialisation window — specifically when there is an active reaction, or the branch-effect/pop() flags indicate the call happens after an await boundary or inside a later effect. Context in Svelte is tied to component init; setting it later would make it unavailable to the synchronous render of children.
Source
Thrown at packages/svelte/src/internal/client/errors.js:444
throw error;
} else {
throw new Error(`https://svelte.dev/e/rune_outside_svelte`);
}
}
/**
* `setContext` must be called when a component first initializes, not in a subsequent effect or after an `await` expression
* @returns {never}
*/
export function set_context_after_init() {
if (DEV) {
const error = new Error(`set_context_after_init\n\`setContext\` must be called when a component first initializes, not in a subsequent effect or after an \`await\` expression\nhttps://svelte.dev/e/set_context_after_init`);
error.name = 'Svelte error';
throw error;
} else {
throw new Error(`https://svelte.dev/e/set_context_after_init`);
}
}
/**
* Property descriptors defined on `$state` objects must contain `value` and always be `enumerable`, `configurable` and `writable`.
* @returns {never}
*/
export function state_descriptors_fixed() {
if (DEV) {
const error = new Error(`state_descriptors_fixed\nProperty descriptors defined on \`$state\` objects must contain \`value\` and always be \`enumerable\`, \`configurable\` and \`writable\`.\nhttps://svelte.dev/e/state_descriptors_fixed`);
error.name = 'Svelte error';
throw error;
} else {
throw new Error(`https://svelte.dev/e/state_descriptors_fixed`);
}
}View on GitHub (pinned to 20b341f100)
Solutions
- Call setContext synchronously at the top of the component body, before any await or effect.
- If the context value depends on async data, set a placeholder synchronously and update the reactive contents later rather than delaying setContext itself.
- Move async work into an effect and store results in $state; keep context registration synchronous.
Example fix
// before
let data = await fetchThing();
setContext('thing', data); // after await -> throws
// after
setContext('thing', {}); // synchronously
let data = $state();
onMount(async () => { data = await fetchThing(); }); Defensive patterns
Strategy: validation
Validate before calling
// Call setContext synchronously at the top of the component body, before any await.
// If the value depends on async data, register context synchronously and update contents later:
// setContext('key', {});
// onMount(async () => { Object.assign(getContext('key'), await load()); }); Prevention
- Place setContext at the very top of the component script, before awaits and effects.
- Never call setContext inside onMount, $effect, or after an await.
- Register a placeholder synchronously and populate it reactively if data is async.
When it happens
Trigger: Calling setContext after an `await` inside component setup, inside a $effect, inside a then()/setTimeout callback, or after the initial synchronous component body has completed. The guard at context.js:129-139 checks active_reaction and the component context 'i' (pop) flag.
Common situations: Awaiting data then calling setContext; setting context inside onMount/$effect; refactor that moved setContext into an async function; top-level await in component script.
Related errors
- effect_orphan
- get_abort_signal_outside_reaction
- lifecycle_legacy_only
- lifecycle_outside_component
- async_derived_orphan
AI-assisted analysis of sveltejs/svelte@20b341f100 (2026-08-12).
Data as JSON: /api/errors/eeada3096e2611c6.
Report an issue: GitHub.