Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,7 @@ export const trpcClient = createTRPCClient<TRPCRouter>({
],
});

let context:
| {
queryClient: QueryClient
trpc: ReturnType<typeof createTRPCOptionsProxy<TRPCRouter>>
}
| undefined

export function getContext() {
if (context) {
return context
}

const queryClient = new QueryClient({
defaultOptions: {
dehydrate: { serializeData: superjson.serialize },
Expand All @@ -48,64 +37,38 @@ export function getContext() {
client: trpcClient,
queryClient: queryClient,
});
context = {
const context = {
queryClient,
trpc: serverHelpers,
}

return context
}

export default function TanStackQueryProvider({
export default function TanstackQueryProvider({
children,
context,
}: {
children: ReactNode
children: ReactNode,
context: ReturnType<typeof getContext>
}) {
const { queryClient } = getContext()
const { queryClient } = context

return (
<QueryClientProvider client={queryClient}>
<TRPCProvider trpcClient={trpcClient} queryClient={queryClient}>
{children}
</TRPCProvider>
</QueryClientProvider>
<TRPCProvider trpcClient={trpcClient} queryClient={queryClient}>
{children}
</TRPCProvider>
)
}
<% } else { %>
import type { ReactNode } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'

let context:
| {
queryClient: QueryClient
}
| undefined
import { QueryClient } from '@tanstack/react-query'

export function getContext() {
if (context) {
return context
}

const queryClient = new QueryClient();

context = {
return {
queryClient,
}

return context
}

export default function TanStackQueryProvider({
children,
}: {
children: ReactNode
}) {
const { queryClient } = getContext()

return (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
)
}
export default function TanstackQueryProvider() {}
Comment on lines 63 to +73
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Non-tRPC branch renders nothing and discards children.

The TanstackQueryProvider component in the non-tRPC branch is completely empty:

  • It doesn't accept children or context props
  • It doesn't render QueryClientProvider
  • Any children passed to it are silently discarded

This breaks the TanStack Query integration when used without tRPC. Users will not have access to QueryClient context, and all useQuery/useMutation hooks will fail.

🐛 Proposed fix to provide QueryClient context
+import type { ReactNode } from 'react'
-import { QueryClient } from '@tanstack/react-query'
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query'

 export function getContext() {
   const queryClient = new QueryClient();

   return {
     queryClient,
   }
 }
-export default function TanstackQueryProvider() {}
+export default function TanstackQueryProvider({
+  children,
+  context,
+}: {
+  children: ReactNode,
+  context: ReturnType<typeof getContext>
+}) {
+  return (
+    <QueryClientProvider client={context.queryClient}>
+      {children}
+    </QueryClientProvider>
+  )
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<% } else { %>
import type { ReactNode } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
let context:
| {
queryClient: QueryClient
}
| undefined
import { QueryClient } from '@tanstack/react-query'
export function getContext() {
if (context) {
return context
}
const queryClient = new QueryClient();
context = {
return {
queryClient,
}
return context
}
export default function TanStackQueryProvider({
children,
}: {
children: ReactNode
}) {
const { queryClient } = getContext()
return (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
)
}
export default function TanstackQueryProvider() {}
<% } else { %>
import type { ReactNode } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
export function getContext() {
const queryClient = new QueryClient();
return {
queryClient,
}
}
export default function TanstackQueryProvider({
children,
context,
}: {
children: ReactNode,
context: ReturnType<typeof getContext>
}) {
return (
<QueryClientProvider client={context.queryClient}>
{children}
</QueryClientProvider>
)
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs`
around lines 63 - 73, The TanstackQueryProvider component currently renders
nothing and drops children; update TanstackQueryProvider to accept props ({
children, context? }) and render <QueryClientProvider
client={context?.queryClient ??
getContext().queryClient}>{children}</QueryClientProvider>, ensuring it uses the
QueryClient from getContext() when no context prop is provided and forwards
children so TanStack Query hooks work outside the tRPC branch; reference the
TanstackQueryProvider and getContext functions and the
QueryClient/QueryClientProvider symbols when making the change.

<% } %>
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
}
],
"integrations": [
{
"type": "provider",
"path": "src/integrations/tanstack-query/root-provider.tsx",
"jsName": "TanStackQueryProvider"
},
{
"type": "devtools",
"path": "src/integrations/tanstack-query/devtools.tsx",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import { createRouter as createTanStackRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'
<% if (addOnEnabled['tanstack-query']) { %>
import { getContext } from './integrations/tanstack-query/root-provider'
import type { ReactNode } from 'react'
import { QueryClient } from '@tanstack/react-query'
import { setupRouterSsrQueryIntegration } from "@tanstack/react-router-ssr-query";
import TanstackQueryProvider, { getContext } from './integrations/tanstack-query/root-provider'
<% } %>

export function getRouter() {
<% if (addOnEnabled['tanstack-query']) { %>
const context = getContext();
<% } %>

const router = createTanStackRouter({
routeTree,
<% if (addOnEnabled['tanstack-query']) { %>
context: getContext(),
<% } else if (addOnEnabled['apollo-client']) { %>
routeTree,<% if (addOnEnabled['tanstack-query']) { %>
context,<% } else if (addOnEnabled['apollo-client']) { %>
context: {} as any,
<% } %>
scrollRestoration: true,
defaultPreload: 'intent',
defaultPreloadStaleTime: 0,
<% if (addOnEnabled.tRPC) { %>
Wrap: (props: { children: ReactNode }) => {
return (
<TanstackQueryProvider context={context}>
{props.children}
</TanstackQueryProvider>
);
},
<% } %>
})

<% if (addOnEnabled['tanstack-query']) { %>
setupRouterSsrQueryIntegration({ router, queryClient: context.queryClient })
<% } %>

return router
}

Expand Down
Loading