Skip to content
Source
Sign inSign up
TanStack/router

router.update() that drops a committed or cached route leaves invalidate() and navigate() pending #8571

Closedstealth-rklopper(GitHub profile, opens in a new tab) opened this issue 2 comments

Which project does this relate to?

Router

Describe the bug

router.update({ routeTree }) with a tree that drops a route breaks the router when a match of that route is committed or cached. A match is committed while the reader is on the route. A match is cached after the reader leaves a route that has a loader, for gcTime.

  • The next router.invalidate() never settles.
  • Every later router.navigate() never settles.
  • The page keeps rendering the old match when the dropped route was open.

commitMatches walks the previously committed matches and the cached matches, and reads route.options for each one (@tanstack/router-core@1.171.33, src/load-client.ts:1630-1637). getRoute returns router.routesById[match.routeId] behind a non-null assertion (src/load-client.ts:299-301). For a route the new tree dropped, that value is undefined, and the commit throws:

TypeError: Cannot read properties of undefined (reading 'options')
    at commitMatches
    at commit
    at runClientTransaction
    at loadClientRoute
    at RouterCore.load
    at RouterCore.invalidate

Navigating off the route and calling router.clearCache() before router.update() avoids both cases.

Complete minimal reproducer

https://gist.github.com/stealth-rklopper/f495c4d13e8ff9e46eb63736068e4233

Steps to Reproduce the Bug

  1. git clone https://gist.github.com/f495c4d13e8ff9e46eb63736068e4233.git router-update-repro
  2. cd router-update-repro && npm install && npm run dev
  3. Open the page. It runs three cases, each on its own router that starts on /a with the tree [/a, /b] and swaps in the tree [/b]:
    1. /a is open when the update drops it.
    2. /a has a loader. The router navigates to /b, then the update drops /a.
    3. The same as case 2, with router.clearCache() before the update.
  4. Read the three result lines and the console.

Result in Chromium 153 and Firefox 155:

1. /a is open, then removed: invalidate(): still pending after 3000 ms; navigate({ to: "/b" }): still pending after 3000 ms; page shows "page /a"
2. /a has a loader, the reader leaves it, then it is removed: invalidate(): still pending after 3000 ms; navigate({ to: "/b" }): still pending after 3000 ms; page shows "page /b"
3. as 2, with router.clearCache() before the update: invalidate(): settled; navigate({ to: "/b" }): settled; page shows "page /b"

The console shows the TypeError four times: once per call in cases 1 and 2.

Expected behavior

invalidate() and navigate() settle in every case. With the open route dropped, the router matches the location against the new tree and renders the not-found result. A dropped route's cached match is discarded.

Platform

  • Router Version: @tanstack/react-router 1.170.40 with @tanstack/router-core 1.171.33, and 1.170.34 with 1.171.29
  • OS: Linux (Fedora 44)
  • Browser: Chromium and Firefox, through Playwright
  • Browser Version: Chromium 153.0.8010.12, Firefox 155.0
  • Bundler: Vite
  • Bundler Version: 8.3.1

Additional context

We swap route trees at run time to add and remove routes contributed by plugins, without a reload. Adding routes works, and a route keeps its component state when its id and component are unchanged.

Either change would cover both cases:

  • commitMatches skips a committed or cached match whose route is absent from routesById.
  • update() discards the cached matches of routes the new tree drops, and re-matches the location when a committed match's route is gone.
  1. We swap route trees at run time to add and remove routes

    I'm sorry but this is not currently supported

  2. Thank you for the quick reply, if its not supported then I'll try and devise a different way to build the tree.

Sign in to join this conversation.