Based on the knowledge sources, there are two different approaches for preloading data in Next.js with Convex - one using Convex's native preloading mechanism and another using TanStack Query. However, there isn't a direct integration between Convex's preloadQuery and TanStack Query.
For TanStack Query with Convex, you would use the @convex-dev/react-query adapter which provides its own integration pattern:
import { ConvexQueryClient } from "@convex-dev/react-query";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ConvexProvider } from "convex/react";
const convexQueryClient = new ConvexQueryClient(convex);
const queryClient = new QueryClient({
defaultOptions: {
queries: {
queryKeyHashFn: convexQueryClient.hashFn(),
queryFn: convexQueryClient.queryFn(),
},
},
});
convexQueryClient.connect(queryClient);
Then you would use TanStack Query's own mechanisms for data fetching:
import { useQuery } from "@tanstack/react-query";
import { convexQuery } from "@convex-dev/react-query";
import { api } from "../convex/_generated/api";
const { data, isPending, error } = useQuery(
convexQuery(api.functions.myQuery, { id: 123 })
);
According to a Discord thread, one Convex team member mentioned they're working on implementing a pattern that would allow server-side rendering with TanStack Query, but it's not yet available as a standard feature:
"Community Member 669 it's helpful to hear this specifically requested! This is a pattern that makes sense. I'll probably implement it in TanStack Start first where we have more reliable data injection SSR primitives"
If you need server-side rendering with data preloading today, the recommended approach is to either: