Skip to content

fix(router-core): keep server loads pending until settled - #8740

Open
BleedingDev wants to merge 8 commits into
TanStack:mainfrom
BleedingDev:fix/server-load-state-20261017
Open

BleedingDev wants to merge 8 commits into
TanStack:mainfrom
BleedingDev:fix/server-load-state-20261017

Conversation

@BleedingDev

@BleedingDev BleedingDev commented Oct 17, 2026 •

Copy link
Copy Markdown

🎯 Changes

Why the change

Wait for every active server load before reporting idle or capturing hydration state so SSR returns the final route data or redirect.

Special things to note

  • A thrown active load fails its SSR request even if application code catches that load's promise; request cleanup prevents the failure leaking into later requests.
  • Early hints still precede custom dehydration hooks; a hook-started load can add final hints, and a later redirect cannot retract hints already sent.
  • AI-assisted and reviewed: exact cd9c19070b passed all 9 focused dehydration regressions, TypeScript 5.6–7.0 and ESLint; hosted CI and the final performance comparison remain pending.

Change outline

 serverLoad
+  activeLoads += 1
   status = pending
   try
     load matches and run lifecycle callbacks
   finally
-    status = idle
+    activeLoads -= 1
+    publish idle only when activeLoads == 0

 SSR
+  drain active loads and retain request failures
   run custom dehydration hook
+  drain loads started by the hook
   capture final matches
+  return a completed redirect before rendering HTML

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with the relevant test commands, or tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Server-rendered pages now wait for active route loads to settle before capturing hydration state, preventing stale or incomplete data from appearing during hydration.
    • Concurrent loads keep the router in a pending state until all settle, and load failures are surfaced even when a loader’s promise is caught.
    • Redirects completed during loading or dehydration are honored before rendering the response.
    • Early hints emitted before or during dehydration are preserved when a later redirect occurs.

@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: TanStack/router/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4c3290ef-61f9-4540-9a5a-fcf77bf97208
📥 Commits

Reviewing files that changed from the base of the PR and between b1b1117 and cd9c190.

📒 Files selected for processing: 14
  • .changeset/server-load-status.md
  • packages/react-router/tests/server-load-state-hydration.test.tsx
  • packages/router-core/package.json
  • packages/router-core/src/load-server.ts
  • packages/router-core/src/router.ts
  • packages/router-core/src/ssr/createRequestHandler.ts
  • packages/router-core/src/ssr/ssr-server.ts
  • packages/router-core/tests/server-load-dehydration.test.ts
  • packages/router-core/tests/server-load-state.bench.ts
  • packages/router-core/tests/server-route-lifecycle.test.ts
  • packages/router-core/vitest.server-load.bench.config.ts
  • packages/start-server-core/src/createStartHandler.ts
  • packages/start-server-core/src/request-handler.ts
  • packages/start-server-core/tests/createStartHandler.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

📝 Walkthrough

Server loads now keep router state pending until all active loads settle. SSR waits for those loads before and after custom dehydration, handles load failures and redirects, and coordinates dynamic early hints with pending loads.

Changes: Server Load and SSR

Layer / File(s) Summary
Track active server loads
packages/router-core/src/router.ts, packages/router-core/src/load-server.ts, packages/router-core/tests/server-route-lifecycle.test.ts, packages/router-core/tests/server-load-state.bench.ts, packages/router-core/package.json, packages/router-core/vitest.server-load.bench.config.ts
Router state tracks active server loads and remains pending until they settle. Lifecycle tests cover overlapping, failed, and cancelled loads. A benchmark suite measures these workloads.
Wait for loads during SSR
packages/router-core/src/ssr/ssr-server.ts, packages/router-core/src/ssr/createRequestHandler.ts, packages/router-core/tests/server-load-dehydration.test.ts, packages/react-router/tests/server-load-state-hydration.test.tsx, .changeset/server-load-status.md
SSR dehydration waits for active loads before and after custom hooks. It propagates recorded load failures and checks for redirects. Tests cover failure, cleanup, abort, redirect, and hydration behavior.
Handle redirects and dynamic early hints
packages/start-server-core/src/createStartHandler.ts, packages/start-server-core/src/request-handler.ts, packages/start-server-core/tests/createStartHandler.test.ts
The start handler checks for redirects after dehydration and adjusts dynamic hint collection based on pending loads and server-result changes. Tests and documentation cover hint and redirect behavior.

