[Begginer] How to redirect without refetching? (tanstack router and tanstack query) #2257
Unanswered
Crossoufire
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I'm looking for a way to do a redirect without refetching data. I'll try to explain my situation in detail with an example:
loader
from TanStack Router, which fetches data from my API. My API, in turn, calls TMDB's API which returns the trending movies to be displayed.Link
component to the route:/details/$mediaType/$mediaId?external=True
Link
component instead ofonClick
to preserve the ability to open links in new tabs (e.g., using Ctrl+Click).loader
from the details page.api_id
orid
attribute, depending on theexternal
argument.external
argument is set toTrue
.The issue I'm facing is that I always want my
internal mediaId
to be displayed (and saved in Tanstack query), but without refetching the data already fetched using theexternal mediaId
. I’ve found a solution that works, but it feels more like a workaround. If anyone has a better approach, I'd appreciate the advice.Here’s my current solution:
Here, I first check if cached data exists for the
internal mediaId
. It does not (always in the example where the user comes from the trending page with external Ids), so I query the data, then set the query data using theinternal mediaId
from my DB (I create the cache). Then, because theexternal
argument isTrue
, I redirect to the same route but with theinternal mediaId
, and because the cache now exists it returns the cached data (I've also set a staleTime of 2 seconds to prevent refetching when the page loads from the cache).Beta Was this translation helpful? Give feedback.
All reactions