examples/angular/ssr-persist
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Angular Query SSR Persist Example#
The SSR posts example with a localStorage persister and a query that only runs in the browser.
Built with Angular SSR.
How it works#
src/app/components/posts.component.tsreads in-memory posts fromsrc/app/services/posts.service.tswithinjectQuery. The query runs during server rendering and its result is transferred to the browser.src/app/app.config.tsaddswithPersistQueryClienttoprovideTanStackQuerywith alocalStoragepersister fromcreateAsyncStoragePersister. The options factory only runs in the browser, solocalStorageis not touched on the server.src/app/app.component.tssetsshowClientDemototrueinafterNextRender, sosrc/app/components/client-persist-demo.component.tsis only created in the browser and its query never runs on the server.- That query stores a timestamp with
staleTime: Infinity. After a hard reload, the posts come from the server again and the timestamp is restored fromlocalStorage. src/app/query-client.tsexports thecreateQueryClientfactory, which turns offretryon the server and sets agcTimeof 24 hours so the persisted cache is kept.
To run this example:
npm installnpm start
To run the production server, run npm run build and then npm run serve:ssr.