Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 17212cc

Browse files
Sync kit docs (#1509)
sync kit docs Co-authored-by: svelte-docs-bot[bot] <196124396+svelte-docs-bot[bot]@users.noreply.github.com>
1 parent 920f817 commit 17212cc

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,40 @@ export const getPost = query(v.string(), async (slug) => {
161161
162162
Both the argument and the return value are serialized with [devalue](https://github.com/sveltejs/devalue), which handles types like `Date` and `Map` (and custom types defined in your [transport hook](hooks#Universal-hooks-transport)) in addition to JSON.
163163
164-
### Refreshing queries
164+
### Updating queries
165165
166-
Any query can be updated via its `refresh` method:
166+
Any query can be re-fetched via its `refresh` method, which retrieves the latest value from the server:
167167
168168
```svelte
169169
<button onclick={() => getPosts().refresh()}>
170170
Check for new posts
171171
</button>
172172
```
173173
174-
> [!NOTE] Queries are cached while they're on the page, meaning `getPosts() === getPosts()`. This means you don't need a reference like `const posts = getPosts()` in order to refresh the query.
174+
Alternatively, if you need to update its value manually, you can use the `set` method:
175+
176+
```svelte
177+
<script>
178+
import { getPosts } from './data.remote';
179+
import { onMount } from 'svelte';
180+
181+
onMount(() => {
182+
const ws = new WebSocket('/ws');
183+
ws.addEventListener('message', (ev) => {
184+
const message = JSON.parse(ev.data);
185+
if (message.type === 'new-post') {
186+
getPosts().set([
187+
message.post,
188+
...getPosts().current,
189+
]);
190+
}
191+
});
192+
return () => ws.close();
193+
});
194+
</script>
195+
```
196+
197+
> [!NOTE] Queries are cached while they're on the page, meaning `getPosts() === getPosts()`. This means you don't need a reference like `const posts = getPosts()` in order to update the query.
175198
176199
## form
177200

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,12 @@ type RemotePrerenderFunction<Input, Output> = (
21912191

21922192
```dts
21932193
type RemoteQuery<T> = RemoteResource<T> & {
2194+
/**
2195+
* On the client, this function will update the value of the query without re-fetching it.
2196+
*
2197+
* On the server, this throws an error.
2198+
*/
2199+
set(value: T): void;
21942200
/**
21952201
* On the client, this function will re-fetch the query from the server.
21962202
*

0 commit comments

Comments
 (0)