Skip to content

Middleware: errors caught at root error boundary, not route error boundary #13529

Closed
@mpqmpqm

Description

@mpqmpqm

I'm using React Router as a...

framework

Reproduction

Middleware: errors caught at root error boundary, not route error boundary

Minimal repro

Under certain conditions route-level middleware errors are caught at root.tsx rather than at route-defined error boundaries.

Additionally, different errors are reported for document loads vs. client navigation.

Please note: Comment the loader in the auth layout on and off to see the expected behavior when no loader is defined for that layout and the unexpected behavior when a loader is defined.

Issue template

What version of React Router are you using?

7.5.3

Steps to Reproduce

Minimal repro

  1. Starting at index, click Account link. provideAccount middleware throws and is caught by root.tsx error boundary.

    1. Reload the page to see different errors reported between client navigation and document load.

      Client navigation: Cannot use 'in' operator to search for 'error' in undefined.
      vs. Document load: Expect Error Boundary: account.tsx

  2. Comment out the loader in the auth layout and observe that errors are caught at the expected route error boundaries.

System Info

System:
    OS: macOS 15.4.1
    CPU: (8) arm64 Apple M1
    Memory: 204.30 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.11.0 - ~/.nvm/versions/node/v22.11.0/bin/node
    npm: 11.1.0 - ~/.nvm/versions/node/v22.11.0/bin/npm
    pnpm: 10.10.0 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 135.0.7049.115
    Edge: 135.0.3179.98
    Safari: 18.4
  npmPackages:
    @react-router/dev: ^7.5.3 => 7.5.3 
    @react-router/node: ^7.5.3 => 7.5.3 
    @react-router/serve: ^7.5.3 => 7.5.3 
    react-router: ^7.5.3 => 7.5.3 
    vite: ^6.3.3 => 6.3.4

Used Package Manager

npm

Expected Behavior

Errors thrown in route-specific middleware should be caught by the route's error boundary. Errors should be consistent between document loads and client navigation.

Actual Behavior

Errors thrown in route-specific middleware are caught by the root error boundary. Different errors are reported for document loads vs. client navigation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions