Possible to make child route's `beforeLoad()` not run when parent route throws a redirect?
Given these routes:
*
/accounts/$accountId/route.tsx
* /accounts/$accountId/settings/billing.tsx
We use route param placeholders so we can link to account routes from emails, e.g. to /accounts/_/settings/billing
and we replace the _
value with the currently logged in $accountId
.
We also have some logic in the BillingRoute
's beforeLoad
that checks if the account has access. It seems that despite a throw redirect()
in the /accounts/$accountId
's beforeLoad()
, the code for the billing route is triggered, causing issues since _
isn't an actually valid ID.
Thoughts?22 Replies
grumpy-cyan•13mo ago
not sure what a route param placeholder is... Can you please provide a minimal complete example by forking one of the examples on stackblitz?
like-gold•7mo ago
I am having the same issue, why is after a throw redirect(), children matches still run?
grumpy-cyan•7mo ago
where are you throwing that redirect? in beforeLoad?
like-gold•7mo ago
Yes
I am on
1.53.1
if it helpsgrumpy-cyan•7mo ago
ancient! 😄
like-gold•7mo ago
I can't update
grumpy-cyan•7mo ago
why
like-gold•7mo ago
GitHub
Cannot update path params when navigating to
'.'
· Issue #2415 · ...Which project does this relate to? Router Describe the bug I wrote a hook which should replace the slug in the URL if it doesn't match reality anymore. For example /projects/here-is-the-name-12...
grumpy-cyan•7mo ago
so beforeLoad should execute serially and as soon as one throws a redirect (or anything), the next child's beforeLoad should not execute
oh well we need to solve this then
like-gold•7mo ago
That is what I was expecting as well, but it doesn't work like that
You're right, it works that way if I throw a redirect directly, but I am throwing the redirect within a promise...
I tried throwing a custom error within that promise (await queryClient.fetchQuery), but it doesn't stop child beforeLoad calls either
grumpy-cyan•7mo ago
can you please provide a complete beforeLoad that does this?
like-gold•7mo ago
You mean with StackBlitz, right?
On it
grumpy-cyan•7mo ago
ideally a complete minimal example yes
like-gold•7mo ago
I wasn't able to reproduce the problem I am having with my project 😦
https://stackblitz.com/edit/github-b9agtn-uqnwv8hq?file=src%2Fmain.tsx
Luis Merino
StackBlitz
Router match IDs include "undefined" (forked) - StackBlitz
Run official live example code for Router Quickstart, created by Tanstack on StackBlitz
grumpy-cyan•7mo ago
if you can share your actual code I could have a look
like-gold•7mo ago
Oh yes now I was
A timeout of 2 seconds did it
I changed
If you remove the timeout, it goes to logout directly, but otherwise it hits the child's beforeLoad
grumpy-cyan•7mo ago
maybe thats a bug with the min pending time
can you play with those values (minPendingMs etc) and check whether it has an influence ?
like-gold•7mo ago
yep
First off, setting the
defaultPendingMinMs: 0
instead of the 500ms is set by default fixed the issue
How did you know?grumpy-cyan•7mo ago
grumpy-cyan•7mo ago
can you please create a GitHub issue for this?
like-gold•7mo ago
GitHub
Child route's
beforeLoad()
run when parent route throws a delayed...Which project does this relate to? Router Describe the bug Inside beforeLoad If a redirect or any kind of error is delayed within a promise, child beforeLoad get executed, triggering unwanted logic...
like-gold•7mo ago
Thanks for everything@Manuel Schiller
Saw your comment: https://github.com/TanStack/router/issues/3416#issuecomment-2659598368
If you still can't reproduce it, please let me know.
After speaking with Manuel, we weren't able to reproduce it sadly anymore...