Skip to content

Commit 58e4266

Browse files
Sync svelte docs (#1419)
sync svelte docs Co-authored-by: svelte-docs-bot[bot] <196124396+svelte-docs-bot[bot]@users.noreply.github.com>
1 parent afe1da1 commit 58e4266

File tree

4 files changed

+188
-6
lines changed

4 files changed

+188
-6
lines changed

apps/svelte.dev/content/docs/svelte/02-runes/02-$state.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class Todo {
120120
}
121121
```
122122

123-
> Svelte provides reactive implementations of built-in classes like `Set` and `Map` that can be imported from [`svelte/reactivity`](svelte-reactivity).
123+
> [NOTE!] Svelte provides reactive implementations of built-in classes like `Set` and `Map` that can be imported from [`svelte/reactivity`](svelte-reactivity).
124124
125125
## `$state.raw`
126126

apps/svelte.dev/content/docs/svelte/02-runes/04-$effect.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ The `$effect.tracking` rune is an advanced feature that tells you whether or not
222222

223223
It is used to implement abstractions like [`createSubscriber`](/docs/svelte/svelte-reactivity#createSubscriber), which will create listeners to update reactive values but _only_ if those values are being tracked (rather than, for example, read inside an event handler).
224224

225+
## `$effect.pending`
226+
227+
When using [`await`](await-expressions) in components, the `$effect.pending()` rune tells you how many promises are pending in the current [boundary](svelte-boundary), not including child boundaries ([demo](/playground/untitled#H4sIAAAAAAAAE3WRMU_DMBCF_8rJdHDUqilILGkaiY2RgY0yOPYZWbiOFV8IleX_jpMUEAIWS_7u-d27c2ROnJBV7B6t7WDsequAozKEqmAbpo3FwKqnyOjsJ90EMr-8uvN-G97Q0sRaEfAvLjtH6CjbsDrI3nhqju5IFgkEHGAVSBDy62L_SdtvejPTzEU4Owl6cJJM50AoxcUG2gLiVM31URgChyM89N3JBORcF3BoICA9mhN2A3G9gdvdrij2UJYgejLaSCMsKLTivNj0SEOf7WEN7ZwnHV1dfqd2dTsQ5QCdk9bI10PkcxexXqcmH3W51Jt_le2kbH8os9Y3UaTcNLYpDx-Xab6GTHXpZ128MhpWqDVK2np0yrgXXqQpaLa4APDLBkIF8bd2sYql0Sn_DeE7sYr6AdNzvgljR-MUq7SwAdMHeUtgHR4CAAA=)):
228+
229+
```svelte
230+
<button onclick={() => a++}>a++</button>
231+
<button onclick={() => b++}>b++</button>
232+
233+
<p>{a} + {b} = {await add(a, b)}</p>
234+
235+
{#if $effect.pending()}
236+
<p>pending promises: {$effect.pending()}</p>
237+
{/if}
238+
```
239+
225240
## `$effect.root`
226241

227242
The `$effect.root` rune is an advanced feature that creates a non-tracked scope that doesn't auto-cleanup. This is useful for nested effects that you want to manually control. This rune also allows for the creation of effects outside of the component initialisation phase.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts
3+
title: await
4+
---
5+
6+
As of Svelte 5.36, you can use the `await` keyword inside your components in three places where it was previously unavailable:
7+
8+
- at the top level of your component's `<script>`
9+
- inside `$derived(...)` declarations
10+
- inside your markup
11+
12+
This feature is currently experimental, and you must opt in by adding the `experimental.async` option wherever you [configure](/docs/kit/configuration) Svelte, usually `svelte.config.js`:
13+
14+
```js
15+
/// file: svelte.config.js
16+
export default {
17+
compilerOptions: {
18+
experimental: {
19+
async: true
20+
}
21+
}
22+
};
23+
```
24+
25+
The experimental flag will be removed in Svelte 6.
26+
27+
## Boundaries
28+
29+
Currently, you can only use `await` inside a [`<svelte:boundary>`](svelte-boundary) with a `pending` snippet:
30+
31+
```svelte
32+
<svelte:boundary>
33+
<MyApp />
34+
35+
{#snippet pending()}
36+
<p>loading...</p>
37+
{/snippet}
38+
</svelte:boundary>
39+
```
40+
41+
This restriction will be lifted once Svelte supports asynchronous server-side rendering (see [caveats](#Caveats)).
42+
43+
> [!NOTE] In the [playground](/playground), your app is rendered inside a boundary with an empty pending snippet, so that you can use `await` without having to create one.
44+
45+
## Synchronized updates
46+
47+
When an `await` expression depends on a particular piece of state, changes to that state will not be reflected in the UI until the asynchronous work has completed, so that the UI is not left in an inconsistent state. In other words, in an example like [this](/playground/untitled#H4sIAAAAAAAAE42QsWrDQBBEf2VZUkhYRE4gjSwJ0qVMkS6XYk9awcFpJe5Wdoy4fw-ycdykSPt2dpiZFYVGxgrf2PsJTlPwPWTcO-U-xwIH5zli9bminudNtwEsbl-v8_wYj-x1Y5Yi_8W7SZRFI1ZYxy64WVsjRj0rEDTwEJWUs6f8cKP2Tp8vVIxSPEsHwyKdukmA-j6jAmwO63Y1SidyCsIneA_T6CJn2ZBD00Jk_XAjT4tmQwEv-32eH6AsgYK6wXWOPPTs6Xy1CaxLECDYgb3kSUbq8p5aaifzorCt0RiUZbQcDIJ10ldH8gs3K6X2Xzqbro5zu1KCHaw2QQPrtclvwVSXc2sEC1T-Vqw0LJy-ClRy_uSkx2ogHzn9ADZ1CubKAQAA)...
48+
49+
```svelte
50+
<script>
51+
let a = $state(1);
52+
let b = $state(2);
53+
54+
async function add(a, b) {
55+
await new Promise((f) => setTimeout(f, 500)); // artificial delay
56+
return a + b;
57+
}
58+
</script>
59+
60+
<input type="number" bind:value={a}>
61+
<input type="number" bind:value={b}>
62+
63+
<p>{a} + {b} = {await add(a, b)}</p>
64+
```
65+
66+
...if you increment `a`, the contents of the `<p>` will _not_ immediately update to read this —
67+
68+
```html
69+
<p>2 + 2 = 3</p>
70+
```
71+
72+
— instead, the text will update to `2 + 2 = 4` when `add(a, b)` resolves.
73+
74+
Updates can overlap — a fast update will be reflected in the UI while an earlier slow update is still ongoing.
75+
76+
## Concurrency
77+
78+
Svelte will do as much asynchronous work as it can in parallel. For example if you have two `await` expressions in your markup...
79+
80+
```svelte
81+
<p>{await one()}</p>
82+
<p>{await two()}</p>
83+
```
84+
85+
...both functions will run at the same time, as they are independent expressions, even though they are _visually_ sequential.
86+
87+
This does not apply to sequential `await` expressions inside your `<script>` or inside async functions — these run like any other asynchronous JavaScript. An exception is that independent `$derived` expressions will update independently, even though they will run sequentially when they are first created:
88+
89+
```js
90+
async function one() { return 1; }
91+
async function two() { return 2; }
92+
// ---cut---
93+
// these will run sequentially the first time,
94+
// but will update independently
95+
let a = $derived(await one());
96+
let b = $derived(await two());
97+
```
98+
99+
> [!NOTE] If you write code like this, expect Svelte to give you an [`await_waterfall`](runtime-warnings#Client-warnings-await_waterfall) warning
100+
101+
## Indicating loading states
102+
103+
In addition to the nearest boundary's [`pending`](svelte-boundary#Properties-pending) snippet, you can indicate that asynchronous work is ongoing with [`$effect.pending()`]($effect#$effect.pending).
104+
105+
You can also use [`settled()`](svelte#settled) to get a promise that resolves when the current update is complete:
106+
107+
```js
108+
let color = 'red';
109+
let answer = -1;
110+
let updating = false;
111+
// ---cut---
112+
import { tick, settled } from 'svelte';
113+
114+
async function onclick() {
115+
updating = true;
116+
117+
// without this, the change to `updating` will be
118+
// grouped with the other changes, meaning it
119+
// won't be reflected in the UI
120+
await tick();
121+
122+
color = 'octarine';
123+
answer = 42;
124+
125+
await settled();
126+
127+
// any updates affected by `color` or `answer`
128+
// have now been applied
129+
updating = false;
130+
}
131+
```
132+
133+
## Error handling
134+
135+
Errors in `await` expressions will bubble to the nearest [error boundary](svelte-boundary).
136+
137+
## Caveats
138+
139+
As an experimental feature, the details of how `await` is handled (and related APIs like `$effect.pending()`) are subject to breaking changes outside of a semver major release, though we intend to keep such changes to a bare minimum.
140+
141+
Currently, server-side rendering is synchronous. If a `<svelte:boundary>` with a `pending` snippet is encountered during SSR, only the `pending` snippet will be rendered.
142+
143+
## Breaking changes
144+
145+
Effects run in a slightly different order when the `experimental.async` option is `true`. Specifically, _block_ effects like `{#if ...}` and `{#each ...}` now run before an `$effect.pre` or `beforeUpdate` in the same component, which means that in [very rare situations](/playground/untitled?#H4sIAAAAAAAAE22R3VLDIBCFX2WLvUhnTHsf0zre-Q7WmfwtFV2BgU1rJ5N3F0jaOuoVcPbw7VkYhK4_URTiGYkMnIyjDjLsFGO3EvdCKkIvipdB8NlGXxSCPt96snbtj0gctab2-J_eGs2oOWBE6VunLO_2es-EDKZ5x5ZhC0vPNWM2gHXGouNzAex6hHH1cPHil_Lsb95YT9VQX6KUAbS2DrNsBdsdDFHe8_XSYjH1SrhELTe3MLpsemajweiWVPuxHSbKNd-8eQTdE0EBf4OOaSg2hwNhhE_ABB_ulJzjj9FULvIcqgm5vnAqUB7wWFMfhuugQWkcAr8hVD-mq8D12kOep24J_IszToOXdveGDsuNnZwbJUNlXsKnhJdhUcTo42s41YpOSneikDV5HL8BktM6yRcCAAA=) it is possible to update a block that should no longer exist, but only if you update state inside an effect, [which you should avoid]($effect#When-not-to-use-$effect).

apps/svelte.dev/content/docs/svelte/05-special-elements/01-svelte-boundary.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,41 @@ title: <svelte:boundary>
1010
> [!NOTE]
1111
> This feature was added in 5.3.0
1212
13-
Boundaries allow you to guard against errors in part of your app from breaking the app as a whole, and to recover from those errors.
13+
Boundaries allow you to 'wall off' parts of your app, so that you can:
1414

15-
If an error occurs while rendering or updating the children of a `<svelte:boundary>`, or running any [`$effect`]($effect) functions contained therein, the contents will be removed.
15+
- provide UI that should be shown when [`await`](await-expressions) expressions are first resolving
16+
- handle errors that occur during rendering or while running effects, and provide UI that should be rendered when an error happens
1617

17-
Errors occurring outside the rendering process (for example, in event handlers or after a `setTimeout` or async work) are _not_ caught by error boundaries.
18+
If a boundary handles an error (with a `failed` snippet or `onerror` handler, or both) its existing content will be removed.
19+
20+
> [!NOTE] Errors occurring outside the rendering process (for example, in event handlers or after a `setTimeout` or async work) are _not_ caught by error boundaries.
1821
1922
## Properties
2023

21-
For the boundary to do anything, one or both of `failed` and `onerror` must be provided.
24+
For the boundary to do anything, one or more of the following must be provided.
25+
26+
### `pending`
27+
28+
As of Svelte 5.36, boundaries with a `pending` snippet can contain [`await`](await-expressions) expressions. This snippet will be shown when the boundary is first created, and will remain visible until all the `await` expressions inside the boundary have resolved ([demo](/playground/untitled#H4sIAAAAAAAAE21QQW6DQAz8ytY9BKQVpFdKkPqDHnorPWzAaSwt3tWugUaIv1eE0KpKD5as8YxnNBOw6RAKKOOAVrA4up5bEy6VGknOyiO3xJ8qMnmPAhpOZDFC8T6BXPyiXADQ258X77P1FWg4moj_4Y1jQZZ49W0CealqruXUcyPkWLVozQXbZDC2R606spYiNo7bqA7qab_fp2paFLUElD6wYhzVa3AdRUySgNHZAVN1qDZaLRHljTp0vSTJ9XJjrSbpX5f0eZXN6zLXXOa_QfmurIVU-moyoyH5ib87o7XuYZfOZe6vnGWmx1uZW7lJOq9upa-sMwuUZdkmmfIbfQ1xZwwaBL8ECgk9zh8axJAdiVsoTsZGnL8Bg4tX_OMBAAA=)):
29+
30+
```svelte
31+
<svelte:boundary>
32+
<p>{await delayed('hello!')}</p>
33+
34+
{#snippet pending()}
35+
<p>loading...</p>
36+
{/snippet}
37+
</svelte:boundary>
38+
```
39+
40+
The `pending` snippet will _not_ be shown for subsequent async updates — for these, you can use [`$effect.pending()`]($effect#$effect.pending).
41+
42+
> [!NOTE] In the [playground](/playground), your app is rendered inside a boundary with an empty pending snippet, so that you can use `await` without having to create one.
43+
2244

2345
### `failed`
2446

25-
If a `failed` snippet is provided, it will be rendered with the error that was thrown, and a `reset` function that recreates the contents ([demo](/playground/hello-world#H4sIAAAAAAAAE3VRy26DMBD8lS2tFCIh6JkAUlWp39Cq9EBg06CAbdlLArL87zWGKk8ORnhmd3ZnrD1WtOjFXqKO2BDGW96xqpBD5gXerm5QefG39mgQY9EIWHxueRMinLosti0UPsJLzggZKTeilLWgLGc51a3gkuCjKQ7DO7cXZotgJ3kLqzC6hmex1SZnSXTWYHcrj8LJjWTk0PHoZ8VqIdCOKayPykcpuQxAokJaG1dGybYj4gw4K5u6PKTasSbjXKgnIDlA8VvUdo-pzonraBY2bsH7HAl78mKSHZpgIcuHjq9jXSpZSLixRlveKYQUXhQVhL6GPobXAAb7BbNeyvNUs4qfRg3OnELLj5hqH9eQZqCnoBwR9lYcQxuVXeBzc8kMF8yXY4yNJ5oGiUzP_aaf_waTRGJib5_Ad3P_vbCuaYxzeNpbU0eUMPAOKh7Yw1YErgtoXyuYlPLzc10_xo_5A91zkQL_AgAA)):
47+
If a `failed` snippet is provided, it will be rendered when an error is thrown inside the boundary, with the `error` and a `reset` function that recreates the contents ([demo](/playground/hello-world#H4sIAAAAAAAAE3VRy26DMBD8lS2tFCIh6JkAUlWp39Cq9EBg06CAbdlLArL87zWGKk8ORnhmd3ZnrD1WtOjFXqKO2BDGW96xqpBD5gXerm5QefG39mgQY9EIWHxueRMinLosti0UPsJLzggZKTeilLWgLGc51a3gkuCjKQ7DO7cXZotgJ3kLqzC6hmex1SZnSXTWYHcrj8LJjWTk0PHoZ8VqIdCOKayPykcpuQxAokJaG1dGybYj4gw4K5u6PKTasSbjXKgnIDlA8VvUdo-pzonraBY2bsH7HAl78mKSHZpgIcuHjq9jXSpZSLixRlveKYQUXhQVhL6GPobXAAb7BbNeyvNUs4qfRg3OnELLj5hqH9eQZqCnoBwR9lYcQxuVXeBzc8kMF8yXY4yNJ5oGiUzP_aaf_waTRGJib5_Ad3P_vbCuaYxzeNpbU0eUMPAOKh7Yw1YErgtoXyuYlPLzc10_xo_5A91zkQL_AgAA)):
2648

2749
```svelte
2850
<svelte:boundary>

0 commit comments

Comments
 (0)