Skip to content

Throwing primitives in beforeLoad causes the router to stop rendering #4707

Closed

Description

@michaelbull

Which project does this relate to?

Router

Describe the bug

Calling throw in a beforeLoad with primitive types such as string or number causes the entire router to hang - preventing the errorComponent from rendering.

This leads to the entire application effectively no longer rendering the router.

Your Example Website or App

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

Steps to Reproduce the Bug or Issue

Implement a beforeLoad in a route that calls throw with a string or number.

Expected behavior

I expect the value to be thrown and rendered in the errorComponent, irrespective of its data type.

Screenshots or Videos

Image

Platform

"@tanstack/react-router": "1.128.3"

Additional context

No response

Activity

  1. changed the title [-]Throwing primitives in beforeLoad causes the router to hang[/-] [+]Throwing primitives in beforeLoad causes the router to stop rendering[/+] on Jul 19, 2025
  2. leesb971204 commented on Jul 19, 2025

    @leesb971204
    Contributor

    It seems that this issue occurs when err is a primitive type at that line.

    Replacing err with an object type does resolve the issue, but I’m not sure if that’s actually necessary.
    Do you think it’s common for users to throw primitive values instead of objects?

    cc. @schiller-manuel
    If needed, I’d be happy to create a PR and work on it.

  3. michaelbull commented on Jul 19, 2025

    @michaelbull
    Author

    Do you think it’s common for users to throw primitive values instead of objects?

    To answer your question I would pose the following hypothetical: what if the user is not in control of what they are throwing?

    Consider the situation in which the user has adopted an RPC library that internally throws errors, and some of those errors are primitive values. They interact with this hypothetical library in a loader, presuming that any errors that occur from the library they are using will be handled by the loader as the docs describe. They are completely unaware that (a) the library they are using is throwing primitives, (b) their application will break because their router will stop rendering as soon as this hypothetical library errors.

    Fundamentally, it should not matter "what" the user throws - the router should be resilient to it. The language specification lets you throw primitives, therefore the router should.

    Either way, the entire router just stopping rendering when this happens seems unacceptable, even if the team considers throwing primitives in loaders an unacceptable strategy. If this is an intentional decision, it should break in a way that's clear to the consumer and tells them that throwing a primitive is unsupported.

  4. michaelbull commented on Jul 19, 2025

    @michaelbull
    Author

    It seems that this issue occurs when err is a primitive type at that line.

    @leesb971204 based on the line that you linked, this issue probably relates heavily to the other issue I opened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions