Skip to content

Commit 6635c55

Browse files
github-actions[bot]svelte-docs-bot[bot]Ocean-OS
authored
Sync kit docs (#1950)
sync kit docs Co-authored-by: svelte-docs-bot[bot] <196124396+svelte-docs-bot[bot]@users.noreply.github.com> Co-authored-by: ComputerGuy <[email protected]>
1 parent 0a969bf commit 6635c55

5 files changed

Lines changed: 121 additions & 37 deletions

File tree

apps/svelte.dev/content/docs/kit/20-core-concepts/60-remote-functions.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,8 @@ export const createPost = form(
10201020
async (data) => {
10211021
// form logic goes here...
10221022

1023-
+++for (const arg of requested(getPosts, 1)) {+++
1024-
+++ void getPosts(arg).refresh();+++
1023+
+++for (const { query } of requested(getPosts, 1)) {+++
1024+
+++ void query.refresh();+++
10251025
+++}+++
10261026

10271027
// Redirect to the newly created page
@@ -1030,7 +1030,9 @@ export const createPost = form(
10301030
);
10311031
```
10321032
1033-
`requested` gives you access to the requested query arguments for the supplied query. It returns the *parsed* arguments for the query -- when these arguments are passed back into the query in `getPosts(arg).refresh()`, they will not be parsed again. If parsing an argument fails, that query will error, but the entire command will not fail. `requested`'s second parameter, `limit`, is the maximum number of items it will return. Any refresh requests beyond this limit will fail.
1033+
`requested` gives you access to the queries the client requested to refresh. Each entry is an `{ arg, query }` object: `arg` is the value the query's implementation function received — i.e. the argument *after* the schema has validated and (where applicable) transformed it — and `query` is a `RemoteQuery` already bound to the client's original cache key, so calling `query.refresh()` / `query.set(...)` updates the correct client instance. If parsing an argument fails, that query will error, but the entire command will not fail. `requested`'s second parameter, `limit`, is the maximum number of items it will return. Any refresh requests beyond this limit will fail.
1034+
1035+
> [!NOTE] `limit` is required because the list of refresh requests is controlled by the client — each entry causes the server to validate an argument and usually re-fetch data, so an unbounded list is a denial-of-service risk. Choose a limit that reflects the worst case you're willing to handle per request. You _can_ pass `Infinity` if you have explicitly decided to accept any number of refreshes, but it is not recommended.
10341036
10351037
Additionally, `requested` allows a simple shorthand when all you want to do is refresh the requested query instances:
10361038
@@ -1039,10 +1041,15 @@ import type { RemoteQueryFunction } from '@sveltejs/kit';
10391041
import { requested } from '$app/server';
10401042
declare const getPosts: RemoteQueryFunction<any, any>;
10411043
// ---cut---
1042-
// this is the same as looping over the result and calling `void getPosts(arg).refresh()`.
1044+
// this is the same as looping over the result and calling `void query.refresh()`.
10431045
await requested(getPosts, 1).refreshAll();
10441046
```
10451047
1048+
> [!NOTE] Why does the command have to name every query it's willing to refresh? Two reasons:
1049+
>
1050+
> - **Bundle size.** If a command could implicitly refresh *any* query in your app, SvelteKit would have to include every query's code in the command's server bundle, because it can't know ahead of time which ones will be called.
1051+
> - **Denial-of-service.** Any malicious user can inspect their network tab to discover which queries your app uses, then POST a command with a client-supplied list of thousands of refreshes. The only defence is for the server handler to declare which queries it is willing to refresh — and in what quantity (hence the required `limit`).
1052+
10461053
## prerender
10471054
10481055
The `prerender` function is similar to `query`, except that it will be invoked at build time to prerender the result. Use this for data that changes at most once per redeployment.

apps/svelte.dev/content/docs/kit/98-reference/[email protected]

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,10 +2687,23 @@ type RemoteQuery<T> = RemoteResource<T> & {
26872687

26882688
The return value of a remote `query` function. See [Remote functions](/docs/kit/remote-functions#query) for full documentation.
26892689

2690+
The optional `Validated` generic parameter represents the argument type *after* the
2691+
query's schema has validated and (optionally) transformed it — this is the type the
2692+
query's implementation function receives on the server, and the type yielded by
2693+
[`requested`](/docs/kit/$app-server#requested). For queries declared
2694+
with [Standard Schema](https://standardschema.dev/) it differs from `Input` when the
2695+
schema contains a transform (e.g. `v.pipe(v.number(), v.transform(String))` has
2696+
`Input = number` but `Validated = string`). For `'unchecked'` validators and queries
2697+
without arguments it defaults to `Input`.
2698+
26902699
<div class="ts-block">
26912700

26922701
```dts
2693-
type RemoteQueryFunction<Input, Output> = (
2702+
type RemoteQueryFunction<
2703+
Input,
2704+
Output,
2705+
_Validated = Input
2706+
> = (
26942707
arg: undefined extends Input ? Input | void : Input
26952708
) => RemoteQuery<Output>;
26962709
```
@@ -3046,21 +3059,41 @@ type RequestHandler<
30463059

30473060
</div>
30483061

3062+
## RequestedEntry
3063+
3064+
A single entry yielded by [`requested`](/docs/kit/$app-server#requested).
3065+
`arg` is the validated argument (the input *after* the query's schema validated and
3066+
transformed it, if applicable); `query` is a `RemoteQuery` bound to the client's
3067+
original cache key, so `refresh()` / `set()` will update the correct client entry.
3068+
3069+
<div class="ts-block">
3070+
3071+
```dts
3072+
type RequestedEntry<Validated, Output> = {
3073+
arg: Validated;
3074+
query: RemoteQuery<Output>;
3075+
};
3076+
```
3077+
3078+
</div>
3079+
30493080
## RequestedResult
30503081

30513082
<div class="ts-block">
30523083

30533084
```dts
3054-
type RequestedResult<T> = Iterable<T> &
3055-
AsyncIterable<T> & {
3085+
type RequestedResult<Validated, Output> = Iterable<
3086+
RequestedEntry<Validated, Output>
3087+
> &
3088+
AsyncIterable<RequestedEntry<Validated, Output>> & {
30563089
/**
30573090
* Call `refresh` on all queries selected by this `requested` invocation.
30583091
* This is identical to:
30593092
* ```ts
30603093
* import { requested } from '$app/server';
30613094
*
3062-
* for await (const arg of requested(query, ...) {
3063-
* void query(arg).refresh();
3095+
* for await (const { query } of requested(getPost, ...)) {
3096+
* void query.refresh();
30643097
* }
30653098
* ```
30663099
*/

apps/svelte.dev/content/docs/kit/98-reference/20-$app-server.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ function query<Schema extends StandardSchemaV1, Output>(
261261
) => MaybePromise<Output>
262262
): RemoteQueryFunction<
263263
StandardSchemaV1.InferInput<Schema>,
264-
Output
264+
Output,
265+
StandardSchemaV1.InferOutput<Schema>
265266
>;
266267
```
267268

@@ -301,18 +302,24 @@ function read(asset: string): Response;
301302
## requested
302303

303304
In the context of a remote `command` or `form` request, returns an iterable
304-
of the client-requested refreshes' validated arguments up to the supplied limit.
305-
Arguments that fail validation or exceed the limit are recorded as failures in
305+
of `{ arg, query }` entries for the refreshes requested by the client, up to
306+
the supplied `limit`. Each `query` is a `RemoteQuery` bound to the original
307+
client-side cache key, so `refresh()` / `set()` propagate correctly even when
308+
the query's schema transforms the input. `arg` is the *validated* argument,
309+
i.e. the value after the schema has run (so `InferOutput<Schema>` for queries
310+
declared with a Standard Schema).
311+
312+
Arguments that fail validation or exceed `limit` are recorded as failures in
306313
the response to the client.
307314

308315
```ts
309316
import { requested } from '$app/server';
310317

311-
for (const arg of requested(getPost, 5)) {
312-
// it's safe to throw away this promise -- SvelteKit
313-
// will await it for us and handle any errors by sending
314-
// them to the client.
315-
void getPost(arg).refresh();
318+
for (const { arg, query } of requested(getPost, 5)) {
319+
// `arg` is the validated argument; `query` is bound to the client's
320+
// cache key. It's safe to throw away this promise -- SvelteKit will
321+
// await it and forward any errors to the client.
322+
void query.refresh();
316323
}
317324
```
318325

@@ -327,10 +334,10 @@ await requested(getPost, 5).refreshAll();
327334
<div class="ts-block">
328335

329336
```dts
330-
function requested<Input, Output>(
331-
query: RemoteQueryFunction<Input, Output>,
332-
limit?: number
333-
): RequestedResult<Input>;
337+
function requested<Input, Output, Validated = Input>(
338+
query: RemoteQueryFunction<Input, Output, Validated>,
339+
limit: number
340+
): RequestedResult<Validated, Output>;
334341
```
335342

336343
</div>
@@ -375,7 +382,8 @@ namespace query {
375382
>
376383
): RemoteQueryFunction<
377384
StandardSchemaV1.InferInput<Schema>,
378-
Output
385+
Output,
386+
StandardSchemaV1.InferOutput<Schema>
379387
>;
380388
}
381389
```

apps/svelte.dev/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"@supabase/supabase-js": "^2.97.0",
5656
"@sveltejs/adapter-vercel": "^6.3.3",
5757
"@sveltejs/enhanced-img": "^0.10.3",
58-
"@sveltejs/kit": "^2.57.1",
58+
"@sveltejs/kit": "^2.58.0",
5959
"@sveltejs/site-kit": "workspace:*",
6060
"@sveltejs/sv-utils": "^0.2.0",
6161
"@sveltejs/vite-plugin-svelte": "^7.0.0",

0 commit comments

Comments
 (0)