invalidate not rerunning loader
I'm having a weird issue, all the information i've found suggests it should work but it doesn't. I want to run the
loader using router.invalidate() but I can't get it to actually run the query again.
The following page loads data and sends it to the form component, once the user saves some data I want to invalidate the data so it's reloaded. I do this as suggested by router.invalidate() from useRouter() but nothing happens.
2 Replies
typical-coral•3w ago
just scrolling through after posting a question...i think you can only pass functions as context at the
createRouter level and not on the file route level. Passing funcs used to be a thing but I think that changed recently and it's now reserved for serializable data only. Maybe thats why?extended-salmon•3w ago
I'm just looking and, this is more work in the router's setup functions than I'm used to but it seems to me like you are invalidating the ROUTER but not the queryClient, so the ensureQueryData comes through a second time and it says "well I have data, so I will return it synchronously without re-running the queryFn" -- unless you set that query's staleTime very low, or change it to fetchQuery.
Or, I think the best solution here is to just invalidate the cache key instead of the router! Because you have set up a reactive listener in the RouteComponent (with useSuspenseQuery), invalidating the query will re-run the query function even without the router's loader firing.
I see you have a console.log in there -- can you see if it's running when you expect it to? (Either way, it still seems like invalidating the query is the right approach.)