Skip to content

Tanstack Router: Stale context in child routes #4546

Closed
@sergey-bogdan

Description

@sergey-bogdan

Which project does this relate to?

Router

Describe the bug

Under certain conditions child route loaders receive stale or cached context data. For example given this structure:
_app.tsx -- which is layout pathless route
_app -- folder

  • index.tsx
  • $id.tsx

both index and $id routes use context as deps for their loaders, however they receive stale data when we navigate to either of routes, overall behavior is very consistently reproducible, so either I got something wrong with route hierarchy or it's a bug. This is observed both in router and start probjects

Your Example Website or App

https://stackblitz.com/edit/github-bxxtecbb?file=src%2Froutes%2F_app%2Findex.tsx,src%2Froutes%2F_app%2F%24id.tsx,src%2Froutes%2F_app.tsx

Steps to Reproduce the Bug or Issue

  1. Land on Home (index) route, there are two timestamps visible, one in header accessed via Route.useRouteContext, another in index route that is accessed via Route.useRouterData
  2. Spam on "Invalidate router" button to ensure both timestamps are updated and loader for index route runs fine. This is expected
  3. Go to "Link to /1", land on $id route and click on the same Invalidate router button, timestamp from loaderData is not updated, in console we see that loader runs on every invalidate call, however it receives old data as context
  4. Go to Home, now index route is also broken and gets old ctx data when clicking on Invalidate data

Expected behavior

As a user, I expect to receive fresh and latest ctx as my loader args, imagine having auth checks or some server-side stored user prefs that we use as deps for other loaders calls

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.123.0
  • OS: Windows
  • Browser: Chrome 137, Firefox 139
  • Bundler: Vite
  • Bundler Version: 6.3.5

Additional context

No response

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