Priority: ➖ Normal

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant createStartHandler
  participant RouterCore
  participant serverSsr.dehydrate
  participant routeLoader
  createStartHandler->>RouterCore: Start server route load
  RouterCore->>RouterCore: Set pending state and count active loads
  RouterCore->>routeLoader: Run route loader
  createStartHandler->>serverSsr.dehydrate: Dehydrate request
  serverSsr.dehydrate->>RouterCore: Wait for active loads
  routeLoader-->>RouterCore: Resolve or reject load
  RouterCore-->>serverSsr.dehydrate: Resolve wait or report failure
  serverSsr.dehydrate-->>createStartHandler: Return dehydrated state or redirect
Loading

Suggested reviewers: schiller-manuel

Merge Risk: ⚪ Minimal · up to cd9c1

Server-rendered requests now wait for every active route load to finish before capturing state or rendering. Redirects that complete during that wait are honored, and load failures surface as errors. No concrete defect was found in the change. Two behaviors are intentional and documented: a failing background load fails the request, and early hints already sent stay in place if a redirect follows. It looks ready to merge once normal CI passes.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 6.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 12 files. (2 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description check Passed The description explains the motivation, implementation, testing status, AI assistance, release impact, and all required checklist items. It also identifies pending hosted CI and performance compariso…
Title check Passed The title clearly and concisely describes the main change: keeping server loads pending until they settle.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description explains the motivation, implementation, testing status, AI assistance, release impact, and all required checklist items. It also identifies pending hosted CI and performance comparison work.

Full details: Docstring Coverage

Explanation

Docstring coverage is 6.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 12 files. (2 skipped: 2 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@changeset-bot

changeset-bot Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cd9c190

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 23 packages
Name Type
@tanstack/router-core Patch
@tanstack/start-server-core Patch
@tanstack/react-router Patch
@tanstack/react-start-client Patch
@tanstack/react-start-rsc Patch
@tanstack/react-start-server Patch
@tanstack/router-generator Patch
@tanstack/router-plugin Patch
@tanstack/solid-router Patch
@tanstack/solid-start-client Patch
@tanstack/solid-start-server Patch
@tanstack/start-client-core Patch
@tanstack/start-plugin-core Patch
@tanstack/start-storage-context Patch
@tanstack/vue-router Patch
@tanstack/vue-start-client Patch
@tanstack/vue-start-server Patch
@tanstack/react-start Patch
@tanstack/solid-start Patch
@tanstack/vue-start Patch
@tanstack/router-cli Patch
@tanstack/router-vite-plugin Patch
@tanstack/start-static-server-functions Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@BleedingDev
BleedingDev marked this pull request as ready for review October 19, 2026 20:23
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T20:31:28.389466Z cd9c190 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cd9c19070b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

: undefined
// A custom hook can start a load. Capture matches only after it settles.
if (router._serverLoads || router._serverLoadFailure) {
await waitForServerLoads(router, serverSsr, opts?.signal)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Abort server lanes omitted from the final snapshot

When an initial server lane returns deferred data and an onEnter or dehydration hook starts a successful reload, this second drain lets the reload replace the stored matches before serialization. The earlier lane still registered an onCleanup callback in load-server.ts, but that callback skips abortLane() when serialization of the final reload reports settled=true; consequently, a promise or resource tied to the earlier lane's public abort signal can continue indefinitely after the response. Track which lanes were actually dehydrated or abort superseded lanes before treating serialization completion as settlement.

AGENTS.md reference: packages/router-core/AGENTS.md:L4-L4

Useful? React with 👍 / 👎.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant