SvelteKit Remote Functions
Current Status
Remote functions are experimental in SvelteKit 2.58. Enable them in
svelte.config.js:
export default {
kit: { experimental: { remoteFunctions: true } },
compilerOptions: { experimental: { async: true } } // only for await in components
};
Quick Start
File naming: export remote functions from *.remote.ts or *.remote.js.
Remote files can live anywhere under src except src/lib/server.
Which function?
- Dynamic reads →
query()
- Progressive forms →
form()
- Event-handler mutations →
command()
- Build-time/static reads →
prerender()
Example
// posts.remote.ts
import { command, query, requested } from '$app/server';
import * as v from 'valibot';
export const getPosts = query(v.object({ tag: v.optional(v.string()) }), async (filter) => {
return db.posts.find(filter);
});
export const createPost = command(v.object({ title: v.string() }), async (data) => {
await db.posts.create(data);
for (const { query } of requested(getPosts, 5)) {
void query.refresh();
}
});
Client:
<script lang="ts">
import { createPost, getPosts } from './posts.remote';
const posts = $derived(await getPosts({ tag: 'svelte' }));
</script>
<button => createPost({ title: 'New' }).updates(getPosts)}>
Create
</button>
Current Rules
- Remote functions always run on the server, even when called from the browser.
- Args/returns use
devalue; avoid functions, class instances, symbols, circular refs, and RegExp.
- Validate exposed inputs with Standard Schema (
valibot, zod, arktype, etc.) or use .unchecked/'unchecked' deliberately.
query.batch() batches calls from the same macrotask to solve n+1 reads.
form().enhance() submit() returns true when submission is valid/successful and false for validation failures.
.updates() is client-requested; server handlers must opt in with requested(queryFn, limit).
requested() now yields { arg, query }; call query.refresh()/query.set(...) on the bound instance.
limit is required for requested() to cap client-controlled refresh requests.
- Inside command/form handlers, use
void query.refresh()/void query.set(value); SvelteKit awaits and serializes the updates.
- Prefer
form() over command() where progressive enhancement matters.
- Use
prerender() for data that changes at most once per deployment.
- Last verified: SvelteKit 2.58.0, 2026-04-24
Reference Files
- references/remote-functions.md - Current patterns, examples, and gotchas
1---
2name: sveltekit-remote-functions
3# IMPORTANT: Keep description on ONE line for agent compatibility
4# prettier-ignore
5description: "SvelteKit remote functions guidance. Use for query(), form(), command(), and prerender() patterns in .remote.ts files."
6metadata:
7 source: https://skillmd.com/skills/spences10/sveltekit-remote-functions
8---
9
10# SvelteKit Remote Functions
11
12## Current Status
13
14Remote functions are **experimental** in SvelteKit 2.58. Enable them in
15`svelte.config.js`:
16
17```js
18export default {
19 kit: { experimental: { remoteFunctions: true } },
20 compilerOptions: { experimental: { async: true } } // only for await in components
21};
22```
23
24## Quick Start
25
26**File naming:** export remote functions from `*.remote.ts` or `*.remote.js`.
27Remote files can live anywhere under `src` except `src/lib/server`.
28
29**Which function?**
30
31- Dynamic reads → `query()`
32- Progressive forms → `form()`
33- Event-handler mutations → `command()`
34- Build-time/static reads → `prerender()`
35
36## Example
37
38```ts
39// posts.remote.ts
40import { command, query, requested } from '$app/server';
41import * as v from 'valibot';
42
43export const getPosts = query(v.object({ tag: v.optional(v.string()) }), async (filter) => {
44 return db.posts.find(filter);
45});
46
47export const createPost = command(v.object({ title: v.string() }), async (data) => {
48 await db.posts.create(data);
49
50 for (const { query } of requested(getPosts, 5)) {
51 void query.refresh();
52 }
53});
54```
55
56Client:
57
58```svelte
59<script lang="ts">
60 import { createPost, getPosts } from './posts.remote';
61
62 const posts = $derived(await getPosts({ tag: 'svelte' }));
63</script>
64
65<button onclick={() => createPost({ title: 'New' }).updates(getPosts)}>
66 Create
67</button>
68```
69
70## Current Rules
71
72- Remote functions always run on the server, even when called from the browser.
73- Args/returns use `devalue`; avoid functions, class instances, symbols, circular refs, and `RegExp`.
74- Validate exposed inputs with Standard Schema (`valibot`, `zod`, `arktype`, etc.) or use `.unchecked`/`'unchecked'` deliberately.
75- `query.batch()` batches calls from the same macrotask to solve n+1 reads.
76- `form().enhance()` `submit()` returns `true` when submission is valid/successful and `false` for validation failures.
77- `.updates()` is client-requested; server handlers must opt in with `requested(queryFn, limit)`.
78- `requested()` now yields `{ arg, query }`; call `query.refresh()`/`query.set(...)` on the bound instance.
79- `limit` is required for `requested()` to cap client-controlled refresh requests.
80- Inside command/form handlers, use `void query.refresh()`/`void query.set(value)`; SvelteKit awaits and serializes the updates.
81- Prefer `form()` over `command()` where progressive enhancement matters.
82- Use `prerender()` for data that changes at most once per deployment.
83- **Last verified:** SvelteKit 2.58.0, 2026-04-24
84
85## Reference Files
86
87- [references/remote-functions.md](references/remote-functions.md) - Current patterns, examples, and gotchas
88
89<!--
90PROGRESSIVE DISCLOSURE GUIDELINES:
91- Keep this file ~50 lines total (max ~150 lines)
92- Use 1-2 code blocks only (recommend 1)
93- Keep description <200 chars for Level 1 efficiency
94- Move detailed docs to references/ for Level 3 loading
95- This is Level 2 - quick reference ONLY, not a manual
96
97LLM WORKFLOW (when editing this file):
981. Write/edit SKILL.md
992. Format (if formatter available)
1003. Run: npx skills add . --list
1014. If the skill is not discovered, check SKILL.md frontmatter formatting
1025. Validate again to confirm
103-->