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

docs(svelte-query): Expand SSR docs #4809

Merged
merged 3 commits into from Jan 13, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/svelte/ssr.md
Expand Up @@ -5,6 +5,14 @@ title: SSR and SvelteKit

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

## Caveat

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.

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.

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).

## Using `initialData`

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

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

Cons:

Expand Down Expand Up @@ -108,3 +117,4 @@ Pros:
Cons:

- Requires more files for initial setup
- Works with only `+page.ts`/`+layout.ts` load functions
1 change: 1 addition & 0 deletions examples/svelte/auto-refetching/src/routes/+layout.ts
@@ -0,0 +1 @@
export const ssr = false
1 change: 1 addition & 0 deletions examples/svelte/hydration/src/routes/+layout.ts
@@ -0,0 +1 @@
export const ssr = true
@@ -0,0 +1 @@
export const ssr = false
1 change: 1 addition & 0 deletions examples/svelte/playground/src/routes/+layout.ts
@@ -0,0 +1 @@
export const ssr = false
1 change: 1 addition & 0 deletions examples/svelte/star-wars/src/routes/+layout.ts
@@ -0,0 +1 @@
export const ssr = false
2 changes: 1 addition & 1 deletion examples/svelte/star-wars/src/routes/films/+page.svelte
Expand Up @@ -6,7 +6,7 @@
return await res.json()
}

query = createQuery({
const query = createQuery({
queryKey: ['films'],
queryFn: getFilms,
})
Expand Down
13 changes: 0 additions & 13 deletions packages/svelte-query/static/emblem-light.svg

This file was deleted.