Skip to content

Commit c05bb91

Browse files
docs(svelte-query): Expand SSR docs (#4809)
* docs(svelte-query): Expand SSR docs * Expand wording * Run prettier
1 parent 2ecf24d commit c05bb91

File tree

8 files changed

+16
-14
lines changed

8 files changed

+16
-14
lines changed

docs/svelte/ssr.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ title: SSR and SvelteKit
55

66
Svelte Query supports two ways of prefetching data on the server and passing that to the client with SvelteKit.
77

8+
## Caveat
9+
10+
SvelteKit defaults to rendering routes with SSR. Unless you are using one of the below solutions, you need to disable the query on the server. Otherwise, your query will continue executing on the server asynchronously, even after the HTML has been sent to the client.
11+
12+
One way to achieve this is to `import { browser } from '$app/environment'` and add `enabled: browser` to the options of `createQuery`. This will set the query to disabled on the server, but enabled on the client.
13+
14+
Another way to achieve this is using page options. For that page or layout, you should set `export const ssr = false` in either `+page.ts` or `+layout.ts`. You can read more about using this option [here](https://kit.svelte.dev/docs/page-options#ssr).
15+
816
## Using `initialData`
917

1018
Together with SvelteKit's [`load`](https://kit.svelte.dev/docs/load), you can pass the data loaded server-side into `createQuery`'s' `initialData` option:
@@ -38,6 +46,7 @@ export const load: PageLoad = async () => {
3846
Pros:
3947

4048
- This setup is minimal and this can be a quick solution for some cases
49+
- Works with both `+page.ts`/`+layout.ts` and `+page.server.ts`/`+layout.server.ts` load functions
4150

4251
Cons:
4352

@@ -108,3 +117,4 @@ Pros:
108117
Cons:
109118

110119
- Requires more files for initial setup
120+
- Works with only `+page.ts`/`+layout.ts` load functions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ssr = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ssr = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ssr = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ssr = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ssr = false

examples/svelte/star-wars/src/routes/films/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
return await res.json()
77
}
88
9-
query = createQuery({
9+
const query = createQuery({
1010
queryKey: ['films'],
1111
queryFn: getFilms,
1212
})

packages/svelte-query/static/emblem-light.svg

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)