Skip to content

Commit ec65515

Browse files
authored
docs(hydration): Document that hydration api requires values to be JSON serializable (TanStack#2780)
1 parent a85d822 commit ec65515

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

docs/src/pages/reference/hydration.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ const dehydratedState = dehydrate(queryClient, {
4848
- You **should not** rely on the exact format of this response, it is not part of the public API and can change at any time
4949
- This result is not in serialized form, you need to do that yourself if desired
5050

51+
### limitations
52+
53+
The hydration API requires values to be JSON serializable. If you need to dehydrate values that are not automatically serializable to JSON (like `Error` or `undefined`), you have to serialize them for yourself. Since only successful queries are included per default, to also include `Errors`, you have to provide `shouldDehydrateQuery`, e.g.:
54+
55+
```js
56+
// server
57+
const state = dehydrate(client, { shouldDehydrateQuery: () => true }) // to also include Errors
58+
const serializedState = mySerialize(state) // transform Error instances to objects
59+
60+
// client
61+
const state = myDeserialize(serializedState) // transform objects back to Error instances
62+
hydrate(client, state)
63+
```
64+
5165
## `hydrate`
5266

5367
`hydrate` adds a previously dehydrated state into a `cache`. If the queries included in dehydration already exist in the queryCache, `hydrate` does not overwrite them.
@@ -118,4 +132,4 @@ function App() {
118132
- `options: HydrateOptions`
119133
- Optional
120134
- `defaultOptions: QueryOptions`
121-
- The default query options to use for the hydrated queries.
135+
- The default query options to use for the hydrated queries.

0 commit comments

Comments
 (0)