You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/svelte.dev/src/routes/llms-small.txt/content-sveltekit.md
+230Lines changed: 230 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -441,6 +441,236 @@ Use it with a simple form:
441
441
</form>
442
442
```
443
443
444
+
## Remote functions (experimental)
445
+
446
+
-**What they are**: Type-safe server-only functions you call from the client. They always execute on the server, so they can access server-only modules (env, DB).
447
+
- If you choose to use them you can replace load functions and form actions with them.
448
+
- Works best in combination with asynchronous Svelte, i.e. using `await` expressions in `$derived` and template
449
+
-**Opt-in**: Enable in `svelte.config.js`:
450
+
451
+
```js
452
+
exportdefault {
453
+
kit: {
454
+
experimental: {
455
+
remoteFunctions:true
456
+
}
457
+
}
458
+
};
459
+
```
460
+
461
+
-**Where and how**:
462
+
- Place `.remote.js`/`.remote.ts` files in `src/lib` or `src/routes`.
463
+
- Export functions using one of: `query`, `form`, `command`, `prerender` from `$app/server`.
464
+
- Client imports become fetch-wrappers to generated HTTP endpoints.
465
+
- Arguments/returns are serialized with devalue (supports Date, Map, custom transport).
466
+
467
+
### query: read dynamic data
468
+
469
+
Define:
470
+
471
+
```js
472
+
// src/routes/blog/data.remote.js
473
+
import { query } from'$app/server';
474
+
import*asdbfrom'$lib/server/database';
475
+
476
+
exportconstgetPosts=query(async () => {
477
+
returndb.posts();
478
+
});
479
+
```
480
+
481
+
Use in component (recommended with await):
482
+
483
+
```svelte
484
+
<script>
485
+
import { getPosts } from './data.remote';
486
+
</script>
487
+
488
+
<ul>
489
+
{#each await getPosts() as { title, slug }}
490
+
<li><a href="/blog/{slug}">{title}</a></li>
491
+
{/each}
492
+
</ul>
493
+
```
494
+
495
+
-**Args + validation**: Pass a Standard Schema (e.g. Valibot/Zod) as first param.
0 commit comments