Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-hydration-boundary-disabled-shell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/react-query': patch
---

Fix `HydrationBoundary` deferring hydration for disabled query shells
42 changes: 42 additions & 0 deletions packages/query-core/src/__tests__/hydration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import { QueryClient } from '../queryClient'
import { QueryCache } from '../queryCache'
import { QueryObserver } from '../queryObserver'
import { dehydrate, hydrate } from '../hydration'
import { MutationCache } from '../mutationCache'
import { executeMutation, mockOnlineManagerIsOnline } from './utils'
Expand Down Expand Up @@ -960,6 +961,47 @@ describe('dehydration and rehydration', () => {
})
})

test('should hydrate over an existing disabled query shell', async () => {
const key = queryKey()

const prefetchClient = new QueryClient()
const prefetchPromise = prefetchClient.prefetchQuery({
queryKey: key,
queryFn: () => sleep(10).then(() => 'prefetched'),
})
await vi.advanceTimersByTimeAsync(10)
await prefetchPromise
const dehydrated = dehydrate(prefetchClient)

const hydrationClient = new QueryClient()
const observer = new QueryObserver(hydrationClient, {
queryKey: key,
queryFn: () => Promise.resolve('client'),
enabled: false,
})
const unsubscribe = observer.subscribe(vi.fn())

const disabledShellQuery = hydrationClient.getQueryCache().find({ queryKey: key })

expect(disabledShellQuery?.state).toMatchObject({
data: undefined,
fetchStatus: 'idle',
status: 'pending',
})
expect(disabledShellQuery?.isDisabled()).toBe(true)

hydrate(hydrationClient, dehydrated)

expect(hydrationClient.getQueryCache().find({ queryKey: key })?.state).toMatchObject({
data: 'prefetched',
status: 'success',
})

unsubscribe()
prefetchClient.clear()
hydrationClient.clear()
})

test('should transform promise result', async () => {
const key = queryKey()
const queryClient = new QueryClient({
Expand Down
11 changes: 10 additions & 1 deletion packages/react-query/src/HydrationBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,17 @@ export const HydrationBoundary = ({
dehydratedQuery.dehydratedAt >
existingQuery.state.dataUpdatedAt)

const canHydrateExistingInRender =
existingQuery.state.fetchStatus === 'idle' &&
(existingQuery.state.data === undefined ||
existingQuery.isDisabled())

if (hydrationIsNewer) {
existingQueries.push(dehydratedQuery)
if (canHydrateExistingInRender) {
newQueries.push(dehydratedQuery)
} else {
existingQueries.push(dehydratedQuery)
}
}
}
}
Expand Down
55 changes: 55 additions & 0 deletions packages/react-query/src/__tests__/HydrationBoundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
QueryClientProvider,
dehydrate,
useQuery,
useSuspenseQuery,
} from '..'
import { renderWithClient } from './utils'
import type { hydrate } from '@tanstack/query-core'

describe('React hydration', () => {
Expand Down Expand Up @@ -511,6 +513,59 @@ describe('React hydration', () => {
queryClient.clear()
})

test('should hydrate an existing disabled query before a nested suspense query with the same key mounts', async () => {
const prefetchedData = ['prefetched']
const clientQueryFn = vi.fn(() => sleep(20).then(() => ['client']))
const queryClient = new QueryClient()

const prefetchClient = new QueryClient()
const prefetchPromise = prefetchClient.prefetchQuery({
queryKey: ['string'],
queryFn: () => sleep(10).then(() => prefetchedData),
})
await vi.advanceTimersByTimeAsync(10)
await prefetchPromise
const dehydratedState = dehydrate(prefetchClient)
prefetchClient.clear()

function Header() {
useQuery({
queryKey: ['string'],
queryFn: clientQueryFn,
enabled: false,
})
return null
}

function Child() {
useSuspenseQuery({
queryKey: ['string'],
queryFn: clientQueryFn,
})
return null
}

renderWithClient(
queryClient,
<>
<Header />
<React.Suspense fallback="loading">
<HydrationBoundary state={dehydratedState}>
<Child />
</HydrationBoundary>
</React.Suspense>
</>,
)

expect(queryClient.getQueryCache().find({ queryKey: ['string'] })?.state).toMatchObject({
data: prefetchedData,
status: 'success',
})
expect(clientQueryFn).toHaveBeenCalledTimes(0)

queryClient.clear()
})

test('should not refetch when query has staleTime set to Infinity', async () => {
const queryFn = vi.fn()
const queryClient = new QueryClient()
Expand Down
Loading