Skip to content

beforeLoad of child route is called even when parent’s beforeLoad throws an error #4151

@gynekolog

Description

@gynekolog

Which project does this relate to?

Router

Describe the bug

When using beforeLoad in nested routes, the expected behavior is that if a parent route’s beforeLoad function throws an error, the child routes’ beforeLoad functions should not be executed.

However, in the current behavior, even when the parent route’s beforeLoad throws an error, the beforeLoad of the child route is still invoked.

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-cu9mdexv

Steps to Reproduce the Bug or Issue

  1. Visit https://stackblitz.com/edit/tanstack-router-cu9mdexv
  2. Open console
  3. Navigate to the Posts route.
  4. Navigate to any Post.
  5. The post route throw an error, it's expected. But in the console you can see the console.warn from the post.index route.

Expected behavior

The child route’s beforeLoad should not be called if the parent’s beforeLoad throws.

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • @tanstack/react-router 1.120.3
  • react 19.0.0

Additional context

The reproduction is forked https://tanstack.com/router/latest/docs/framework/react/examples/basic-file-based.
Changes:

  • add posts.$postId.index.tsx
  • add beforeLoad to the posts.$postId.tsx and posts.$postId.index.tsx
  • remove defaultPreload: 'intent' from the main.tsx

Possibly a related discussion
Possibly a related issue

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