SvelteKit • Reference

$app/server

On this page
import {
	function command<Output>(fn: () => MaybePromise<Output>): RemoteCommand<void, Output> (+2 overloads)

Creates a remote command. When called from the browser, the function will be invoked on the server via a fetch call.

See Remote functions for full documentation.

@since2.27
function command<Output>(fn: () => MaybePromise<Output>): RemoteCommand<void, Output> (+2 overloads)

Creates a remote command. When called from the browser, the function will be invoked on the server via a fetch call.

See Remote functions for full documentation.

@since2.27
command
,
function form<Output>(fn: () => MaybePromise<Output>): RemoteForm<void, Output> (+2 overloads)

Creates a form object that can be spread onto a <form> element.

See Remote functions for full documentation.

@since2.27
function form<Output>(fn: () => MaybePromise<Output>): RemoteForm<void, Output> (+2 overloads)

Creates a form object that can be spread onto a <form> element.

See Remote functions for full documentation.

@since2.27
form
,
function getRequestEvent(): RequestEvent

Returns the current RequestEvent. Can be used inside server hooks, server load functions, actions, and endpoints (and functions called by them).

In environments without AsyncLocalStorage, this must be called synchronously (i.e. not after an await).

@since2.20.0
function getRequestEvent(): RequestEvent

Returns the current RequestEvent. Can be used inside server hooks, server load functions, actions, and endpoints (and functions called by them).

In environments without AsyncLocalStorage, this must be called synchronously (i.e. not after an await).

@since2.20.0
getRequestEvent
,
function prerender<Output>(fn: () => MaybePromise<Output>, options?: {
    inputs?: RemotePrerenderInputsGenerator<void>;
    dynamic?: boolean;
} | undefined): RemotePrerenderFunction<void, Output> (+2 overloads)

Creates a remote prerender function. When called from the browser, the function will be invoked on the server via a fetch call.

See Remote functions for full documentation.

@since2.27
function prerender<Output>(fn: () => MaybePromise<Output>, options?: {
    inputs?: RemotePrerenderInputsGenerator<void>;
    dynamic?: boolean;
} | undefined): RemotePrerenderFunction<void, Output> (+2 overloads)

Creates a remote prerender function. When called from the browser, the function will be invoked on the server via a fetch call.

See Remote functions for full documentation.

@since2.27
prerender
,
function query<Output>(fn: () => MaybePromise<Output>): RemoteQueryFunction<void, Output> (+2 overloads)

Creates a remote query. When called from the browser, the function will be invoked on the server via a fetch call.

See Remote functions for full documentation.

@since2.27
function query<Output>(fn: () => MaybePromise<Output>): RemoteQueryFunction<void, Output> (+2 overloads)

Creates a remote query. When called from the browser, the function will be invoked on the server via a fetch call.

See Remote functions for full documentation.

@since2.27
query
,
function read(asset: string): Response

Read the contents of an imported asset from the filesystem

@example```js import { read } from '$app/server'; import somefile from './somefile.txt'; const asset = read(somefile); const text = await asset.text(); ```@since2.4.0
function read(asset: string): Response

Read the contents of an imported asset from the filesystem

@example```js import { read } from '$app/server'; import somefile from './somefile.txt'; const asset = read(somefile); const text = await asset.text(); ```@since2.4.0
read
,
function requested<Input, Output, Validated = Input>(query: RemoteQueryFunction<Input, Output, Validated>, limit: number): QueryRequestedResult<Validated, Output> (+1 overload)

Inside a remote command or form callback, returns an iterable of { arg, query } entries for the query instances the client asked to refresh, up to the supplied limit. Each query is a RemoteQuery bound to the original client-side cache key, so refresh() / set() propagate correctly even when the query's schema transforms the input. arg is the validated argument, i.e. the value after the schema has run (so InferOutput<Schema> for queries declared with a Standard Schema).

Arguments that fail validation or exceed limit are recorded as failures in the response to the client. See Client-requested refreshes for usage in a remote command or form.

@example```ts import { requested } from '$app/server'; for (const { arg, query } of requested(getPost, 5)) { // `arg` is the validated argument; `query` is bound to the client's // cache key. It's safe to throw away this promise -- SvelteKit will // await it and forward any errors to the client. void query.refresh(); } ``` As a shorthand for the above, you can also call `refreshAll` on the result:@example```ts import { requested } from '$app/server'; await requested(getPost, 5).refreshAll(); ``` Works with `query.batch` as well — refreshes for individual entries are collected into a single batched call. For live queries, the same applies, but with `reconnect` and `reconnectAll`.
function requested<Input, Output, Validated = Input>(query: RemoteQueryFunction<Input, Output, Validated>, limit: number): QueryRequestedResult<Validated, Output> (+1 overload)

Inside a remote command or form callback, returns an iterable of { arg, query } entries for the query instances the client asked to refresh, up to the supplied limit. Each query is a RemoteQuery bound to the original client-side cache key, so refresh() / set() propagate correctly even when the query's schema transforms the input. arg is the validated argument, i.e. the value after the schema has run (so InferOutput<Schema> for queries declared with a Standard Schema).

Arguments that fail validation or exceed limit are recorded as failures in the response to the client. See Client-requested refreshes for usage in a remote command or form.

@example```ts import { requested } from '$app/server'; for (const { arg, query } of requested(getPost, 5)) { // `arg` is the validated argument; `query` is bound to the client's // cache key. It's safe to throw away this promise -- SvelteKit will // await it and forward any errors to the client. void query.refresh(); } ``` As a shorthand for the above, you can also call `refreshAll` on the result:@example```ts import { requested } from '$app/server'; await requested(getPost, 5).refreshAll(); ``` Works with `query.batch` as well — refreshes for individual entries are collected into a single batched call. For live queries, the same applies, but with `reconnect` and `reconnectAll`.
requested
} from '$app/server';

