Skip to content

Conversation

@schiller-manuel
Copy link
Contributor

@schiller-manuel schiller-manuel commented Dec 13, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved router resilience—all loaders now execute to completion even if individual loaders fail.
    • Enhanced redirect error handling for faster error responses.
    • Made head metadata processing more robust with improved error handling and logging.
    • Fixed notFound error handling in the routing flow.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 13, 2025

Walkthrough

Refactored head metadata execution to defer until after all loaders settle using Promise.allSettled, enabling all loaders to complete regardless of failures before head processing, with improved early handling for redirect and notFound errors.

Changes

Cohort / File(s) Summary
Head execution and loader coordination refactor
packages/router-core/src/load-matches.ts
Deferred head computation from inline loader handling to post-loader phase; replaced Promise.all with Promise.allSettled for resilient loader completion; added serial head execution loop after loaders settle; added early redirect error throwing; added notFound error handling after head executions; enhanced error logging with try/catch guards.

Sequence Diagram

sequenceDiagram
    autonumber
    actor Client
    participant LoadMatches
    participant Loaders
    participant HeadExecution
    participant ErrorHandler

    Client->>LoadMatches: initiate match loading
    LoadMatches->>Loaders: execute all loaders in parallel
    par Loader Execution
        Loaders->>Loaders: loader 1 (success/failure)
        Loaders->>Loaders: loader 2 (success/failure)
        Loaders->>Loaders: loader N (success/failure)
    end
    
    rect rgb(100, 200, 100, 0.1)
        note over LoadMatches: AllSettled Phase
        Loaders-->>LoadMatches: all promises settle (regardless of failures)
    end
    
    rect rgb(200, 100, 100, 0.1)
        note over ErrorHandler: Early Redirect Check
        LoadMatches->>ErrorHandler: check for redirect errors
        alt Redirect found
            ErrorHandler-->>Client: throw immediately (skip heads)
        end
    end
    
    rect rgb(100, 150, 200, 0.1)
        note over HeadExecution: Deferred Head Execution
        LoadMatches->>HeadExecution: iterate matches, executeHead serially
        par Head Processing
            HeadExecution->>HeadExecution: head 1 (try/catch)
            HeadExecution->>HeadExecution: head 2 (try/catch)
            HeadExecution->>HeadExecution: head N (try/catch)
        end
        HeadExecution-->>LoadMatches: apply head metadata to matches
    end
    
    LoadMatches->>ErrorHandler: check for notFound errors
    alt NotFound found
        ErrorHandler-->>Client: throw notFound error
    else All processed
        ErrorHandler-->>Client: return settled matches
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Async coordination logic: Promise.allSettled transition and its implications for loader failure handling require careful verification
  • Error handling paths: Multiple new branches (redirect early throw, notFound deferred throw) and their interactions with head execution need validation
  • Head execution deferral: Verify that post-loader serial head execution and error guards correctly handle all previously inline scenarios

Possibly related PRs

Suggested labels

package: router-core

Poem

🐰 Once loaders finish their merry chase,
AllSettled gathers each trace,
Then heads compute with patient grace,
Redirects exit the embrace! 🎯

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: serial head execution' directly and specifically describes the main change: switching from parallel to serial head execution after loader settlement, which is the core modification throughout the changeset.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch serial-head-execution

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link

nx-cloud bot commented Dec 13, 2025

🤖 Nx Cloud AI Fix Eligible

An automatically generated fix could have helped fix failing tasks for this run, but Self-healing CI is disabled for this workspace. Visit workspace settings to enable it and get automatic fixes in future runs.

To disable these notifications, a workspace admin can disable them in workspace settings.


View your CI Pipeline Execution ↗ for commit aca0594

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ❌ Failed 8m 57s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1m 44s View ↗

☁️ Nx Cloud last updated this comment at 2025-12-13 13:32:18 UTC

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/router-core/src/load-matches.ts (2)

917-933: Consider limiting head execution to successfully loaded matches.

The loop iterates over all inner.matches, but loaders only execute for matches up to inner.firstBadMatchIndex (line 896-898). Matches beyond this index may have incomplete state (no loader data), which could cause unexpected behavior in head functions that depend on loaderData.

Consider aligning the head execution range with the loader execution range:

-    for (const match of inner.matches) {
+    const headMax = inner.firstBadMatchIndex ?? inner.matches.length
+    for (let i = 0; i < headMax; i++) {
+      const match = inner.matches[i]!
       const { id: matchId, routeId } = match

Alternatively, if executing heads for all matches is intentional (even failed ones), please add a comment clarifying this design choice.


916-916: Address TODO: decide on head execution failure behavior.

The current implementation continues executing other head functions when one fails, which is resilient but may mask issues in development. Consider documenting the chosen behavior or removing the TODO if the current approach is final.

Would you like me to open an issue to track this decision, or is the current continue-on-failure behavior the intended final design?

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 85bf22e and aca0594.

📒 Files selected for processing (1)
  • packages/router-core/src/load-matches.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use TypeScript strict mode with extensive type safety for all code

Files:

  • packages/router-core/src/load-matches.ts
**/*.{js,ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Implement ESLint rules for router best practices using the ESLint plugin router

Files:

  • packages/router-core/src/load-matches.ts
🧬 Code graph analysis (1)
packages/router-core/src/load-matches.ts (1)
packages/router-core/src/index.ts (2)
  • isRedirect (368-368)
  • isNotFound (374-374)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Test
  • GitHub Check: Preview
🔇 Additional comments (3)
packages/router-core/src/load-matches.ts (3)

900-905: LGTM - Promise.allSettled usage is appropriate here.

Using Promise.allSettled ensures all loaders complete before processing results, which aligns with the goal of deferring head execution until all loaders settle. The failure extraction logic is correct.


907-912: Redirect early-exit logic is correct.

Throwing on the first redirect before head execution is the right approach - there's no need to compute head metadata when redirecting.


935-940: NotFound handling after head execution is appropriate.

The asymmetry with redirect handling (redirect throws before head, notFound throws after head) is intentional and makes sense - a notFound page can still benefit from head metadata, while a redirect doesn't need any rendering.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 13, 2025

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@6093

@tanstack/directive-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@6093

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@6093

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@6093

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@6093

@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@6093

@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@6093

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@6093

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@6093

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@6093

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@6093

@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@6093

@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@6093

@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@6093

@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@6093

@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@6093

@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@6093

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@6093

@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@6093

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@6093

@tanstack/server-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@6093

@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@6093

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@6093

@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@6093

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@6093

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@6093

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@6093

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@6093

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@6093

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@6093

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@6093

@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@6093

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@6093

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@6093

@tanstack/vue-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router@6093

@tanstack/vue-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-devtools@6093

@tanstack/vue-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-ssr-query@6093

@tanstack/vue-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start@6093

@tanstack/vue-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-client@6093

@tanstack/vue-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-server@6093

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@6093

commit: aca0594

Comment on lines +935 to +940
// After head execution, check for notFound errors and throw the first one
for (const err of failures) {
if (isNotFound(err)) {
throw err
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it a little curious that we call executeHead for children of a match that would have thrown a notFound

If we have /a/b/c and /b throws a notFound it its loader, should we really still execute the head of /c? It feels "leaky"

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants