Available since SvelteKit 2.27. This feature is experimental and subject to change without notice.
Enabling remote functions
You must opt in by adding thekit.experimental.remoteFunctions option in your svelte.config.js:
Overview
Remote functions are exported from a.remote.js or .remote.ts file and come in four flavors:
query
Read dynamic data from the server
form
Submit form data with progressive enhancement
command
Write data to the server from anywhere
prerender
Prerender static data at build time
On the client, exported functions are transformed to
fetch wrappers that invoke their counterparts on the server via a generated HTTP endpoint.query
Thequery function allows you to read dynamic data from the server:
Query arguments
Query functions can accept an argument. Use a Standard Schema validation library like Zod or Valibot to validate:Refreshing queries
Any query can be re-fetched via itsrefresh method:
Queries are cached while they’re on the page, meaning
getPosts() === getPosts(). You don’t need a reference to update the query.query.batch
query.batch batches requests that happen within the same macrotask, solving the n+1 problem:
form
Theform function makes it easy to write data to the server:
The form object contains
method and action properties that allow it to work without JavaScript. It also has an attachment that progressively enhances the form when JavaScript is available.Fields
A form is composed of fields defined by the schema. To get attributes for a field, call its.as(...) method:
Supported field types
Supported field types
Fields can be nested in objects and arrays, with values as:
- Strings
- Numbers
- Booleans
- File objects
Validation
If submitted data doesn’t pass the schema, each invalid field’sissues() method returns error objects:
Single-flight mutations
Specify which queries should be refreshed in response to a form submission:enhance
Customize form submission behavior:command
Thecommand function allows you to write data to the server from anywhere (unlike form, which is element-specific):
Updating queries
Tell SvelteKit which queries need to be refreshed:- Inside command
- When calling
withOverride for optimistic updates:
prerender
Theprerender function is similar to query, except it’s invoked at build time:
Use this for data that changes at most once per redeployment. You can use
prerender functions on pages that are otherwise dynamic, allowing for partial prerendering.Prerender arguments
Specify which values should be prerendered using theinputs option:
dynamic: true to change this:
Using getRequestEvent
Inside remote functions, usegetRequestEvent to get the current RequestEvent object:
Next steps
Routing
Learn about SvelteKit’s routing system
Load functions
Fetch data for your pages