MCPcopy Index your code
hub / github.com/TanStack/query / dehydrateQuery

Function dehydrateQuery

packages/query-core/src/hydration.ts:76–108  ·  view source on GitHub ↗
(
  query: Query,
  serializeData: TransformerFn,
  shouldRedactErrors: (error: unknown) => boolean,
)

Source from the content-addressed store, hash-verified

74// Sometimes it might make sense to prefetch data on the server and include
75// in the html-payload, but not consume it on the initial render.
76function dehydrateQuery(
77 query: Query,
78 serializeData: TransformerFn,
79 shouldRedactErrors: (error: unknown) => boolean,
80): DehydratedQuery {
81 return {
82 dehydratedAt: Date.now(),
83 state: {
84 ...query.state,
85 ...(query.state.data !== undefined && {
86 data: serializeData(query.state.data),
87 }),
88 },
89 queryKey: query.queryKey,
90 queryHash: query.queryHash,
91 ...(query.state.status === 'pending' && {
92 promise: query.promise?.then(serializeData).catch((error) => {
93 if (!shouldRedactErrors(error)) {
94 // Reject original error if it should not be redacted
95 return Promise.reject(error)
96 }
97 // If not in production, log original error before rejecting redacted error
98 if (process.env.NODE_ENV !== 'production') {
99 console.error(
100 `A query that was dehydrated as pending ended up rejecting. [${query.queryHash}]: ${error}; The error will be redacted in production builds`,
101 )
102 }
103 return Promise.reject(new Error('redacted'))
104 }),
105 }),
106 ...(query.meta && { meta: query.meta }),
107 }
108}
109
110export function defaultShouldDehydrateMutation(mutation: Mutation) {
111 return mutation.state.isPaused

Callers 1

dehydrateFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…