Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document initialData #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,47 @@ Running a mutation looks like
</form>
```

### Server-side rendering

`useQuery()` accepts an `initialData` option in its third argument.
By defining a `load()` function in a +page.server.ts file
that uses the `ConvexHttpClient` to request the same query to get initial data
and passing that through to the `initialData` option of a useQuery call you can avoid an initial loading state.

```ts
// +page.server.ts
import { ConvexHttpClient } from 'convex/browser';
import type { PageServerLoad } from './$types.js';
import { PUBLIC_CONVEX_URL } from '$env/static/public';
import { api } from '../convex/_generated/api.js';

export const load = (async () => {
const client = new ConvexHttpClient(PUBLIC_CONVEX_URL!);
return {
messages: await client.query(api.messages.list, { muteWords: [] })
};
}) satisfies PageServerLoad;
```

```svelte
<script lang="ts">
// +page.svelte
import type { PageData } from './$types.js';
let { data }: { data: PageData } = $props();

import { useQuery, useConvexClient } from '$lib/client.svelte.js';
import { api } from '../convex/_generated/api.js';

const messages = useQuery(
api.messages.list,
() => args,
() => ({ initialData: data.messages })
);
</script>
```

Combining specifying `initialData` and either setting the `keepPreviousData` option to true or never modifying the arguments passed to a query should be enough to avoid ever seeing a loading state for a `useQuery()`.

### Deploying a Svelte App

In production build pipelines use the build command
Expand Down