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
8.27k stars 664 forks source link

Throwing redirect in beforeLoad still renders component #2820

Closed isaachinman closed 4 days ago

isaachinman commented 4 days ago

Which project does this relate to?

Router

Describe the bug

This could be similar to #2819, but the behaviour I am observing is slightly different:

export const Route = createFileRoute('/some-path')({
  beforeLoad: async () => {
    if (2 + 2 === 4) {
      console.log('throwing')
      throw redirect({
        to: '/some-other-path',
      })
    }
  },
  component: () => {
    console.log('component is running')

    return <Outlet />
  },
})

If I hit this URL, I see this in the console:

throwing
component is running

Apologies if I've misunderstood the purpose of beforeLoad, but isn't this as straightforward a bug as there could be?

EDIT: This is related to #2818. If I set TanStackRouterVite.autoCodeSplitting to false, the bug no longer occurs.

Your Example Website or App

http://example.com

Steps to Reproduce the Bug or Issue

export const Route = createFileRoute('/some-path')({
  beforeLoad: async () => {
    if (2 + 2 === 4) {
      console.log('throwing')
      throw redirect({
        to: '/some-other-path',
      })
    }
  },
  component: () => {
    console.log('component is running')

    return <Outlet />
  },
})

Expected behavior

Component should not be run.

Screenshots or Videos

No response

Platform

Additional context

No response

schiller-manuel commented 4 days ago

please provide a minimal complete example by forking an existing example on stackblitz

isaachinman commented 4 days ago

I've provided the minimal repro via a code snippet, and further follow-up investigation. I have isolated the problem for you.

I will gladly close this issue. You can deal with the next user to report it, and they can jump through your hoops.