T
TanStack8mo ago
equal-jade

Query keeps getting triggered

Hi there, I am using a query in my beforeload function in my router. It's a function that fetches the user session, but I have an issue, that it seems that whenever I navigate anywhere on my app, the function gets called every time, sometimes multiple times:
beforeLoad: async ({ context }) => {
const user = await context.queryClient.fetchQuery({
queryKey: ["user"],
queryFn: ({ signal }) => getUser({ signal }),
});
return { user };
}
beforeLoad: async ({ context }) => {
const user = await context.queryClient.fetchQuery({
queryKey: ["user"],
queryFn: ({ signal }) => getUser({ signal }),
});
return { user };
}
This is my implementation, do I need to add more properties? I sent a screenshot of my terminal it just shows a bunch of calls from me just hovering over a link few times
No description
No description
2 Replies
foreign-sapphire
foreign-sapphire8mo ago
beforeLoad runs in serial whearas the loaders all run in parallel, which is why anything that does a fetch usual goes in loader I'd say if you use ensureQueryData that'll cut down on the fetches
equal-jade
equal-jadeOP8mo ago
Awesome thanks

Did you find this page helpful?