-
Simplified example with Tanstack Router: export const Route = createRootRouteWithContext<{
queryClient: typeof queryClient;
}>()({
component: Root,
beforeLoad: async ({ context, location }) => {
try {
await context.queryClient.ensureQueryData(getGetUserQueryOptions()); // fetches and errors
} catch (error) {
throw redirect({ to: "/login", search: { nextPath: location.href } });
}
}
},
});
function Root() {
const userQuery = useQuery(getGetUserQueryOptions()); // fetches again immediately
... How can I avoid this? |
Beta Was this translation helpful? Give feedback.
Answered by
TkDodo
Apr 18, 2025
Replies: 1 comment 3 replies
-
if you redirect in beforeLoad, I’d expect the component not to be rendered at all. Is this a |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
that's very likely not what's happening. The query fails and it's put into an error state. But when the consumer mounts, it sees the error, sees that there is no data and puts the query back into pending state and triggers a refetch. That's the expected default behavior. Error'd queries are always stale.
you can set
retryOnMount: false
to change that.