Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,40 +161,17 @@ export const getPost = query(v.string(), async (slug) => {

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.

### Updating queries
### Refreshing queries

Any query can be re-fetched via its `refresh` method, which retrieves the latest value from the server:
Any query can be updated via its `refresh` method:

```svelte
<button onclick={() => getPosts().refresh()}>
Check for new posts
</button>
```

Alternatively, if you need to update its value manually, you can use the `set` method:

```svelte
<script>
import { getPosts } from './data.remote';
import { onMount } from 'svelte';

onMount(() => {
const ws = new WebSocket('/ws');
ws.addEventListener('message', (ev) => {
const message = JSON.parse(ev.data);
if (message.type === 'new-post') {
getPosts().set([
message.post,
...getPosts().current,
]);
}
});
return () => ws.close();
});
</script>
```

> [!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.
> [!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.

## form

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2191,12 +2191,6 @@ type RemotePrerenderFunction<Input, Output> = (

```dts
type RemoteQuery<T> = RemoteResource<T> & {
/**
* On the client, this function will update the value of the query without re-fetching it.
*
* On the server, this throws an error.
*/
set(value: T): void;
/**
* On the client, this function will re-fetch the query from the server.
*
Expand Down