TanStackT
TanStack2y ago
4 replies
conventional-black

Preventing a specific query from triggering `onError`

I have the following client setup

export const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      retry: 2,
      staleTime: Infinity,
    },
  },
  queryCache: new QueryCache({
    onError: (error) => toast.error(error.message),
  }),
});


And in my RootRoute, I fetch user data on
beforeLoad
function and immediately showing the pendingCompoent. When the query fails, it triggers the toast.error and then redirects.

export const Route = createRootRoute({
  pendingMs: 0,
  pendingMinMs: 1000,
  // wrapInSuspense: https://github.com/TanStack/router/pull/1611
  wrapInSuspense: true,
  beforeLoad: async () => {
    try {
      await queryClient.fetchQuery({
        retry: 1,
        queryFn: getCurrentUserQuery,
        queryKey: ['current_user'],
      });
    } catch {
      redirect({ to: '/sign_in' });
    }
  },
  pendingComponent: () => <h1>Loading...</h1>,
  component: () => (
    <>
      <Navigator />
      <Outlet />
      <TanStackRouterDevtools />
    </>
  ),
});


Is there a way to prevent this specific query used in
beforeLoad
to trigger the toast message?
Was this page helpful?