Skip to content

Throwing errors in beforeload #6097

@Zefty

Description

@Zefty

Which project does this relate to?

Router

Describe the bug

Let say I have a protected route createFileRoute("/(app)/_authed") where I will throw an error

e.g. if (!authSession) { throw new Error("Unauthorized", { cause: 401 }); }

Right now this is working only client-side. The error message and cause is caught and displayed by the defaultErrorComponent.

Image

However if i do a full page refresh and fetch from the server with SSR I get a brief flash of the "401 Unauthorised" but then it seems like after the page hydrates, it then shows "Undefined Unauthorised" as the cause from the error is gone.

Image

Im suspecting this might be related to the GET http://localhost:3000/my-todos 500 (Internal Server Error) call.

Your Example Website or App

https://github.com/Zefty/tanstack-playground

Steps to Reproduce the Bug or Issue

  1. Clone https://github.com/Zefty/tanstack-playground
  2. Run pnpm dev
  3. Navigate to http://localhost:3000/my-todos which should be an authed route. This should show "401 Unauthorised"
  4. Click refresh button of browser. This should show "undefined Unauthorized" which is wrong as there as a flash of "401 Unauthorised"

Expected behavior

Both client-side navigation and ssr should show the same page

"401 Unauthorised"

Screenshots or Videos

No response

Platform

  • Router / Start Version: [e.g. 1.121.0]
  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Browser Version: [e.g. 91.1]
  • Bundler: [e.g. vite]
  • Bundler Version: [e.g. 7.0.0]

Additional context

No response

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