Skip to main content

$app/server

import {
	function command<Output>(fn: () => 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.

@since
2.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.

@since
2.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).

@since
2.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.

@since
2.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.

@since
2.27
query
,
function read(asset: string): Response

Read the contents of an imported asset from the filesystem

@example
import { read } from '$app/server';
import somefile from './somefile.txt';

const asset = read(somefile);
const text = await asset.text();
@since
2.4.0
read
,
function requested<Input, Output>(query: RemoteQueryFunction<Input, Output>, limit?: number): RequestedResult<Input>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { function requested<Input, Output>(query: RemoteQueryFunction<Input, Output>, limit?: number): RequestedResult<Input>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { requested } from '$app/server';

for (const arg of requested(getPost, 5)) {
	// it's safe to throw away this promise -- SvelteKit
	// will await it for us and handle any errors by sending
	// them to the client.
	void getPost(arg).refresh();
}

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
} from '$app/server';
for (const const arg: unknownarg of requested<unknown, unknown>(query: RemoteQueryFunction<unknown, unknown>, limit?: number): RequestedResult<unknown>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { requested } from '$app/server';

for (const arg of requested(getPost, 5)) {
	// it's safe to throw away this promise -- SvelteKit
	// will await it for us and handle any errors by sending
	// them to the client.
	void getPost(arg).refresh();
}

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
(getPost, 5)) {
// it's safe to throw away this promise -- SvelteKit // will await it for us and handle any errors by sending // them to the client. void getPost(const arg: unknownarg).refresh(); }

As a shorthand for the above, you can also call refreshAll on the result:

import { function requested<Input, Output>(query: RemoteQueryFunction<Input, Output>, limit?: number): RequestedResult<Input>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { requested } from '$app/server';

for (const arg of requested(getPost, 5)) {
	// it's safe to throw away this promise -- SvelteKit
	// will await it for us and handle any errors by sending
	// them to the client.
	void getPost(arg).refresh();
}

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
} from '$app/server';
await requested<unknown, unknown>(query: RemoteQueryFunction<unknown, unknown>, limit?: number): RequestedResult<unknown>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { function requested<Input, Output>(query: RemoteQueryFunction<Input, Output>, limit?: number): RequestedResult<Input>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { requested } from '$app/server';

for (const arg of requested(getPost, 5)) {
	// it's safe to throw away this promise -- SvelteKit
	// will await it for us and handle any errors by sending
	// them to the client.
	void getPost(arg).refresh();
}

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
} from '$app/server';
for (const const arg: unknownarg of requested<unknown, unknown>(query: RemoteQueryFunction<unknown, unknown>, limit?: number): RequestedResult<unknown>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { requested } from '$app/server';

for (const arg of requested(getPost, 5)) {
	// it's safe to throw away this promise -- SvelteKit
	// will await it for us and handle any errors by sending
	// them to the client.
	void getPost(arg).refresh();
}

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
(getPost, 5)) {
// it's safe to throw away this promise -- SvelteKit // will await it for us and handle any errors by sending // them to the client. void getPost(const arg: unknownarg).refresh(); }

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
(getPost, 5).refreshAll: () => Promise<void>

Call refresh on all queries selected by this requested invocation. This is identical to:

import { requested } from '$app/server';

for await (const arg of requested(query, ...) {
  void query(arg).refresh();
}
refreshAll
();
requested
} from '$app/server';

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.

function command<Output>(
	fn: () => Output
): RemoteCommand<void, Output>;
function command<Input, Output>(
	validate: 'unchecked',
	fn: (arg: Input) => Output
): RemoteCommand<Input, Output>;
function command<Schema extends StandardSchemaV1, Output>(
	validate: Schema,
	fn: (arg: StandardSchemaV1.InferOutput<Schema>) => 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.

function form<Output>(
	fn: () => MaybePromise<Output>
): RemoteForm<void, Output>;
function form<Input extends RemoteFormInput, Output>(
	validate: 'unchecked',
	fn: (
		data: Input,
		issue: InvalidField<Input>
	) => MaybePromise<Output>
): RemoteForm<Input, Output>;
function form<
	Schema extends StandardSchemaV1<
		RemoteFormInput,
		Record<string, any>
	>,
	Output
>(
	validate: Schema,
	fn: (
		data: StandardSchemaV1.InferOutput<Schema>,
		issue: InvalidField<StandardSchemaV1.InferInput<Schema>>
	) => MaybePromise<Output>
): RemoteForm<StandardSchemaV1.InferInput<Schema>, Output>;

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).

function getRequestEvent(): RequestEvent;

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.

function prerender<Output>(
	fn: () => MaybePromise<Output>,
	options?:
		| {
				inputs?: RemotePrerenderInputsGenerator<void>;
				dynamic?: boolean;
		  }
		| undefined
): RemotePrerenderFunction<void, Output>;
function prerender<Input, Output>(
	validate: 'unchecked',
	fn: (arg: Input) => MaybePromise<Output>,
	options?:
		| {
				inputs?: RemotePrerenderInputsGenerator<Input>;
				dynamic?: boolean;
		  }
		| undefined
): RemotePrerenderFunction<Input, Output>;
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.

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

read

Available since 2.4.0

Read the contents of an imported asset from the filesystem

import { function read(asset: string): Response

Read the contents of an imported asset from the filesystem

@example
import { read } from '$app/server';
import somefile from './somefile.txt';

const asset = read(somefile);
const text = await asset.text();
@since
2.4.0
read
} from '$app/server';
import const somefile: stringsomefile from './somefile.txt'; const const asset: Responseasset = function read(asset: string): Response

Read the contents of an imported asset from the filesystem

@example
import { read } from '$app/server';
import somefile from './somefile.txt';

const asset = read(somefile);
const text = await asset.text();
@since
2.4.0
read
(const somefile: stringsomefile);
const const text: stringtext = await const asset: Responseasset.Body.text(): Promise<string>text();
function read(asset: string): Response;

requested

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

import { function requested<Input, Output>(query: RemoteQueryFunction<Input, Output>, limit?: number): RequestedResult<Input>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { requested } from '$app/server';

for (const arg of requested(getPost, 5)) {
	// it's safe to throw away this promise -- SvelteKit
	// will await it for us and handle any errors by sending
	// them to the client.
	void getPost(arg).refresh();
}

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
} from '$app/server';
for (const const arg: unknownarg of requested<unknown, unknown>(query: RemoteQueryFunction<unknown, unknown>, limit?: number): RequestedResult<unknown>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { requested } from '$app/server';

for (const arg of requested(getPost, 5)) {
	// it's safe to throw away this promise -- SvelteKit
	// will await it for us and handle any errors by sending
	// them to the client.
	void getPost(arg).refresh();
}

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
(getPost, 5)) {
// it's safe to throw away this promise -- SvelteKit // will await it for us and handle any errors by sending // them to the client. void getPost(const arg: unknownarg).refresh(); }

As a shorthand for the above, you can also call refreshAll on the result:

import { function requested<Input, Output>(query: RemoteQueryFunction<Input, Output>, limit?: number): RequestedResult<Input>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { requested } from '$app/server';

for (const arg of requested(getPost, 5)) {
	// it's safe to throw away this promise -- SvelteKit
	// will await it for us and handle any errors by sending
	// them to the client.
	void getPost(arg).refresh();
}

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
} from '$app/server';
await requested<unknown, unknown>(query: RemoteQueryFunction<unknown, unknown>, limit?: number): RequestedResult<unknown>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { function requested<Input, Output>(query: RemoteQueryFunction<Input, Output>, limit?: number): RequestedResult<Input>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { requested } from '$app/server';

for (const arg of requested(getPost, 5)) {
	// it's safe to throw away this promise -- SvelteKit
	// will await it for us and handle any errors by sending
	// them to the client.
	void getPost(arg).refresh();
}

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
} from '$app/server';
for (const const arg: unknownarg of requested<unknown, unknown>(query: RemoteQueryFunction<unknown, unknown>, limit?: number): RequestedResult<unknown>

In the context of a remote command or form request, returns an iterable of the client-requested refreshes' validated arguments up to the supplied limit. Arguments that fail validation or exceed the limit are recorded as failures in the response to the client.

@example
import { requested } from '$app/server';

for (const arg of requested(getPost, 5)) {
	// it's safe to throw away this promise -- SvelteKit
	// will await it for us and handle any errors by sending
	// them to the client.
	void getPost(arg).refresh();
}

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
(getPost, 5)) {
// it's safe to throw away this promise -- SvelteKit // will await it for us and handle any errors by sending // them to the client. void getPost(const arg: unknownarg).refresh(); }

As a shorthand for the above, you can also call refreshAll on the result:

import { requested } from '$app/server';

await requested(getPost, 5).refreshAll();
requested
(getPost, 5).refreshAll: () => Promise<void>

Call refresh on all queries selected by this requested invocation. This is identical to:

import { requested } from '$app/server';

for await (const arg of requested(query, ...) {
  void query(arg).refresh();
}
refreshAll
();
function requested<Input, Output>(
	query: RemoteQueryFunction<Input, Output>,
	limit?: number
): RequestedResult<Input>;

query

namespace query {
	/**
	 * Creates a batch query function that collects multiple calls and executes them in a single request
	 *
	 * See [Remote functions](https://svelte.dev/docs/kit/remote-functions#query.batch) for full documentation.
	 *
	 * @since 2.35
	 */
	function batch<Input, Output>(
		validate: 'unchecked',
		fn: (
			args: Input[]
		) => MaybePromise<(arg: Input, idx: number) => Output>
	): RemoteQueryFunction<Input, Output>;
	/**
	 * Creates a batch query function that collects multiple calls and executes them in a single request
	 *
	 * See [Remote functions](https://svelte.dev/docs/kit/remote-functions#query.batch) for full documentation.
	 *
	 * @since 2.35
	 */
	function batch<Schema extends StandardSchemaV1, Output>(
		schema: Schema,
		fn: (
			args: StandardSchemaV1.InferOutput<Schema>[]
		) => MaybePromise<
			(
				arg: StandardSchemaV1.InferOutput<Schema>,
				idx: number
			) => Output
		>
	): RemoteQueryFunction<
		StandardSchemaV1.InferInput<Schema>,
		Output
	>;
}

Edit this page on GitHub llms.txt

previous next