Skip to content

Middleware next() function preventing handleError() from being called in resource routes #13936

Open
@rossipedia

Description

@rossipedia

Reproduction

(🇺🇸🎆 Happy 4th! 🎆🇺🇸)

Go to https://stackblitz.com/~/github.com/rossipedia/rr-middleware-handle-error-shenanigans

Make a request to /, you should see *** HANDLE ERROR *** in the terminal output.

Make a request to /?next=true. You should NOT see the handle error output.

The only difference between those two scenarios is that one calls next() in a middleware, and one does not.

Note

This only seems to affect Resource Routes. Exporting a default component from _index.tsx causes handleError() to be invoked as expected in both cases.

• Update •

Looks like this only happens with Resource Route document requests; ie: they do not have the .data suffix.

I've updated the repo to rename _index.tsx to test.tsx (because I couldn't remember how to trigger a data request for _index 😅).

# DOES NOT produce the `handleError` output
❯ curl 'localhost:5173/test?next=true'
Error: Foo

# DOES produce the `handleError` output
❯ curl 'localhost:5173/test.data?next=true'
[{"_1":2},"routes/test",{"_3":4},"error",["SanitizedError",5,6,7],"Error","Foo","Error: Foo\n    at loader ...

System Info

System:
    OS: macOS 15.5
    CPU: (16) arm64 Apple M4 Max
    Memory: 2.43 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.16.0 - ~/.local/state/fnm_multishells/3459_1751671592366/bin/node
    Yarn: 1.22.22 - ~/.local/state/fnm_multishells/3459_1751671592366/bin/yarn
    npm: 11.4.2 - ~/src/rr/rr-middleware-handle-error-shenanigans/node_modules/.bin/npm
    pnpm: 10.12.1 - ~/.local/state/fnm_multishells/3459_1751671592366/bin/pnpm
    bun: 1.2.18 - /opt/homebrew/bin/bun
  Browsers:
    Chrome: 137.0.7151.120
    Safari: 18.5
  npmPackages:
    @react-router/dev: ^7.6.1 => 7.6.3 
    @react-router/fs-routes: ^7.6.1 => 7.6.3 
    @react-router/node: ^7.6.1 => 7.6.3 
    @react-router/serve: ^7.6.1 => 7.6.3 
    react-router: ^7.6.1 => 7.6.3 
    vite: ^6.3.5 => 6.3.5

Used Package Manager

npm

Expected Behavior

I expect the same behavior in both cases:

handleError should be invoked when an error is thrown, regardless of whether or not there is a middleware that calls next() or not.

Actual Behavior

If the middleware calls next(), then the error is never propagated to handleError.

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