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

make-query-client.ts

53 lines1.3 KB

1import { Temporal } from '@js-temporal/polyfill'
2import { QueryClient, defaultShouldDehydrateQuery } from '@tanstack/react-query'
3import { createTson } from 'tupleson'
4import type { TsonType } from 'tupleson'
5​
6const plainDate = {
7 deserialize: (v) => Temporal.PlainDate.from(v),
8 key: 'PlainDate',
9 serialize: (v) => v.toJSON(),
10 test: (v) => v instanceof Temporal.PlainDate,
11} satisfies TsonType<Temporal.PlainDate, string>
12​
13export const countRef = {
14 current: 0,
16​
17export const tson = createTson({
18 types: [plainDate],
19})
20​
21export function makeQueryClient() {
22 return new QueryClient({
23 defaultOptions: {
24 hydrate: {
25 /**
26 * Called when the query is rebuilt from a prefetched
27 * promise, before the query data is put into the cache.
28 */
29 deserializeData: (data) => {
30 return tson.deserialize(data)
31 },
32 },
33 queries: {
34 staleTime: 60 * 1000,
35 },
36 dehydrate: {
37 serializeData: (data) => {
38 return tson.serialize(data)
39 },
40 shouldDehydrateQuery: (query) => {
41 return (
42 defaultShouldDehydrateQuery(query) ||
43 query.state.status === 'pending'
44 )
45 },
46 shouldRedactErrors: (error) => {
47 // Next.js automatically redacts errors for us
48 return false
49 },
50 },
51 },
52 })