TanStackT
TanStack2y ago
17 replies
technological-jade

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>
  )
}


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>
  );
}


When I load this route in my browser, I get the following stack trace:
Unhandled Runtime Error
Error: Maximum call stack size exceeded


Can anyone point me in a direction as to what I'm doing wrong?
Thanks!
Was this page helpful?