Invalid route is redirecting back to "/"
Repro: https://stackblitz.com/edit/tanstack-router-fw4wfg?file=src%2Froutes%2F__root.tsx
Only thing I added is a link to "To not found", and a defaultNotFoundComponent.
When you click on this link I would expect the user to be shown the not found component, but instead they are redirected to "/". How can I fix this so users are actually shown the not found page?
Josh Manning
StackBlitz
Router Quickstart File Based Example (forked) - StackBlitz
Run official live example code for Router Quickstart File Based, created by Tanstack on StackBlitz
13 Replies
like-gold•2y ago
your link to /test is not a router <Link> but an <a>
genetic-orangeOP•2y ago
That's intentional, a Link (should?) give a TS error, but even with it as a Link it behaves the same way.
Sorry, I was incorrect, the Link seems to route to the not found page.
But the
<a> tag, as well as just navigating in your url bar to an invalid route should show you the not found route rather than redirect you.like-gold•2y ago
this must be supported by the server serving the index.html
so if you try to access /test and it does not exist on the server, your server must deliver the index.html
THEN the client side routing can trigger the not found handling
genetic-orangeOP•2y ago
Unless I'm misunderstanding, the server is certainly serving the index.html, even for these "not found routes". This is what the stackblitz example above is doing, correct?
like-gold•2y ago
no idea how Stackblitz does it really
genetic-orangeOP•2y ago
Stackblitz is just serving an SPA, so any url is going to be passed to the index.html file.
You can see this by checking the behavior when you refresh the page in that repro. Go to the About page, refresh (it stays there). Go to the "not found" page, refresh (it navigates back to /)
Vite / Stackblitz aren't aware of these specific routes, so somethings happening in the TSR side I believe when the page loads on a not found route
like-gold•2y ago
can you please create a github issue so this does not get lost here?
xenial-black•2y ago
This one might be my bad, looking into it now
nvm it was this PR https://github.com/TanStack/router/pull/1293
like-gold•2y ago
@iDarkLightning can you provide a fix nevertheless?
xenial-black•2y ago
yeah im trying
@Tanner Linsley do you remember why it is that you removed this: https://github.com/TanStack/router/pull/1248/files#diff-79f4d04ee5888e5303ef436722c9e5538cc02edcc56717919d77f6f090c339a0L145 because that PR just adds it back
ratty-blush•2y ago
We need to figure out the condition to have it run and when not to run
Most of the time, it's right. We want to sync the url with the state we have in the router
But I ran into a situation with SSR where that wasn't the case
The safer default is to sync, so we should add it back
and then I'll deal with the weirdness in SSR with some custom logic
xenial-black•2y ago
ratty-blush•2y ago
Excellent!