W
Wasp•3mo ago
Zeke

Stop Refetching on Focus?

Any way to make it not refetch the /auth/me and my custom /get-user endpoints all the time? it's messing up some stuff in my app. I attempted to add some config to my useQuery(getUser), but that didn't help:
{
refetchOnWindowFocus: false,
refetchOnMount: false,
cacheTime: 10000000,
}
{
refetchOnWindowFocus: false,
refetchOnMount: false,
cacheTime: 10000000,
}
Not sure if I can do anything about /auth/me. I think that might be what's causing /get-user to rerun as well. So that this isn't an XY-problem, here's the issue I'm having: - I have a useUser hook that uses a UserContext and a full data wrapper that does status for loading/error/{children} that basically guarantees that wherever I do useUser the data is there. I don't like doing data?.xyz 😅 - I have a dialog. it lets you edit profile settings, basically. Works flawlessly... but if you edit the data and then change tabs, it refetches user data and clears the edited state. Perhaps this is just a bad way of doing it, but it's the only thing I've come up with.
4 Replies
kapa.ai
kapa.ai•3mo ago
Hi @Zeke. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you can control the refetching behavior of your queries in Wasp, since Wasp's useQuery hook is a thin wrapper around react-query's useQuery. You can pass options like refetchOnWindowFocus and refetchOnMount to your query, and these should work as expected:
const { data: user } = useQuery(getUser, undefined, {
refetchOnWindowFocus: false,
refetchOnMount: false,
cacheTime: 10000000,
})
const { data: user } = useQuery(getUser, undefined, {
refetchOnWindowFocus: false,
refetchOnMount: false,
cacheTime: 10000000,
})
If this isn't working for your custom /get-user endpoint, double-check that you're passing the options as the third argument to useQuery, as shown above. Wasp does not expect you to supply the cache key, but the options object is supported and should be respected by the underlying react-query instance. If you want to change the global defaults for all queries (including /auth/me), you can do so in your client setup function using configureQueryClient:
import { configureQueryClient } from 'wasp/client/operations'

export default async function mySetupFunction() {
configureQueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
refetchOnMount: false,
cacheTime: 10000000,
},
},
})
}
import { configureQueryClient } from 'wasp/client/operations'

export default async function mySetupFunction() {
configureQueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
refetchOnMount: false,
cacheTime: 10000000,
},
},
})
}
This will apply your desired defaults to all queries, including the /auth/me endpoint, which is used internally by Wasp for authentication state management. For more details, see the docs on overriding default behaviour for queries and the useQuery hook. Note: If you are still seeing unexpected refetches, it could be due to other parts of your app (such as cache invalidation after actions or automatic entity-based invalidation) or how your dialog manages local state. Wasp will automatically invalidate and refetch queries when related entities are updated by actions, which could also cause your dialog state to reset if it relies on query data being "fresh" after an action is performed Cache Invalidation. If you want to prevent the dialog from losing its edited state, consider managing the form state locally (e.g., with React state) and only syncing with the server when the user saves, rather than relying on the query data to persist unsaved changes. If after these changes you still experience issues, there may be a bug or an edge case in Wasp's integration with react-query—in that case, consider opening an issue with a minimal reproduction. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Zeke, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Zeke
ZekeOP•3mo ago
you aren't having issues, man. chill out
Filip
Filip•2mo ago
Hahaha, yeah, the bot is right We do need to work on its self esteem though 🙂
Zeke
ZekeOP•2mo ago
yup, it worked. thanks!

Did you find this page helpful?