Skip to content

Commit a1ce3c4

Browse files
AerilymTkDodo
andauthored
docs: Update Streaming with Server Components docs to reflect correct usage (#7725)
* chore: update Streaming with Server Components docs to reflect correct usage * chore: update Streaming with Server Components to show moving the getQueryClient function * chore: fix prettier styling --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent 95d814a commit a1ce3c4

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

docs/framework/react/guides/advanced-ssr.md

+25-5
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,9 @@ With the prefetching patterns described above, React Query is perfectly compatib
365365

366366
As of React Query v5.40.0, you don't have to `await` all prefetches for this to work, as `pending` Queries can also be dehydrated and sent to the client. This lets you kick off prefetches as early as possible without letting them block an entire Suspense boundary, and streams the _data_ to the client as the query finishes. This can be useful for example if you want to prefetch some content that is only visible after some user interaction, or say if you want to `await` and render the first page of an infinite query, but start prefetching page 2 without blocking rendering.
367367

368-
To make this work, we have to instruct the `queryClient` to also `dehydrate` pending Queries. We can do this globally, or by passing that option directly to `hydrate`:
368+
To make this work, we have to instruct the `queryClient` to also `dehydrate` pending Queries. We can do this globally, or by passing that option directly to `hydrate`.
369+
370+
We will also need to move the `getQueryClient()` function out of our `app/providers.jsx` file as we want to use it in our server component and our client provider.
369371

370372
```tsx
371373
// app/get-query-client.ts
@@ -378,15 +380,30 @@ function makeQueryClient() {
378380
staleTime: 60 * 1000,
379381
},
380382
dehydrate: {
381-
// per default, only successful Queries are included,
382-
// this includes pending Queries as well
383+
// include pending queries in dehydration
383384
shouldDehydrateQuery: (query) =>
384385
defaultShouldDehydrateQuery(query) ||
385386
query.state.status === 'pending',
386387
},
387388
},
388389
})
389390
}
391+
392+
let browserQueryClient: QueryClient | undefined = undefined
393+
394+
export function getQueryClient() {
395+
if (isServer) {
396+
// Server: always make a new query client
397+
return makeQueryClient()
398+
} else {
399+
// Browser: make a new query client if we don't already have one
400+
// This is very important, so we don't re-make a new client if React
401+
// suspends during the initial render. This may not be needed if we
402+
// have a suspense boundary BELOW the creation of the query client
403+
if (!browserQueryClient) browserQueryClient = makeQueryClient()
404+
return browserQueryClient
405+
}
406+
}
390407
```
391408

392409
> Note: This works in NextJs and Server Components because React can serialize Promises over the wire when you pass them down to Client Components.
@@ -439,7 +456,7 @@ If you're using non-JSON data types and serialize the query results on the serve
439456
import { QueryClient, defaultShouldDehydrateQuery } from '@tanstack/react-query'
440457
import { deserialize, serialize } from './transformer'
441458

442-
export function makeQueryClient() {
459+
function makeQueryClient() {
443460
return new QueryClient({
444461
defaultOptions: {
445462
// ...
@@ -452,6 +469,8 @@ export function makeQueryClient() {
452469
},
453470
})
454471
}
472+
473+
// ...
455474
```
456475

457476
```tsx
@@ -461,11 +480,12 @@ import {
461480
HydrationBoundary,
462481
QueryClient,
463482
} from '@tanstack/react-query'
483+
import { getQueryClient } from './get-query-client'
464484
import { serialize } from './transformer'
465485
import Posts from './posts'
466486

467487
export default function PostsPage() {
468-
const queryClient = new QueryClient()
488+
const queryClient = getQueryClient()
469489

470490
// look ma, no await
471491
queryClient.prefetchQuery({

0 commit comments

Comments
 (0)