-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix: add (de)hydration options to tanstack query integration #5186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,6 +5,8 @@ import { | |||||||||
import { isRedirect } from '@tanstack/router-core' | ||||||||||
import type { AnyRouter } from '@tanstack/router-core' | ||||||||||
import type { | ||||||||||
DehydrateOptions, | ||||||||||
HydrateOptions, | ||||||||||
QueryClient, | ||||||||||
DehydratedState as QueryDehydratedState, | ||||||||||
} from '@tanstack/query-core' | ||||||||||
|
@@ -20,6 +22,8 @@ export type RouterSsrQueryOptions<TRouter extends AnyRouter> = { | |||||||||
* @link [Guide](https://tanstack.com/router/latest/docs/framework/react/api/router/redirectFunction) | ||||||||||
*/ | ||||||||||
handleRedirects?: boolean | ||||||||||
hydrateOptions?: Omit<HydrateOptions, 'shouldDehydrateQuery'> | ||||||||||
dehydrateOptions?: DehydrateOptions | ||||||||||
} | ||||||||||
|
||||||||||
type DehydratedRouterQueryState = { | ||||||||||
|
@@ -31,6 +35,8 @@ export function setupCoreRouterSsrQueryIntegration<TRouter extends AnyRouter>({ | |||||||||
router, | ||||||||||
queryClient, | ||||||||||
handleRedirects = true, | ||||||||||
dehydrateOptions, | ||||||||||
hydrateOptions, | ||||||||||
}: RouterSsrQueryOptions<TRouter>) { | ||||||||||
const ogHydrate = router.options.hydrate | ||||||||||
const ogDehydrate = router.options.dehydrate | ||||||||||
|
@@ -50,7 +56,10 @@ export function setupCoreRouterSsrQueryIntegration<TRouter extends AnyRouter>({ | |||||||||
queryStream: queryStream.stream, | ||||||||||
} | ||||||||||
|
||||||||||
const dehydratedQueryClient = queryDehydrate(queryClient) | ||||||||||
const dehydratedQueryClient = queryDehydrate( | ||||||||||
queryClient, | ||||||||||
dehydrateOptions, | ||||||||||
) | ||||||||||
if (dehydratedQueryClient.queries.length > 0) { | ||||||||||
dehydratedQueryClient.queries.forEach((query) => { | ||||||||||
sentQueries.add(query.queryHash) | ||||||||||
|
@@ -93,6 +102,7 @@ export function setupCoreRouterSsrQueryIntegration<TRouter extends AnyRouter>({ | |||||||||
sentQueries.add(event.query.queryHash) | ||||||||||
queryStream.enqueue( | ||||||||||
queryDehydrate(queryClient, { | ||||||||||
...dehydrateOptions, | ||||||||||
shouldDehydrateQuery: (query) => { | ||||||||||
if (query.queryHash === event.query.queryHash) { | ||||||||||
return ( | ||||||||||
|
@@ -110,15 +120,19 @@ export function setupCoreRouterSsrQueryIntegration<TRouter extends AnyRouter>({ | |||||||||
await ogHydrate?.(dehydrated) | ||||||||||
// hydrate the query client with the dehydrated data (if it was dehydrated on the server) | ||||||||||
if (dehydrated.dehydratedQueryClient) { | ||||||||||
queryHydrate(queryClient, dehydrated.dehydratedQueryClient) | ||||||||||
queryHydrate( | ||||||||||
queryClient, | ||||||||||
dehydrated.dehydratedQueryClient, | ||||||||||
hydrateOptions, | ||||||||||
) | ||||||||||
} | ||||||||||
|
||||||||||
// read the query stream and hydrate the queries as they come in | ||||||||||
const reader = dehydrated.queryStream.getReader() | ||||||||||
reader | ||||||||||
.read() | ||||||||||
.then(async function handle({ done, value }) { | ||||||||||
queryHydrate(queryClient, value) | ||||||||||
queryHydrate(queryClient, value, hydrateOptions) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick Add null/undefined check before hydrating streamed values. When reading from the query stream, the Apply this diff to add a safety check: - queryHydrate(queryClient, value, hydrateOptions)
+ if (value) {
+ queryHydrate(queryClient, value, hydrateOptions)
+ } 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||
if (done) { | ||||||||||
return | ||||||||||
} | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the
hydrateOptions
type definition to avoid potential issues.The
shouldDehydrateQuery
property should not be omitted fromHydrateOptions
. According to the TanStack Query API,HydrateOptions
does not include ashouldDehydrateQuery
property - that property belongs toDehydrateOptions
. This appears to be a copy-paste error.Apply this diff to fix the type:
📝 Committable suggestion
🤖 Prompt for AI Agents