This page documents the Svelte 5 component lifecycle as implemented in the client-side runtime. It covers the mounting, hydration, and unmounting processes, effect hierarchy management, and the boundary system for error handling.
For details on the underlying reactivity primitives, see page [4.1]. For batching and async update handling, see [4.2]. For control flow block implementations, see [4.4].
The component lifecycle consists of three primary phases, each managed by specific runtime functions:
Sources:
packages/svelte/src/internal/client/render.js67-152
packages/svelte/src/internal/client/reactivity/effects.js266-283
The mount() function creates a new component instance and attaches it to the DOM. It is defined in packages/svelte/src/internal/client/render.js67-69 and performs the following sequence:
The mount() function follows these steps:
init_operations() packages/svelte/src/internal/client/render.js167component_root() which returns a cleanup function packages/svelte/src/internal/client/reactivity/effects.js266-283boundary() packages/svelte/src/internal/client/render.js176-216push/pop) packages/svelte/src/internal/client/render.js182-213mounted_components WeakMap packages/svelte/src/internal/client/render.js288| Option | Type | Purpose |
|---|---|---|
target | Element | Document | ShadowRoot | DOM node to mount into |
anchor | Node (optional) | Insertion point within target |
props | Record<string, any> | Component properties |
events | Record<string, Function> (optional) | Event handlers |
context | Map<any, any> (optional) | Context values |
intro | boolean (default: true) | Whether to play intro transitions |
transformError | Function (optional) | Error transformation function |
Sources:
packages/svelte/src/internal/client/render.js67-290
packages/svelte/src/internal/client/reactivity/effects.js266-283
The hydrate() function attaches a component to existing server-rendered HTML rather than creating new DOM nodes. It is defined in packages/svelte/src/internal/client/render.js96-152
Server-rendered HTML includes special comment nodes that guide the hydration process:
| Marker | Purpose |
|---|---|
<!--<FileRef file-url="https://github.com/sveltejs/svelte/blob/0c7f8151/--> | HYDRATION_START - marks the beginning of component content [packages/svelte/src/constants.js#L11-L11" min=11 file-path="-->` |
<!--]--> | HYDRATION_END - marks the end of component content packages/svelte/src/constants.js12 |
<!--<FileRef file-url="https://github.com/sveltejs/svelte/blob/0c7f8151/?--> | HYDRATION_START_ELSE - marks else/fallback branches [packages/svelte/src/constants.js#L14-L14" min=14 file-path="?-->` |
During hydration, the runtime:
HYDRATION_START is found packages/svelte/src/internal/client/render.js104-115set_hydrating(true) and set_hydrate_node() packages/svelte/src/internal/client/render.js117-118HYDRATION_ERROR on mismatch packages/svelte/src/internal/client/render.js114options.recover !== false packages/svelte/src/internal/client/render.js138-147If the client-side component structure doesn't match server-rendered HTML:
Sources:
packages/svelte/src/internal/client/render.js96-152
packages/svelte/src/internal/client/dom/hydration.js1-183
packages/svelte/src/constants.js11-15
Components are structured as trees of effects, where each effect can have parent and child effects. This hierarchy is central to Svelte's lifecycle management.
Effects are created with specific flags that determine their behavior in the hierarchy:
| Effect Type | Flag | Purpose | Created By |
|---|---|---|---|
ROOT_EFFECT | 1 << 6 | Top-level component effect | component_root() packages/svelte/src/internal/client/reactivity/effects.js266 |
BRANCH_EFFECT | 1 << 5 | Transparent boundary between blocks | branch() packages/svelte/src/internal/client/reactivity/effects.js419 |
BLOCK_EFFECT | 1 << 4 | Control flow blocks | block() packages/svelte/src/internal/client/reactivity/effects.js396 |
RENDER_EFFECT | 1 << 3 | DOM rendering effects | render_effect() packages/svelte/src/internal/client/reactivity/effects.js355 |
EFFECT | 1 << 2 | User-defined effects | user_effect() packages/svelte/src/internal/client/reactivity/effects.js196 |
MANAGED_EFFECT | 1 << 24 | Effects that preserve children | managed() packages/svelte/src/internal/client/reactivity/effects.js408 |
Effects maintain bidirectional links through the first, last, next, and prev properties:
Effects are linked during creation via push_effect() packages/svelte/src/internal/client/reactivity/effects.js69-78:
parent to active_effectlast.next points to new effectprev points to parent's lastlast updated to new effectWhen an effect is destroyed via destroy_effect() packages/svelte/src/internal/client/reactivity/effects.js494-542:
The unlink_effect() function packages/svelte/src/internal/client/reactivity/effects.js564-576 removes the effect from the doubly-linked list while maintaining the integrity of sibling connections.
Sources:
packages/svelte/src/internal/client/reactivity/effects.js69-172
packages/svelte/src/internal/client/reactivity/effects.js494-576
packages/svelte/src/internal/client/constants.js1-78
The unmount() function destroys a component instance and cleans up all associated resources. It is defined in packages/svelte/src/internal/client/render.js318-335
The unmount process follows these steps:
mounted_components WeakMap packages/svelte/src/internal/client/render.js319options.outro === true via pause_effect() packages/svelte/src/internal/client/reactivity/effects.js588-608destroy_effect() packages/svelte/src/internal/client/reactivity/effects.js494-542mounted_components packages/svelte/src/internal/client/render.js322| Resource Type | Cleanup Action | Code Location |
|---|---|---|
| Effect tree | destroy_effect() - recursive destruction | packages/svelte/src/internal/client/reactivity/effects.js494-542 |
| DOM nodes | remove_effect_dom() - removes nodes between start and end | packages/svelte/src/internal/client/reactivity/effects.js549-557 |
| Event listeners | removeEventListener() on target and document | packages/svelte/src/internal/client/render.js261-280 |
| Reactions | remove_reactions() - detaches from signals | packages/svelte/src/internal/client/runtime.js416-423 |
| Teardown functions | execute_effect_teardown() | packages/svelte/src/internal/client/reactivity/effects.js426-440 |
| Transitions | transition.stop() on collected managers | packages/svelte/src/internal/client/reactivity/effects.js510-516 |
When unmount() is called with { outro: true }, the component plays outro transitions before destruction:
The pause_effect() function packages/svelte/src/internal/client/reactivity/effects.js588-608 marks effects as INERT and collects all TransitionManager instances. Each manager's out() method is called with a callback that counts down remaining transitions. Once all transitions complete, destroy_effect() is invoked.
Sources:
packages/svelte/src/internal/client/render.js318-335
packages/svelte/src/internal/client/reactivity/effects.js588-694
packages/svelte/src/internal/client/reactivity/effects.js494-542
Each component maintains a context object that tracks its state and relationships:
The context is managed through:
set_component_context() during effect executionpush() and pop() for context nestingSources: packages/svelte/src/internal/client/context.js10-16 packages/svelte/src/internal/client/types.d.ts12-52
Refresh this wiki