Best practice for redirecting after a fetch
Hi everyone,
I'm working on a routing setup and need some guidance on the best way to handle a redirect after fetching data. Here's my scenario:
1. I have two routes:
/users
and /users/$userId
2. The purpose of the /users
route is to redirect to /users/$userId
for the first user fetched from my backend.
3. I've attempted to use throw redirect()
in the loader, but it's not working as expected due to preloading.
Here's what I'm trying to achieve:
- When a user visits /users
, the app should fetch the first user from the backend.
- Then, it should immediately redirect to /users/$userId
where $userId
is the ID of the first user fetched.
I'd appreciate any suggestions on how to implement this efficiently and correctly. Are there any best practices or specific methods I should be using for this kind of redirect after a fetch? Should I do in the component ?
Thanks in advance for your help!2 Replies
genetic-orange•15mo ago
"not working as expected"
is this the same issue as this here?
https://github.com/TanStack/router/issues/1882
GitHub
BeforeLoad causing infinite pending state when redirecting to curre...
Describe the bug I've noticed that when you are on a route, and then you hover over another link with preload: 'intent' enabled, and said route throws a redirect to the current route yo...
useful-bronzeOP•15mo ago
Ah yes, It looks the same !