examples/preact/ssr
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Preact Query SSR Example#
Server-side rendering: the server prefetches queries, dehydrates the cache into the page, and the client hydrates it.
How it works#
src/services/posts/queries.ts,src/services/users/queries.ts, andsrc/services/comments/queries.tsdefinequeryOptionsfactories for JSONPlaceholder.usersQueries.detail(id)usesskipTokenuntil the id is known.src/App.tsxcallsuseQueryfor the posts, the first post's author, and the comments. Only the posts and the author are prefetched on the server, so the comments start loading on the client.src/entry-server.tsxprefetches withprefetchClient.query(...), awaiting the posts and then the author with.catch(noop). It callsdehydrate,hydrates a second client forrenderToString, and serializes the state withunevalfromdevalue.src/entry-client.tsxreadswindow.__DEHYDRATED_STATE__, passes it toHydrationBoundary, and calls Preact'shydrate.src/makeQueryClient.tssets a 1-minutestaleTimeso the client does not refetch right away.server.jsis an Express server that runsrender()fromsrc/entry-server.tsxand fills the<!--app-html-->and<!--app-state-->placeholders inindex.html.
To run this example:
npm installnpm run dev