Open History to see the last commit
1import type { ApplicationConfig } from '@angular/core'
2import {
3 provideClientHydration,
4 withEventReplay,
5} from '@angular/platform-browser'
6import {
7 QueryClient,
8 provideTanStackQuery,
9} from '@tanstack/angular-query-experimental'
10import { withDevtools } from '@tanstack/angular-query-devtools'
11import { withPersistQueryClient } from '@tanstack/angular-query-persist-client'
12import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister'
13β
14/** Storage key for the persisted client cache (browser only). */
15export const PERSIST_STORAGE_KEY = 'tanstack-query-angular-ssr-persist-example'
16β
17export const sharedQueryDefaults = {
18 staleTime: 1000 * 30,
19 gcTime: 1000 * 60 * 60 * 24,
20} as const
21β
22export const createBrowserQueryClient = () =>
23 new QueryClient({
24 defaultOptions: {
25 queries: { ...sharedQueryDefaults },
26 },
27 })
28β
29/**
30 * Shared browser + server config. Extra TanStack Query features wrap the same client in browser
31 * and SSR; persister setup only runs in the browser when using
32 * {@link withPersistQueryClient}'s factory pattern.
33 */
34export const getBaseAppConfig = (
35 queryClient: QueryClient,
36): ApplicationConfig => {
37 return {
38 providers: [
39 provideClientHydration(withEventReplay()),
40 provideTanStackQuery(
41 queryClient,
42 withDevtools(),
43 withPersistQueryClient(() => ({
44 persistOptions: {
45 persister: createAsyncStoragePersister({
46 storage: localStorage,
47 key: PERSIST_STORAGE_KEY,
48 throttleTime: 1000,
49 })
50 },
51 })),
52 ),
53 ],
54 }
55}
56β
57export const getClientAppConfig = (): ApplicationConfig =>
58 getBaseAppConfig(createBrowserQueryClient())