Skip to content

Commit 6e238af

Browse files
EphemTkDodo
andauthored
docs: SSR rewrite (#5966)
* docs(react-query): add Request Waterfalls guide Also includes initial scaffold for new prefetch/SSR docs structure * docs(react-query): update prefetching guide * docs(migration-guide): add more hydration api changes to migration guide * docs(prefetching): polish prefetching docs * docs(react-query): rewrite ssr doc page * docs(react-query): add advanced server rendering guide * docs(react-query): add clarifications to ssr docs * docs(react-query): add note about Suspense for streaming ssr plugin --------- Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 05bfd3e commit 6e238af

File tree

8 files changed

+1459
-368
lines changed

8 files changed

+1459
-368
lines changed

docs/config.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@
112112
"label": "Placeholder Query Data",
113113
"to": "react/guides/placeholder-query-data"
114114
},
115-
{
116-
"label": "Prefetching",
117-
"to": "react/guides/prefetching"
118-
},
119115
{
120116
"label": "Mutations",
121117
"to": "react/guides/mutations"
@@ -149,9 +145,21 @@
149145
"to": "react/guides/filters"
150146
},
151147
{
152-
"label": "SSR & Next.js",
148+
"label": "Performance & Request Waterfalls",
149+
"to": "react/guides/request-waterfalls"
150+
},
151+
{
152+
"label": "Prefetching & Router Integration",
153+
"to": "react/guides/prefetching"
154+
},
155+
{
156+
"label": "Server Rendering & Hydration",
153157
"to": "react/guides/ssr"
154158
},
159+
{
160+
"label": "Advanced Server Rendering",
161+
"to": "react/guides/advanced-ssr"
162+
},
155163
{
156164
"label": "Caching",
157165
"to": "react/guides/caching"

docs/react/guides/advanced-ssr.md

Lines changed: 389 additions & 0 deletions
Large diffs are not rendered by default.

docs/react/guides/dependent-queries.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,28 @@ Dynamic parallel query - `useQueries` can depend on a previous query also, here'
6868
const { data: userIds } = useQuery({
6969
queryKey: ['users'],
7070
queryFn: getUsersData,
71-
select: users => users.map(user => user.id),
71+
select: (users) => users.map((user) => user.id),
7272
})
7373

7474
// Then get the users messages
7575
const usersMessages = useQueries({
7676
queries: users
77-
? usersId.map(id => {
77+
? usersId.map((id) => {
7878
return {
7979
queryKey: ['messages', id],
8080
queryFn: () => getMessagesByUsers(id),
81-
};
81+
}
8282
})
83-
: [], // if users is undefined, an empty array will be returned
83+
: [], // if users is undefined, an empty array will be returned
8484
})
8585
```
8686

8787
[//]: # 'Example2'
8888

8989
**Note** that `useQueries` return an **array of query results**
90+
91+
## A note about performance
92+
93+
Dependent queries by definition constitutes a form of [request waterfall](../guides/request-waterfalls), which hurts performance. If we pretend both queries take the same amount of time, doing them serially instead of in parallel always takes twice as much time, which is especially hurtful when it happens on a client that has high latency. If you can, it's always better to restructure the backend APIs so that both queries can be fetched in parallel, though that might not always be practically feasible.
94+
95+
In the example above, instead of first fetching `getUserByEmail` to be able to `getProjectsByUser`, introducing a new `getProjectsByUserEmail` query would flatten the waterfall.

docs/react/guides/migrating-to-v5.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ However, refetching all pages might lead to UI inconsistencies. Also, this optio
312312

313313
The v5 includes a new `maxPages` option for infinite queries to limit the number of pages to store in the query data and to refetch. This new feature handles the use cases initially identified for the `refetchPage` page feature without the related issues.
314314

315-
### New hydration API
315+
### New `dehydrate` API
316316

317-
The options you can pass to dehydrate have been simplified. Queries and Mutations are always dehydrated (according to the default function implementation). To change this behaviour, you can implement `shouldDehydrateQuery` or `shouldDehydrateMutation`.
317+
The options you can pass to `dehydrate` have been simplified. Queries and Mutations are always dehydrated (according to the default function implementation). To change this behaviour, instead of using the removed boolean options `dehydrateMutations` and `dehydrateQueries` you can implement the function equivalents `shouldDehydrateQuery` or `shouldDehydrateMutation` instead. To get the old behaviour of not hydrating queries/mutations at all, pass in `() => false`.
318318

319319
```diff
320320
- dehydrateMutations?: boolean
@@ -386,9 +386,15 @@ import { batch } from 'solid-js'
386386
notifyManager.setBatchNotifyFunction(batch)
387387
```
388388

389-
### `Hydrate` has been renamed to `HydrationBoundary` and the `useHydrate` hook has been removed
389+
### Hydration API changes
390390

391-
The `Hydrate` component has been renamed to `HydrationBoundary`. The `Hydrate` component was also a wrapper over `useHydrate` hook, which has been removed.
391+
To better support concurrent features and transitions we've made some changes to the hydration APIs. The `Hydrate` component has been renamed to `HydrationBoundary` and the `useHydrate` hook has been removed.
392+
393+
The `HydrationBoundary` no longer hydrates mutations, only queries. To hydrate mutations, use the low level `hydrate` API or the `persistQueryClient` plugin.
394+
395+
Finally, as a technical detail, the timing for when queries are hydrated have changed slightly. New queries are still hydrated in the render phase so that SSR works as usual, but any queries that already exist in the cache are now hydrated in an effect instead (as long as their data is fresher than what is in the cache). If you are hydrating just once at the start of your application as is common, this wont affect you, but if you are using Server Components and pass down fresh data for hydration on a page navigation, you might notice a flash of the old data before the page immediately rerenders.
396+
397+
This last change is technically a breaking one, and was made so we don't prematurely update content on the _existing_ page before a page transition has been fully committed. No action is required on your part.
392398

393399
```diff
394400
- import { Hydrate } from '@tanstack/react-query'

0 commit comments

Comments
 (0)