Skip to content
Source
Sign inSign up
TanStack/router

fix(router-plugin): only patch the route a module exported during route HMR #8686

OpenVugarAhmadov(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/8686/headopened

🎯 Changes

Fixes #7921.

Route HMR finds the route to patch by id on window.__TSR_ROUTER__. With a second app on the page (e.g. a Module Federation remote), the remote's __root__ or / route matches the host's, so loading the remote patches the host's route. When the remote's tree is attached to the host router, it fails with Duplicate routes found with id: /.

Each route module now remembers the first Route it exported, and only patches the router's route when it is that object:

// vite: hot.data persists across evaluations
hotData['tsr-route'] ??= Route
// webpack/rspack: carried to the next evaluation through dispose
const ownRoute = hotData['tsr-route'] ?? Route
if (routeId && existingRoute === ownRoute && existingRoute !== Route) { /* patch */ }

A module's own hot updates are unchanged, including the aliased-import case from #7560. Production output doesn't change: the HMR code is dev-only, and none of the @false snapshots changed.

Tests

  • New tests/route-hmr-statement.test.ts runs the emitted statement in both vite and webpack styles against a real RouterCore:
    • another module's route with the same id isn't patched, either when it loads or when it's hot updated (both fail on main);
    • a module's own hot update still patches its route.
  • @tanstack/router-plugin: test:unit (906 passed), test:types, test:eslint.
  • tanstack-react-start-e2e-hmr: test:e2e--vite-ssr, test:e2e--vite-ssr--bundled-dev and test:e2e--rsbuild-ssr, 28/28 each.
  • Also checked in an Rsbuild Module Federation app (host + 2 remotes, remote route HMR on): the remote attaches without touching the host's routes, and the remote's route HMR still applies when it runs on its own. Repro: https://github.com/VugarAhmadov/module-federation-tanstack-react-router (set addHmr: true in remote-*/rsbuild.config.ts).

This PR was written with AI assistance (Claude Code). I reviewed and tested it.

✅ 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).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Route hot updates now apply only to the route owned by the updating module, preventing changes to another module’s route when IDs match.
    • Updates to a module’s own route continue to preserve the existing route instance.
    1. 🦋 Changeset detected

      Latest commit: ea6d2d1ca691bdfdd4a6180d3781dcf865d2556c

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

      This PR includes changesets to release 7 packages
      NameType
      @tanstack/router-pluginPatch
      @tanstack/router-vite-pluginPatch
      @tanstack/start-plugin-corePatch
      @tanstack/react-start-rscPatch
      @tanstack/react-startPatch
      @tanstack/solid-startPatch
      @tanstack/vue-startPatch

      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

    2. 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: b472df7a-6488-4391-a113-e40d202fdf7a

      📥 Commits

      Reviewing files that changed from the base of the PR and between 57bd1a92f9839af5bf773c3b36d039569fb1a41e and ea6d2d1ca691bdfdd4a6180d3781dcf865d2556c.


      📒 Files selected for processing (16)
      • .changeset/route-hmr-own-route-only.md
      • packages/router-plugin/src/core/hmr/vite-adapter.ts
      • packages/router-plugin/src/core/hmr/webpack-adapter.ts
      • packages/router-plugin/tests/add-hmr/snapshots/react/arrow-function@true.tsx
      • packages/router-plugin/tests/add-hmr/snapshots/react/arrow-function@webpack-hot.tsx
      • packages/router-plugin/tests/add-hmr/snapshots/react/createFileRoute-lowercase-components@true.tsx
      • packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute-inline-component@true.tsx
      • packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute-lowercase-components@true.tsx
      • packages/router-plugin/tests/add-hmr/snapshots/react/createRootRoute@true.tsx
      • packages/router-plugin/tests/add-hmr/snapshots/react/createRootRouteWithContext-type-args@true.tsx
      • packages/router-plugin/tests/add-hmr/snapshots/react/explicit-undefined-component@true.tsx
      • packages/router-plugin/tests/add-hmr/snapshots/react/function-declaration@true.tsx
      • packages/router-plugin/tests/add-hmr/snapshots/react/multi-component@true.tsx
      • packages/router-plugin/tests/add-hmr/snapshots/react/string-literal-keys@true.tsx
      • packages/router-plugin/tests/add-hmr/snapshots/solid/arrow-function@true.tsx
      • packages/router-plugin/tests/route-hmr-statement.test.ts

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



      📝 Walkthrough
      📝 Walkthrough

      Walkthrough

      Route HMR now retains each module’s route object and checks route identity before applying initial or accepted updates. Vite and webpack adapters, generated snapshots, and tests reflect this ownership check.

      Changes

      Route HMR ownership

      Layer / File(s)Summary
      Retain route identity and guard HMR updates
      .changeset/route-hmr-own-route-only.md, packages/router-plugin/src/core/hmr/*-adapter.ts, packages/router-plugin/tests/add-hmr/snapshots/{react,solid}/*
      Vite and webpack HMR retain the module’s route object and update only when the router maps the route ID to that object. Generated snapshots apply the same check to initial and accepted updates.
      Test route ownership on load and update
      packages/router-plugin/tests/route-hmr-statement.test.ts
      Tests simulate Vite and webpack HMR. Same-ID routes from another module leave the host route and loader unchanged; an update from the owning module preserves its route object and replaces its loader.

      Priority: ➖ Normal

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Suggested reviewers: schiller-manuel


      Merge Risk: ⚪ Minimal · up to ea6d2

      This change limits dev-only route HMR so a module patches only the route it exported. It prevents a remote app's route with a matching ID from altering the host's route. Production output is unchanged. No merge-blocking risk was found.

      Pre-merge checks | Passed 6 | Failed 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring CoverageWarningDocstring coverage is 18.75% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 15 files. (1 skipped:…Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (6 passed)
      Check nameStatusExplanation
      Linked Issues checkPassedThe description references issue #7921 and clearly explains how route ownership checks address cross-application route HMR conflicts.
      Out of Scope Changes checkPassedThe changes remain within router-plugin route HMR behavior, related snapshots, tests, and release metadata. No unrelated changes are described.
      Linked Issues checkPassedIssue #7921 requires independent applications with overlapping route IDs to avoid cross-application route mutation during development HMR. The Vite adapter stores the module's first Route in `hotDat…
      Out of Scope Changes checkPassedThe changes remain within issue #7921. They modify generated Vite and webpack/Rspack route-HMR statements, update the corresponding generated snapshots, add focused regression tests, and add the requi…
      Title checkPassedThe title clearly and concisely describes the main change: route HMR patches only the route exported by the module.
      Description checkPassedThe description includes the required Changes, Checklist, and Release Impact sections. It explains the motivation, implementation, testing, AI assistance, and changeset impact, with all required check…

      Full details: Docstring Coverage

      Explanation

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


      • 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.
    3. Heads-up: I missed #8277 before opening this. It also fixes #7921, and more completely (it tracks the owning router per route instead of using window.TSR_ROUTER).

      This PR is only the minimal #7921 part (2 files, no router-core change), in case splitting it out helps it land sooner, since #8277 also carries the RSC and chunk-recovery work. Happy to close this in favor of #8277, your call.

Sign in to join this conversation.