Skip to content

pendingComponent gets the route's own beforeLoad context on a fast navigation, loses it on a slow one #9009

Backlog

Description

@balzdur

Which project does this relate to?

Router

Describe the bug

Inside a route's pendingComponent, Route.useRouteContext() sometimes contains what that route's own synchronous beforeLoad returns, and sometimes does not. The only difference is how long the router takes to reach the route:

  • less than pendingMs after the navigation started: the pending component renders with the full context;
  • more than pendingMs (a slow ancestor beforeLoad, or a busy main thread): the pending component renders without the keys of the route's own beforeLoad, and they stay missing until the navigation commits.

useRouteContext() is typed with those keys present in both cases, so nothing warns about it. In our production app this surfaced as a TypeError: Cannot read properties of undefined thrown by a pending component, only on slow navigations.

Your Example Website or App

https://github.com/balzdur/tanstack-router-pending-context-repro

StackBlitz: https://stackblitz.com/github/balzdur/tanstack-router-pending-context-repro

The whole repro is one file. The relevant part:

const parentRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: 'parent',
  component: Outlet,
  beforeLoad: async () => {
    await sleep(parentBeforeLoadMs) // 0 or 400, set by the button clicked
    return { fromParent: 'parent' }
  },
})

const bRoute = createRoute({
  getParentRoute: () => parentRoute,
  path: 'b',
  beforeLoad: () => ({ fromB: 'b' }), // synchronous
  loader: () => sleep(600),
  pendingComponent: function Pending() {
    // typed as { fromParent: string; fromB: string }
    const context = bRoute.useRouteContext()
    log(`pendingComponent: fromParent=${context.fromParent} fromB=${context.fromB}`)
    return <p>pending…</p>
  },
  component: B,
})

const router = createRouter({ routeTree, defaultPendingMs: 200, defaultPendingMinMs: 0 })

Steps to Reproduce the Bug or Issue

  1. npm install && npm run dev, open /parent/a.
  2. Click "fast navigation", then "back to a".
  3. Click "parent beforeLoad takes 400ms", then "back to a".
  4. Click "main thread blocked 400ms".
  5. Read the log printed on the page.

Expected behavior

One consistent answer, whichever it is:

  • either a route's pendingComponent always sees the result of that route's beforeLoad when it is synchronous,
  • or it never does, and the types say so.

Actual behavior

On @tanstack/react-router 1.170.46 (latest at the time of writing), with defaultPendingMs: 200:

navigation fromParent in the pending component fromB in the pending component
fast "parent" "b"
parent beforeLoad takes 400 ms "parent" undefined
main thread blocked 400 ms after navigate() "parent" undefined

The route's component gets both keys in the three cases.

Platform

  • @tanstack/react-router 1.170.46 / @tanstack/router-core 1.171.39 (also on 1.170.32 / 1.171.27, which we run in production)
  • React 19.2.8, Vite dev server
  • Reproduced in a Chromium browser; seen in production on Edge / Windows

Additional context

Why it happens (our reading of the code)

In contextualize (router-core, load-client), for a match that is not retained:

  1. match.context is first set to { ...parentContext, ...routeContext };
  2. onReady is called (options[7]?.()), which runs offerPending;
  3. only then is beforeLoad invoked, and its result merged into match.context.

When the pending deadline has already passed at step 2, offerPending presents shallow clones of the lane matches right away. The clone of this match therefore carries the context of step 1, without the beforeLoad result, although that beforeLoad is synchronous and runs on the next line. The presented clone is not refreshed afterwards.

On a fast navigation, step 2 only arms the timer, and by the time it fires the beforeLoad result is merged: the context is complete.

This is the two-step match.context assignment described in #8115. #8084 addressed it for retained (already mounted) matches. This report is about the fresh match below the pending boundary, whose own pendingComponent reads its context. #8116 would have closed the window, and was not merged.

What would resolve it, as we see it

Two separate things, which can be treated separately.

1. The timing inconsistency. contextualize already tells a synchronous beforeLoad return value from a promise (typeof value?.then === "function"). Offering the pending match after a synchronous result has been merged would make the three rows of the table agree.

2. The contract, and its types. Whatever is decided for 1, there are cases where a route's own beforeLoad context cannot be there:

  • its pendingComponent, while its asynchronous beforeLoad is still running;
  • its errorComponent, when that beforeLoad threw.

Route.useRouteContext() promises that context in both. What can be guaranteed there seems to be exactly what the route's beforeLoad receives as context: the parents' context plus the route's own context(). That type already exists. A dedicated hook, or props passed to pendingComponent / errorComponent, typed with it, would turn this mistake into a compile error instead of a production crash on slow devices.

We would be glad to know which contract you consider the intended one. Until then we treat the route's own beforeLoad and loader output as unavailable in these components, and derive what they need from the search params.

Related: #8115, #8084, #8116, #4998.

AI disclosure

This report was written with the help of an AI assistant (Claude). The investigation in particular was done by the AI: reading router-core, building the reproduction, and the explanation in "Why it happens". I have not verified that analysis line by line myself. The observable behaviour (the table above) comes from running the linked reproduction in a browser.

Activity

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

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