T
TanStack2y ago
genetic-orange

Maximum callstack exceeded when using `prefetchQuery` in NextJS 14

Hey there! I recently discovered this library and have been trying to swap out my generic OpenAPI client using fetch with react-query using axios. I'm in a NextJS 14 project using the app router and trying to handle SSR for initial load by following the Advanced SSR guide for the NextJS app router. I have the following code:
import {
dehydrate,
HydrationBoundary,
QueryClient,
} from '@tanstack/react-query'

import { getLeaderboard } from "@/lib/stormgate/api";

export default async function LeaderboardPage() {
const queryClient = new QueryClient()

await queryClient.prefetchQuery({
queryKey: ["leaderboard"],
queryFn: () => {
return getLeaderboard();
}
})

return (
<HydrationBoundary state={dehydrate(queryClient)}>
<span>Test</span>
</HydrationBoundary>
)
}
import {
dehydrate,
HydrationBoundary,
QueryClient,
} from '@tanstack/react-query'

import { getLeaderboard } from "@/lib/stormgate/api";

export default async function LeaderboardPage() {
const queryClient = new QueryClient()

await queryClient.prefetchQuery({
queryKey: ["leaderboard"],
queryFn: () => {
return getLeaderboard();
}
})

return (
<HydrationBoundary state={dehydrate(queryClient)}>
<span>Test</span>
</HydrationBoundary>
)
}
Where getLeaderboard is an axios-based function generated by https://orval.dev. I've set up my providers as followed like the guide:
"use client";

import { useState, type ReactNode } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";

export function QueryProvider({ children }: { children: ReactNode }) {
const [queryClient] = useState(
() =>
new QueryClient({
defaultOptions: {
queries: {
staleTime: 60 * 1000,
},
},
}),
);

return (
<QueryClientProvider client={queryClient}>
{children}
<ReactQueryDevtools />
</QueryClientProvider>
);
}
"use client";

import { useState, type ReactNode } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";

export function QueryProvider({ children }: { children: ReactNode }) {
const [queryClient] = useState(
() =>
new QueryClient({
defaultOptions: {
queries: {
staleTime: 60 * 1000,
},
},
}),
);

return (
<QueryClientProvider client={queryClient}>
{children}
<ReactQueryDevtools />
</QueryClientProvider>
);
}
When I load this route in my browser, I get the following stack trace:
Unhandled Runtime Error
Error: Maximum call stack size exceeded
Unhandled Runtime Error
Error: Maximum call stack size exceeded
Can anyone point me in a direction as to what I'm doing wrong? Thanks!
8 Replies
fascinating-indigo
fascinating-indigo2y ago
useQueryClient You create a qc on every render
fair-rose
fair-rose2y ago
@M00LTi I only see new QueryClient() in the server component, where it's fine ?
fascinating-indigo
fascinating-indigo2y ago
You are right! Maybe a full stack trace would help…
genetic-orange
genetic-orangeOP2y ago
i do make a queryClient in the provider file that’s used on client orval also exports functions per query to get query options so i changed it to this: await queryClient.prefetchQuery(getGetLeaderboardQueryOptions()); i still get the same issue 😦 that function returns this:
return { queryKey, queryFn, ...queryOptions } as UseQueryOptions<
Awaited<ReturnType<typeof getLeaderboard>>,
TError,
TData
> & { queryKey: QueryKey };
};
return { queryKey, queryFn, ...queryOptions } as UseQueryOptions<
Awaited<ReturnType<typeof getLeaderboard>>,
TError,
TData
> & { queryKey: QueryKey };
};
genetic-orange
genetic-orangeOP2y ago
No description
genetic-orange
genetic-orangeOP2y ago
sadly the full trace isn't that helpful https://codesandbox.io/p/devbox/nextjs-react-query-mv3y2l @TkDodo 🔮 here's a codesandbox of my implementation if this helps 🙂
fair-rose
fair-rose2y ago
you return a full axios response object, which is recursive and not serializable extract data and just return that first thing to do is to just return { foo: "bar" } from your queryFn to rule out anything with actual data fetching and of course it works with that: https://codesandbox.io/p/devbox/nextjs-react-query-forked-jq98tj
genetic-orange
genetic-orangeOP2y ago
i don't have permission to view that it seems but i think i get the idea thanks!

Did you find this page helpful?