SSR and SvelteKit

Setup

SvelteKit defaults to rendering routes with SSR. Because of this, 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.

The recommended way to achieve this is to use the browser module from SvelteKit in your QueryClient object. This will not disable queryClient.prefetchQuery(), which is used in one of the solutions below.

src/routes/+layout.svelte

Prefetching data

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

If you wish to view the ideal SSR setup, please have a look at the SSR example.

Using initialData

Together with SvelteKit's load, you can pass the data loaded server-side into createQuery's' initialData option:

src/routes/+page.ts

src/routes/+page.svelte

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:

  • If you are calling createQuery in a component deeper down in the tree you need to pass the initialData down to that point
  • If you are calling createQuery with the same query in multiple locations, you need to pass initialData to all of them
  • There is no way to know at what time the query was fetched on the server, so dataUpdatedAt and determining if the query needs refetching is based on when the page loaded instead

Using prefetchQuery

Svelte Query supports prefetching queries on the server. Using this setup below, you can fetch data and pass it into QueryClientProvider before it is sent to the user's browser. Therefore, this data is already available in the cache, and no initial fetch occurs client-side.

src/routes/+layout.ts

src/routes/+layout.svelte

src/routes/+page.ts

src/routes/+page.svelte

Pros:

  • Server-loaded data can be accessed anywhere without prop-drilling
  • No initial fetch occurs client-side once the page is rendered, as the query cache retains all information about the query was made including dataUpdatedAt

Cons:

  • Requires more files for initial setup
  • Will not work with +page.server.ts/+layout.server.ts load functions (however, APIs which are used with TanStack Query need to be fully exposed to the browser anyway)
Want to Skip the Docs?
Query.gg - The Official React Query Course
“This course is the best way to learn how to use React Query in real-world applications.”—Tanner Linsley
Check it out