Remote Functions
Choose Query, Command, Form, or Prerender for server work.
Remote functions are SvelteKit server entry points wrapped in Effect. They keep server code on the server while giving clients typed inputs, typed failures, and predictable pending states.
When to use this
Use a remote function when the work needs a database, secret, cookie, session, server service, or build-time data source. Keep purely local UI behavior in component effects.
Minimal working example
import { Query } from "svelte-effect-runtime/server";
import { Effect, Schema } from "effect";
export const get_count = Query(Schema.Struct({ key: Schema.String }), ({ key }) =>
Effect.succeed({ key, value: 1 }),
);<script lang="ts" effect>
import { get_count } from "./counter.remote";
const count = get_count({ key: "visits" });
</script>
{#await yield* count}
<p>Loading...</p>
{:then result}
<p>{result.value}</p>
{/await}Realistic variant
Pick the shape that matches the user intent:
Query
Read server data without changing it.
Query.batch
Batch repeated lookups discovered during render.
Query.live
Stream changing server data into the client.
Command
Run a mutation from a button, menu item, or effect.
Form
Submit progressive forms with validation and field errors.
Prerender
Read build-time or static data with optional dynamic fallback.
Decision guide
- Use
Querywhen the UI needs server data without changing it. - Use
Query.batchwhen many small same-kind reads should become one server call. - Use
Query.livewhen server data should keep streaming while the UI is connected. - Use
Commandwhen the user does something that changes server state. - Use
Formwhen HTML form semantics, validation, and progressive enhancement matter. - Use
Prerenderwhen data can be resolved at build time or cached as static output.
Common mistakes
- Using
Queryfor writes because it is convenient to call. - Returning unstructured thrown errors instead of typed failures.
- Reading cookies or locals from component code instead of the server request context.
- Forgetting to enable SvelteKit's remote function flag.