Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -58,10 +47,12 @@ export function getContext() {

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

return (
<QueryClientProvider client={queryClient}>
Expand All @@ -75,32 +66,22 @@ export default function TanStackQueryProvider({
import type { ReactNode } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'

let context:
| {
queryClient: QueryClient
}
| undefined

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

const queryClient = new QueryClient();

context = {
return {
queryClient,
}

return context
}

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

return (
<QueryClientProvider client={queryClient}>
Expand Down
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,42 @@
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 { 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(),
context,
<% } else if (addOnEnabled['apollo-client']) { %>
context: {} as any,
<% } %>
scrollRestoration: true,
defaultPreload: 'intent',
defaultPreloadStaleTime: 0,
<% if (addOnEnabled['tanstack-query']) { %>
Wrap: (props: { children: React.ReactNode }) => {
return (
<TanStackQueryProvider context={context}>
{props.children}
</TanStackQueryProvider>
);
},
<% } %>
})

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

return router
}

Expand Down
Loading