TanStackT
TanStack5d ago
6 replies
hidden-sapphire

Redirect based on tanstack query result in `beforeLoad`

Have a case where I want to redirect based on the result of a query. Is there an idiomatic approach to which query method to use in this case? i.e. queryClient.fetchQuery() or queryClient.ensureQueryData()

For example:

beforeLoad: ({ context }) => {
  // context.queryClient.fetchQuery(...)
  // or...
  // context.queryClient.ensureQueryData({ revalidateIfStale: true, ... })
  const foo = ...

  if (!foo) {
    throw redirect(...)
  }
}


I've always been using ensureQueryData() but NOT specifying revalidateIfStale , which I think is the cause of an issue in my app because it's redirecting based on stale data that is never being refetched.
Was this page helpful?