remote-functions
Use when building, auditing, or reviewing SvelteKit remote functions for validation, batching, and optimistic UI patterns
Installation
Suivez les instructions du dépôt pour votre agent. Consultez les fichiers et dépendances avant l’installation.
Voir la source ↗GuidesInstructions
Remote Functions
Quick Start
/// file: src/routes/counter/count.remote.ts
import * as v from 'valibot'
import { query, command } from '$app/server'
let count = 0
export const getCount = query(async () => count)
export const increment = command(
v.object({ delta: v.optional(v.number(), 1) }),
async ({ delta = 1 }) => {
count += delta
getCount().set(count) // keep cached query in sync without extra fetch
},
)
Trigger optimistic single-flight updates via
increment({ delta: 1 }).updates(getCount().withOverride((n = 0) => n + 1))
inside the component.
Core Principles
- Validate every argument with a Standard Schema; use
getRequestEventfor auth/cookies but never treatroute/params/urlas an authorization boundary. - Queries are cached promises — refresh,
set, orwithOverridethem explicitly from commands/forms to prevent stale UI and redundant roundtrips.
Common Patterns
Single-flight optimistic mutations
Pair a command/form with its sibling query: perform the
mutation, then call query.refresh()/set() or
.updates(query.withOverride(...)) so the UI updates once per
request, even with high latency.
Reference Files
For detailed documentation, see:
Notes
- Prefer
formfor progressive enhancement; reservecommandfor imperative or cross-component workflows. - Use
query.batchfor n+1 scenarios (e.g., weather demo) andprerender({ inputs, dynamic })for data that should be cached at build time yet remain opt-in for runtime refreshes.
Exemples
Consultez les exemples du dépôt source.
Voir la source ↗Historique source
- Date d’indexation
- 2026-01-20
- Mise à jour de l’index
- 2026-01-26
- Branch
- main
Les dates concernent ce répertoire. Consultez le dépôt pour l’historique du code.
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!