T
TanStack•3y ago
exotic-emerald

Understanding whether a fetch is made on the server

Hi there, Sorry if this is a noob question. I have a call that I only intend to make client-side (don't want it during SSR) and am trying to figure out whether a fetch is being made.
export const useUser = () => {
const getCurrentUser = trpc.auth.getCurrentUser.useQuery(undefined, {
refetchOnWindowFocus: false
});

console.log(getCurrentUser.isFetching);

return {
...getCurrentUser,
user: getCurrentUser.data
};
};
export const useUser = () => {
const getCurrentUser = trpc.auth.getCurrentUser.useQuery(undefined, {
refetchOnWindowFocus: false
});

console.log(getCurrentUser.isFetching);

return {
...getCurrentUser,
user: getCurrentUser.data
};
};
I can see that during SSR isFetching gets set to true, which implies I am making an unneeded fetch on the server? I don't want to use hydration or initialData or anything for this one, just make the call client-side.
3 Replies
exotic-emerald
exotic-emeraldOP•3y ago
Is an option just to do something like:
enabled: typeof window !== "undefined"
enabled: typeof window !== "undefined"
? Do I need to add that?
complex-teal
complex-teal•3y ago
queries don't run on the server unless you expclicitly prefetch, or you use suspense and have a framework that can do streaming (like next13+ app dir)
exotic-emerald
exotic-emeraldOP•3y ago
Thanks @TkDodo 🔮 !

Did you find this page helpful?