read

Available since 2.4.0

Read the contents of an imported asset from the filesystem

export function read(asset: string): Response;

getRequestEvent

Available since 2.20.0

Returns the current RequestEvent. Can be used inside server hooks, server load functions, actions, and endpoints (and functions called by them).

In environments without AsyncLocalStorage, this must be called synchronously (i.e. not after an await).

export function getRequestEvent(): RequestEvent;

command

Available since 2.27

Creates a remote command. When called from the browser, the function will be invoked on the server via a fetch call.

See Remote functions for full documentation.

export function command<Output>(fn: () => MaybePromise<Output>): RemoteCommand<void, Output>;
export function command<Input, Output>(validate: "unchecked", fn: (arg: Input) => MaybePromise<Output>): RemoteCommand<Input, Output>;
export function command<Schema extends StandardSchemaV1, Output>(validate: Schema, fn: (arg: StandardSchemaV1.InferOutput<Schema>) => MaybePromise<Output>): RemoteCommand<StandardSchemaV1.InferInput<Schema>, Output>;

form

Available since 2.27

Creates a form object that can be spread onto a <form> element.

See Remote functions for full documentation.

export function form<Output>(fn: () => MaybePromise<Output>): RemoteForm<void, Output>;
export function form<Input extends RemoteFormInput, Output>(validate: "unchecked", fn: (data: Input, issue: InvalidField<Input>) => MaybePromise<Output>): RemoteForm<Input, Output>;
export function form<Schema extends StandardSchemaV1<RemoteFormInput, Record<string, any>>, Output>(validate: true extends HasNonOptionalBoolean<StandardSchemaV1.InferInput<Schema>> ? "Error: All booleans in form schemas must be optional (e.g. `v.optional(v.boolean(), false)`) because checkbox inputs do not send a false value when unchecked." : Schema, fn: (data: StandardSchemaV1.InferOutput<Schema>, issue: InvalidField<StandardSchemaV1.InferInput<Schema>>) => MaybePromise<Output>): RemoteForm<StandardSchemaV1.InferInput<Schema>, Output>;

prerender

Available since 2.27

Creates a remote prerender function. When called from the browser, the function will be invoked on the server via a fetch call.

See Remote functions for full documentation.

export function prerender<Output>(fn: () => MaybePromise<Output>, options?: {
		inputs?: RemotePrerenderInputsGenerator<void>;
		dynamic?: boolean;
	} | undefined): RemotePrerenderFunction<void, Output>;
export function prerender<Input, Output>(validate: "unchecked", fn: (arg: Input) => MaybePromise<Output>, options?: {
		inputs?: RemotePrerenderInputsGenerator<Input>;
		dynamic?: boolean;
	} | undefined): RemotePrerenderFunction<Input, Output>;
export function prerender<Schema extends StandardSchemaV1, Output>(schema: Schema, fn: (arg: StandardSchemaV1.InferOutput<Schema>) => MaybePromise<Output>, options?: {
		inputs?: RemotePrerenderInputsGenerator<StandardSchemaV1.InferInput<Schema>>;
		dynamic?: boolean;
	} | undefined): RemotePrerenderFunction<StandardSchemaV1.InferInput<Schema>, Output>;

query

Available since 2.27

Creates a remote query. When called from the browser, the function will be invoked on the server via a fetch call.

See Remote functions for full documentation.

export function query<Output>(fn: () => MaybePromise<Output>): RemoteQueryFunction<void, Output>;
export function query<Input, Output>(validate: "unchecked", fn: (arg: Input) => MaybePromise<Output>): RemoteQueryFunction<Input, Output>;
export function query<Schema extends StandardSchemaV1, Output>(schema: Schema, fn: (arg: StandardSchemaV1.InferOutput<Schema>) => MaybePromise<Output>): RemoteQueryFunction<StandardSchemaV1.InferInput<Schema>, Output, StandardSchemaV1.InferOutput<Schema>>;

requested

Inside a remote command or form callback, returns an iterable of { arg, query } entries for the query instances the client asked to refresh, up to the supplied limit. Each query is a RemoteQuery bound to the original client-side cache key, so refresh() / set() propagate correctly even when the query's schema transforms the input. arg is the validated argument, i.e. the value after the schema has run (so InferOutput<Schema> for queries declared with a Standard Schema).

Arguments that fail validation or exceed limit are recorded as failures in the response to the client. See Client-requested refreshes for usage in a remote command or form.

export function requested<Input, Output, Validated = Input>(query: RemoteQueryFunction<Input, Output, Validated>, limit: number): QueryRequestedResult<Validated, Output>;
export function requested<Input, Output, Validated = Input>(query: RemoteLiveQueryFunction<Input, Output, Validated>, limit: number): LiveQueryRequestedResult<Validated, Output>;