TanStack / router

🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering.
https://tanstack.com/router
MIT License
7.27k stars 495 forks source link

Redirect from beforeLoad: uncaught error #1480

Closed bazo closed 1 month ago

bazo commented 1 month ago

Describe the bug

when checking if user is authenticated and throwing a redirect from before load an unhandled error is thrown instead of redirect happenning

Your Example Website or App

https://stackblitz.com/edit/github-qtxl7r?file=src%2Fmain.tsx

Steps to Reproduce the Bug or Issue

run the app open console try to go to /posts

this error is thrown router.ts:1607 Uncaught (in promise) {to: '/login', search: {…}, isRedirect: true, statusCode: 301, headers: {…}, …}

Expected behavior

i am redirected to login page

Screenshots or Videos

No response

Platform

Additional context

No response

tannerlinsley commented 1 month ago

Should be fixed in latest!

gregflynn commented 1 month ago

Still seeing this in 1.28.7

gregflynn commented 1 month ago

as seen in other issues, downgrading to 1.28.1 is a solid workaround

AndrejNemec commented 1 month ago

+1

Another scenario involves manually entering a URL that belongs to an authenticated layout directly into the address bar. If I navigate to that page manually, not using a Link or navigate function, the same error occurs.

bazo commented 1 month ago

yeah still happens, even on 1.28.9

6jonathanaloi6 commented 1 month ago

Still happens in 1.28.9

nick-cheatwood7 commented 1 month ago

as seen in other issues, downgrading to 1.28.1 is a solid workaround

Can confirm, downgrading from 1.28.7 to 1.28.1 fixes the issue temporarily.

planktonrobo commented 1 month ago

Seeing this is broken in latest as well

sher commented 1 month ago

Can confirm this is a degradation from 1.28.2 and still exists in 1.29.2.

Working: https://stackblitz.com/edit/vitejs-vite-qsrurg?file=src%2Frouter.tsx Failing: https://stackblitz.com/edit/vitejs-vite-m1s4ek?file=src%2Frouter.tsx

vitoorgomes commented 1 month ago

using the official example, even on latest version 1.29.2 or 1.28.1, although it works just fine when using the <Link> component to navigate, it keeps throwing the Uncaught error when manually navigating to /dashboard url

junglast commented 1 month ago

As mentioned above, this occurs to me when I initially hard-load the route that includes redirect in the browser address bar. It works well when navigating to that page on client side.

artsiomshaitar commented 1 month ago

Still happens in 1.29.2

angelsantosa commented 1 month ago

Also happens using loader in route. image

andelkocvjetkovic commented 1 month ago

Experiencing the same issue.

felix-bohlin commented 1 month ago

Same here, but it only happens in my prod build. I downgraded to 1.28.1 and it worked again.

"@tanstack/react-router": "^1.29.2",
"@tanstack/router-vite-plugin": "^1.30.0",
"vite": "^5.2.10",
hugocostadev commented 1 month ago

Still happening with the latest version.

Error: image

Code:

"@tanstack/react-router": "^1.30.1",
"@tanstack/router-devtools": "^1.30.1",
"@tanstack/router-vite-plugin": "^1.30.0",
import { createFileRoute, redirect } from "@tanstack/react-router";

export const Route = createFileRoute("/")({
  beforeLoad: () => {
    throw redirect({ to: "/home" });
  },
});
angelsantosa commented 1 month ago

Hey, I just tested the new change, and while redirect does work, now it does not show Outlet content. You can see the behavior here: https://stackblitz.com/edit/vitejs-vite-pcqzrt?file=src%2Fmain.tsx

vafanassieff commented 1 month ago

There is no more error in the console, but the redirect does not load the current page and leave me with an empty screen.

hugocostadev commented 1 month ago

There is no more error in the console, but the redirect does not load the current page and leave me with an empty screen.

Same here

miroslavmih commented 1 month ago

Tested in

@tanstack/react-router": "^1.31.3

and it seems that redirect doesn't work, i.e.

throw redirect({ to: '/' });

will redirect to current page and not to: '/'

mykhailoalekseiev commented 1 month ago

Wrapping the <RouterProvider router={router} /> in Suspense component helped me to resolve this issue.

dedene commented 3 weeks ago

Wrapping the <RouterProvider router={router} /> in Suspense component helped me to resolve this issue.

This does not seem to help for our case. throw redirect({ to: '/login', search: { redirect: location.href }}); is changing the url, but showing a blank screen, suggesting indeed the Outlet is not rendering?

dedene commented 3 weeks ago

Ah, version 1.31.20 seems to have a fix! 🙌 Probably https://github.com/TanStack/router/commit/51eb39b85ec437bb46c831ef388a9dc7ecb23c35 fixed it, it is working after updating. Thanks!!