Skip to content
Source
Sign inSign up
TanStack/query

fix(react-query/HydrationBoundary): hydrate existing queries during SSR #11318

Openmgarcia-improving(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/11318/headopened View on GitHub

Showing 3 changed files with 70 additions and 3 deletions.

x
0 of 3 viewed, 3 left
7 additions and 0 deletions.changeset/lucky-bats-listen.mdAdded
View file
@@ -0,0 +1,7 @@
+---
+'@tanstack/react-query': patch
+---
+​
+fix(react-query/HydrationBoundary): hydrate existing queries during SSR
+​
+`HydrationBoundary` holds back hydration of queries that are already in the cache until after the render phase, so that transitions don't update mounted observers mid-render. There is no effect phase during SSR, so those queries were never hydrated at all. A `useQuery` rendered above the boundary creates a cache entry without fetching, which was enough to make the boundary skip the prefetched data and leave a child `useSuspenseQuery` to fetch again on the server.
51 additions and 1 deletionpackages/react-query/src/__tests__/ssr-hydration.test.tsxModified
View file
@@ -1,15 +1,17 @@
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import { hydrateRoot } from 'react-dom/client'
-import { act } from 'react'
+import { Suspense, act } from 'react'
import * as ReactDOMServer from 'react-dom/server'
import { queryKey } from '@tanstack/query-test-utils'
import {
+ HydrationBoundary,
QueryCache,
QueryClient,
QueryClientProvider,
dehydrate,
hydrate,
useQuery,
+ useSuspenseQuery,
} from '..'
import { setIsServer } from './utils'
​
@@ -270,4 +272,52 @@