Skip to content
Source
Sign inSign up
TanStack/router

Child routers: render another location with the parent's history #8820

Send to my agent
Openpiecyk opened this issue 0 comments
piecykcommented

We keep pages mounted under sliding panels: any page can open on top of the current one with stackNavigate('/foo/p1'), and the page below keeps its exact state (form input, filters, scroll). The URL is always the top page, the pages below live in history state.

Right now the only way to do this with TanStack Router is a createRouter per layer plus a hand-written RouterHistory that's fixed to one location. It works, but every router adds scroll restoration listeners that are never removed (#8775), every Transitioner canonicalizes and replaces the URL on mount, self.__TSR_ROUTER__ gets overwritten, and the custom history has to implement _getBlockers/notify.

Proposal, a parent option:

const child = createRouter({ ...router.options, parent: router, location: '/foo/p1' })
child.setLocation('/foo/p2')

A child uses the parent's history and blockers, hands its navigations to the parent (relative to still resolves against the child's location), never reads or writes the URL, adds no global listeners and doesn't touch __TSR_ROUTER__. Hooks stay the same, each child has its own RouterProvider. Out of scope for now: sharing committed match data, SSR, transitions.

I made a prototype to see how it looks: compare. It's ~45 lines in router.ts plus a 2-line guard in each Transitioner, with tests and a stack-pages example. In our app it removes our whole custom layer history.

Is parent an OK shape, or would you prefer something like a free createChildRouter(router, { location })? Happy to turn it into a PR once the API is agreed.

Sign in to join this conversation.