Skip to content
Source
Sign inSign up
TanStack/router

head() title from a child route wins when a parent's component throws during render (errorComponent / notFoundComponent) #8791

Openloganrosen(GitHub profile, opens in a new tab) opened this issue 2 comments

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

  1. pnpm install && pnpm dev
  2. Open /render/child. The parent errorComponent renders, and the title is "Render child".
  3. Open /missing/child. The parent notFoundComponent renders, and the title is "Missing child".
  4. 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/notFoundComponent can 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.

  1. I don't think head should have access to an error from in-render sources. A slight variant of your example would be this:

      head: ({ match }) => ({
        meta: [{ title: match.status === 'error' ? 'Error' : 'Parent' }],
      }),
      errorComponent: () => <p>error</p>,
      component: () => {
        const [flag, setFlag] = useState(false)
        if (flag) throw new Error('render failed')
        return <button onClick={() => setFlag(true)}>crash</button>
      },

    and then we wouldn't expect head to re-run when the button is clicked.

  2. The use-case of "components contribute head tags" is still interesting, but more complex i think.

Sign in to join this conversation.