Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

README.md

20 lines1.2 KB

1# TanStack Angular Query SSR Example
2​
3A list of posts that is rendered on the server and hydrated in the browser without fetching again.
4​
5Built with [Angular SSR](https://angular.dev/guide/ssr).
6​
7## How it works
8​
9- `src/app/services/posts.service.ts` is an `@Injectable` `PostsService` that returns in-memory posts as an observable, so the example needs no API.
10- `src/app/components/posts.component.ts` reads the posts with `injectQuery` and `lastValueFrom`. The query runs during server rendering, and the server waits for it before sending the HTML.
11- `src/app/query-client.ts` exports the `createQueryClient` factory. It runs in an injection context, so it uses `PLATFORM_ID` to turn off `retry` on the server.
12- `src/app/app.config.ts` passes the factory to `provideTanStackQuery`, which transfers the query cache from the server to the browser, and adds `provideClientHydration` and `withDevtools()`.
13- `src/app/app.config.server.ts`, `src/main.server.ts` and `src/server.ts` are the Angular CLI server files. `src/app/app.routes.server.ts` renders every route with `RenderMode.Server`.
14​
15To run this example:
16​
17- `npm install`
18- `npm start`
19​
20To run the production server, run `npm run build` and then `npm run serve:ssr`.