{"record":{"id":"eeada3096e2611c6","repo":"sveltejs/svelte","slug":"set-context-after-init","errorCode":"set_context_after_init","errorMessage":"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","messagePattern":"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","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/internal/client/errors.js","lineNumber":444,"sourceCode":"\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/rune_outside_svelte`);\n\t}\n}\n\n/**\n * `setContext` must be called when a component first initializes, not in a subsequent effect or after an `await` expression\n * @returns {never}\n */\nexport function set_context_after_init() {\n\tif (DEV) {\n\t\tconst 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`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/set_context_after_init`);\n\t}\n}\n\n/**\n * Property descriptors defined on `$state` objects must contain `value` and always be `enumerable`, `configurable` and `writable`.\n * @returns {never}\n */\nexport function state_descriptors_fixed() {\n\tif (DEV) {\n\t\tconst 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`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/state_descriptors_fixed`);\n\t}\n}","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/internal/client/errors.js#L426-L462","documentation":"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.","triggerScenarios":"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.","commonSituations":"Awaiting data then calling setContext; setting context inside onMount/$effect; refactor that moved setContext into an async function; top-level await in component script.","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."],"exampleFix":"// before\nlet data = await fetchThing();\nsetContext('thing', data); // after await -> throws\n\n// after\nsetContext('thing', {}); // synchronously\nlet data = $state();\nonMount(async () => { data = await fetchThing(); });","handlingStrategy":"validation","validationCode":"// Call setContext synchronously at the top of the component body, before any await.\n// If the value depends on async data, register context synchronously and update contents later:\n// setContext('key', {});\n// onMount(async () => { Object.assign(getContext('key'), await load()); });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["svelte","context","async","lifecycle","runes"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}