You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This hook is called when a remote function is called with an argument that does not match the provided [Standard Schema](https://standardschema.dev/). It must return an object matching the shape of [`App.Error`](types#Error).
150
+
151
+
Say you have a remote function that expects a string as its argument ...
152
+
153
+
```js
154
+
/// file: todos.remote.js
155
+
import*asvfrom'valibot';
156
+
import { query } from'$app/server';
157
+
158
+
exportconstgetTodo=query(v.string(), (id) => {
159
+
// implementation...
160
+
});
161
+
```
162
+
163
+
...but it is called with something that doesn't match the schema — such as a number (e.g `await getTodos(1)`) — then validation will fail, the server will respond with a [400 status code](https://http.dog/400), and the function will throw with the message 'Bad Request'.
164
+
165
+
To customise this message and add additional properties to the error object, implement `handleValidationError`:
Be thoughtful about what information you expose here, as the most likely reason for validation to fail is that someone is sending malicious requests to your server.
178
+
147
179
## Shared hooks
148
180
149
181
The following can be added to `src/hooks.server.js`_and_`src/hooks.client.js`:
/** Spread this onto a `<button>` or `<input type="submit">` */
2001
+
buttonProps: {
2002
+
type: 'submit';
2003
+
formmethod: 'POST';
2004
+
formaction: string;
2005
+
onclick: (event: Event) => void;
2006
+
/** Use the `enhance` method to influence what happens when the form is submitted. */
2007
+
enhance(
2008
+
callback: (opts: {
2009
+
form: HTMLFormElement;
2010
+
data: FormData;
2011
+
submit: () => Promise<void> & {
2012
+
updates: (
2013
+
...queries: Array<
2014
+
RemoteQuery<any> | RemoteQueryOverride
2015
+
>
2016
+
) => Promise<void>;
2017
+
};
2018
+
}) => void
2019
+
): {
2020
+
type: 'submit';
2021
+
formmethod: 'POST';
2022
+
formaction: string;
2023
+
onclick: (event: Event) => void;
2024
+
};
2025
+
};
2026
+
};
2027
+
```
2028
+
2029
+
</div>
2030
+
2031
+
## RemotePrerenderFunction
2032
+
2033
+
The return value of a remote `prerender` function. See [Remote functions](/docs/kit/remote-functions#prerender) for full documentation.
2034
+
2035
+
<divclass="ts-block">
2036
+
2037
+
```dts
2038
+
type RemotePrerenderFunction<Input, Output> = (
2039
+
arg: Input
2040
+
) => RemoteResource<Output>;
2041
+
```
2042
+
2043
+
</div>
2044
+
2045
+
## RemoteQuery
2046
+
2047
+
<divclass="ts-block">
2048
+
2049
+
```dts
2050
+
type RemoteQuery<T> = RemoteResource<T> & {
2051
+
/**
2052
+
* On the client, this function will re-fetch the query from the server.
2053
+
*
2054
+
* On the server, this can be called in the context of a `command` or `form` and the refreshed data will accompany the action response back to the client.
2055
+
* This prevents SvelteKit needing to refresh all queries on the page in a second server round-trip.
2056
+
*/
2057
+
refresh(): Promise<void>;
2058
+
/**
2059
+
* Temporarily override the value of a query. This is used with the `updates` method of a [command](https://svelte.dev/docs/kit/remote-functions#command-Single-flight-mutations) or [enhanced form submission](https://svelte.dev/docs/kit/remote-functions#form-enhance) to provide optimistic updates.
2060
+
*
2061
+
* ```svelte
2062
+
* <script>
2063
+
* import { getTodos, addTodo } from './todos.remote.js';
The return value of a remote `query` function. See [Remote functions](/docs/kit/remote-functions#query) for full documentation.
2088
+
2089
+
<divclass="ts-block">
2090
+
2091
+
```dts
2092
+
type RemoteQueryFunction<Input, Output> = (
2093
+
arg: Input
2094
+
) => RemoteQuery<Output>;
2095
+
```
2096
+
2097
+
</div>
2098
+
2099
+
## RemoteQueryOverride
2100
+
2101
+
<divclass="ts-block">
2102
+
2103
+
```dts
2104
+
interface RemoteQueryOverride {/*…*/}
2105
+
```
2106
+
2107
+
<divclass="ts-block-property">
2108
+
2109
+
```dts
2110
+
_key: string;
2111
+
```
2112
+
2113
+
<divclass="ts-block-property-details"></div>
2114
+
</div>
2115
+
2116
+
<divclass="ts-block-property">
2117
+
2118
+
```dts
2119
+
release(): void;
2120
+
```
2121
+
2122
+
<divclass="ts-block-property-details"></div>
2123
+
</div></div>
2124
+
2125
+
## RemoteResource
2126
+
2127
+
<divclass="ts-block">
2128
+
2129
+
```dts
2130
+
type RemoteResource<T> = Promise<Awaited<T>> & {
2131
+
/** The error in case the query fails. Most often this is a [`HttpError`](https://svelte.dev/docs/kit/@sveltejs-kit#HttpError) but it isn't guaranteed to be. */
2132
+
get error(): any;
2133
+
/** `true` before the first result is available and during refreshes */
2134
+
get loading(): boolean;
2135
+
} & (
2136
+
| {
2137
+
/** The current value of the query. Undefined until `ready` is `true` */
2138
+
get current(): undefined;
2139
+
ready: false;
2140
+
}
2141
+
| {
2142
+
/** The current value of the query. Undefined until `ready` is `true` */
2143
+
get current(): Awaited<T>;
2144
+
ready: true;
2145
+
}
2146
+
);
2147
+
```
2148
+
2149
+
</div>
2150
+
1911
2151
## RequestEvent
1912
2152
1913
2153
<divclass="ts-block">
@@ -2115,6 +2355,20 @@ isSubRequest: boolean;
2115
2355
2116
2356
`true` for `+server.js` calls coming from SvelteKit without the overhead of actually making an HTTP request. This happens when you make same-origin `fetch` requests on the server.
2117
2357
2358
+
</div>
2359
+
</div>
2360
+
2361
+
<divclass="ts-block-property">
2362
+
2363
+
```dts
2364
+
isRemoteRequest: boolean;
2365
+
```
2366
+
2367
+
<divclass="ts-block-property-details">
2368
+
2369
+
`true` if the request comes from the client via a remote function. The `url` property will be stripped of the internal information
2370
+
related to the data request in this case. Use this property instead if the distinction is important to you.
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/docs/kit/98-reference/20-$app-navigation.md
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,7 @@ import {
18
18
preloadCode,
19
19
preloadData,
20
20
pushState,
21
+
refreshAll,
21
22
replaceState
22
23
} from'$app/navigation';
23
24
```
@@ -248,6 +249,27 @@ function pushState(
248
249
249
250
250
251
252
+
## refreshAll
253
+
254
+
Causes all currently active remote functions to refresh, and all `load` functions belonging to the currently active page to re-run (unless disabled via the option argument).
255
+
Returns a `Promise` that resolves when the page is subsequently updated.
256
+
257
+
<divclass="ts-block">
258
+
259
+
```dts
260
+
function refreshAll({
261
+
includeLoadFunctions
262
+
}?:
263
+
| {
264
+
includeLoadFunctions?: boolean;
265
+
}
266
+
| undefined): Promise<void>;
267
+
```
268
+
269
+
</div>
270
+
271
+
272
+
251
273
## replaceState
252
274
253
275
Programmatically replace the current history entry with the given `page.state`. To use the current URL, you can pass `''` as the first argument. Used for [shallow routing](/docs/kit/shallow-routing).
0 commit comments