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

ssr.md

101 lines3.2 KB

1---
2id: ssr
3title: SSR
4---
5​
6For [Angular SSR](https://angular.dev/guide/ssr), [`provideTanStackQuery`](../reference/functions/provideTanStackQuery.md) serializes the `QueryClient` cache into Angular's `TransferState` and restores it when the browser application starts.
7​
8See the [Angular SSR example](https://github.com/TanStack/query/tree/main/examples/angular/ssr). The
9[SSR persistence example](https://github.com/TanStack/query/tree/main/examples/angular/ssr-persist)
10builds on the same setup with browser persistence.
11​
12## Query client factory
13​
14```ts
15import { isPlatformBrowser } from '@angular/common'
16import { inject, PLATFORM_ID } from '@angular/core'
17import { QueryClient } from '@tanstack/angular-query'
18​
19export function createQueryClient() {
20 const isBrowser = isPlatformBrowser(inject(PLATFORM_ID))
21​
22 return new QueryClient({
23 defaultOptions: {
24 queries: {
25 // Retry failed queries in the browser, but not on the server.
26 retry: isBrowser ? 3 : 0,
27 },
28 },
29 })
31```
32​
33## Browser config
34​
35Use the factory with `provideTanStackQuery` in your application config. Angular's HTTP transfer
36cache is enabled by default with
37[`provideClientHydration`](https://angular.dev/api/platform-browser/provideClientHydration). Add
38[`withNoHttpTransferCache`](https://angular.dev/api/platform-browser/withNoHttpTransferCache) so
39query results are not serialized once by each cache.
40​
41```ts
42import { provideHttpClient } from '@angular/common/http'
43import {
44 provideClientHydration,
45 withEventReplay,
46 withNoHttpTransferCache,
47} from '@angular/platform-browser'
48import { provideTanStackQuery } from '@tanstack/angular-query'
49import { withDevtools } from '@tanstack/angular-query-devtools'
50import { createQueryClient } from './query-client'
51import type { ApplicationConfig } from '@angular/core'
52​
53export const appConfig: ApplicationConfig = {
54 providers: [
55 provideHttpClient(),
56 provideClientHydration(withEventReplay(), withNoHttpTransferCache()),
57 provideTanStackQuery(createQueryClient, withDevtools()),
58 ],
60```
61​
62## Server config
63​
64Merge the application config with `provideServerRendering` in the server config.
65​
66```ts
67import { mergeApplicationConfig } from '@angular/core'
68import { provideServerRendering, withRoutes } from '@angular/ssr'
69import { appConfig } from './app.config'
70import { serverRoutes } from './app.routes.server'
71​
72export const serverConfig = mergeApplicationConfig(appConfig, {
73 providers: [provideServerRendering(withRoutes(serverRoutes))],
74})
75```
76​
77## Multiple query clients
78​
79Built-in hydration uses a default transfer key. For a second `QueryClient` in a child injector, pass a distinct key with `withHydrationKey` so each client's serialized cache stays separate.
80​
81```ts
82providers: [
83 provideTanStackQuery(
84 createSecondaryQueryClient,
85 withHydrationKey('my-secondary-query-cache'),
86 ),
88```
89​
90## Disabling built-in hydration
91​
92If you need to opt out of TanStack Query's built-in `TransferState` integration for a specific injector, add `withNoQueryHydration()`.
93​
94```ts
95providers: [provideTanStackQuery(createQueryClient, withNoQueryHydration())]
96```
97​
98## See also
99​
100- [Angular HttpClient and data fetching](../angular-httpclient-and-other-data-fetching-clients.md)
101- [Devtools](../devtools.md)