T
TanStack17mo ago
eastern-cyan

`loader` function doesn't have access to `context` after being redirected to

I load a route and check some things in the loader function. If it errors, I throw redirect to another route. That route, however, doesn't seem to have access to the context in its loader function. routes/offers.tsx
...
loader: () {
try {
// some api call
} catch (err) {
throw redirect({ to: "/", search: { error: "missing_offer" } });
}
}
...
...
loader: () {
try {
// some api call
} catch (err) {
throw redirect({ to: "/", search: { error: "missing_offer" } });
}
}
...
routes/index.tsx
loader: async ({ deps, context, location }) => {
console.log("loader", context); // loader is undefined!
}
loader: async ({ deps, context, location }) => {
console.log("loader", context); // loader is undefined!
}
9 Replies
eastern-cyan
eastern-cyanOP17mo ago
It might be worth noting that the beforeLoad function DOES have access to context however
sunny-green
sunny-green17mo ago
This looks to be related to this issue: https://github.com/TanStack/router/issues/1531 If you've got a reproduction, it'd help being able to pinpoint the issue.
GitHub
Context is undefined after redirect · Issue #1531 · TanStack/router
Describe the bug I use authentication, similar to Authenticated Routes example from documentation. So, when user is on /$lang page, if user is not authorised, user is been redirecting to /$lang/log...
eastern-cyan
eastern-cyanOP17mo ago
Ah yes, looks related. Thanks!
fair-rose
fair-rose17mo ago
I have also encountered this issue. Interesting observation I found, calling the navigate exposed in the beforeLoad callback, does not cause the issue. Not sure if helpful or not
sunny-green
sunny-green17mo ago
That's probably because navigate directly changes the matches internally. On the flip-side the fact that it is not preload friendly makes it more of a problem that anything and why its been deprecated.
extended-salmon
extended-salmon16mo ago
@Sean Cassiere @Manuel Schiller the "context is undefined after redirect" issue is back and it's quite easy to reproduce. Can you please have a look here? https://github.com/TanStack/router/issues/1670#issuecomment-2144897100
GitHub
Undefined context after redirect · Issue #1670 · TanStack/router
Describe the bug Seems like the issue with undefined context is not fully resolved yet. When redirecting I'm seeing undefined context. Some criteria to trigger the issue defaultPreload: 'in...
sunny-green
sunny-green16mo ago
I'll be online a couple days. Will look into this. There was a PR opened I think about this. Will check.
other-emerald
other-emerald16mo ago
Does someone know what version reimplemented this bug ?
correct-apricot
correct-apricot16mo ago
I found another edge case in v1.34.9 that causes context to become undefined if the route that throws the redirect has search params that are being validated through validateSearch, and the validation fails/throws (e.g. with zod). Here's a complete repro: https://stackblitz.com/edit/tanstack-router-qbczsv?file=src%2Froutes%2Findex.tsx
Melvin Valster
StackBlitz
Router Redirect undefined context - StackBlitz
Run official live example code for Router Basic File Based, created by Tanstack on StackBlitz

Did you find this page helpful?