head() title from a child route wins when a parent's component throws during render (errorComponent / notFoundComponent) #8791
Which project does this relate to?#
Router
Describe the bug#
When a route's component throws during render (an Error or notFound()), its errorComponent/notFoundComponent is shown, but the document title still comes from the child route's head().
The thrown value is caught by the CatchBoundary and never reaches match.status. So head() on the failing route can't tell it errored. HeadContent also keeps using the deepest match's title, even though that child isn't rendered.
Since 1.170.19 the same setup works when the error comes from a loader: projectLane stops at the first non-success match. The render-time path has no equivalent, and I couldn't find a supported way to set head tags from an error or not-found boundary.
const parent = createRoute({
getParentRoute: () => rootRoute,
path: '/render',
head: ({ match }) => ({
meta: [{ title: match.status === 'error' ? 'Error' : 'Parent' }],
}),
errorComponent: () => <p>error</p>,
component: () => {
throw new Error('render failed')
},
})
const child = createRoute({
getParentRoute: () => parent,
path: '/child',
head: () => ({ meta: [{ title: 'Child' }] }),
component: () => <p>child</p>,
})
// /render/child shows "error" with document.title === "Child"
Related: #3657 and #4684, which fixed head for error/notFound pages from beforeLoad and loaders.
Your Example Website or App#
https://github.com/loganrosen/tanstack-router-head-render-error
Steps to Reproduce the Bug or Issue#
pnpm install && pnpm dev- Open
/render/child. The parenterrorComponentrenders, and the title is "Render child". - Open
/missing/child. The parentnotFoundComponentrenders, and the title is "Missing child". - For comparison, open
/parent/child, where the loader throws. The title is "Error".
Expected behavior#
The title should reflect the boundary that is actually rendered. That could mean either of these:
- heads of matches below a route whose boundary caught a render error are skipped, and that route's
head()re-runs with the error; errorComponent/notFoundComponentcan contribute head tags.
Screenshots or Videos#
No response
Platform#
- Router / Start Version: @tanstack/react-router 1.170.46 (router-core 1.171.39)
- OS: macOS
- Browser: WebKit (macOS system webview)
- Bundler: vite
- Bundler Version: 8.3.5
Additional context#
Found while adding page titles to matrix-authentication-service (element-hq/matrix-authentication-service#6069). The issue was written with help from an AI coding assistant. I ran and checked the reproducer myself.
The use-case of "components contribute head tags" is still interesting, but more complex i think.
Sign in to join this conversation.
I don't think
headshould have access to an error from in-render sources. A slight variant of your example would be this:and then we wouldn't expect
headto re-run when the button is clicked.