examples/svelte/ssr-sveltekit
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Svelte Query SSR Example (SvelteKit)#
Fills the cache in SvelteKit load functions with SvelteKit's fetch, so pages render on the server with data and the browser reuses the responses SvelteKit inlined instead of fetching again.
Built with SvelteKit.
How it works#
src/lib/services/posts/queries.tsdefinespostsQueries.list(limit, customFetch)andpostsQueries.detail(id, customFetch)withqueryOptions. The optionalcustomFetchlets aloadfunction pass SvelteKit'sfetchtosrc/lib/services/posts/api.ts.src/routes/+layout.tscreates theQueryClientin aloadfunction with a 1-minutestaleTimeandenabled: browser, andsrc/routes/+layout.svelteprovides it withQueryClientProvider.src/routes/+page.tsandsrc/routes/[postId]/+page.tsget the client fromparent()and fill the cache withqueryClient.query(...)using SvelteKit'sfetch, so the page is rendered on the server with data.src/lib/Posts.svelteandsrc/lib/Post.svelteread the same options withcreateQuery, andsrc/lib/Posts.sveltecallsclient.getQueryData(postsQueries.detail(post.id).queryKey)to style links whose detail is already cached.
To run this example:
npm installnpm run dev