Skip to content
Source
Sign inSign up
TanStack/router

Solid Router: root `errorComponent` never catches re-thrown errors when `shellComponent` renders `<Outlet />` directly #6845

Openljho01(GitHub profile, opens in a new tab) opened this issue 6 comments

Which project does this relate to?

Start

Describe the bug

When using shellComponent on the root route, if the shell function renders <Outlet /> directly (instead of {props.children}), the root's errorComponent never catches errors re-thrown from a child route's errorComponent.

This happens because shellComponent receives the CatchBoundary (which wraps errorComponent) as part of its children prop. When the shell ignores children and renders <Outlet /> directly — which is the natural and common pattern — the CatchBoundary is never placed in the render tree. Re-thrown errors from child errorComponent have no parent boundary to propagate to.

The same route structure works correctly in @tanstack/react-router because React uses the component pattern where CatchBoundary wraps the component (which contains <Outlet />), keeping it in the render tree.

Root route (__root.tsx) — Solid:

export const Route = createRootRoute({
  shellComponent: RootShell,
  errorComponent: ({ error }) => (
    <div data-testid="root-error">Root caught: {error.message}</div>
  ),
})

function RootShell() {
  return (
    <html>
      <head>...</head>
      <body>
        <Outlet />   {/* ← renders Outlet directly, bypasses CatchBoundary in children */}
        <Scripts />
      </body>
    </html>
  )
}

Child route (test-error.tsx) — identical for both Solid and React:

export const Route = createFileRoute('/test-error')({
  beforeLoad: () => { throw new Error('CHILD_THREW') },
  component: () => <div>never renders</div>,
  errorComponent: ({ error }) => {
    if (error.message === 'HANDLE_THIS') return <div>handled</div>
    throw error   // ← re-throw to parent
  },
})

Your Example Website or App

https://github.com/ljho01/tanstack-solid-router-error-mre

Steps to Reproduce the Bug or Issue

  1. cd solid && pnpm install && pnpm dev
  2. Open http://localhost:3001/
  3. Click "Go to /test-error (client nav)"
  4. Page goes blank — root errorComponent is never rendered

Compare with React:

  1. cd react && pnpm install && pnpm dev
  2. Open http://localhost:3002/
  3. Click "Go to /test-error (client nav)"
  4. Root errorComponent renders correctly with "Root errorComponent caught: CHILD_THREW"

E2E tests (optional):

# Solid — 2 of 3 FAIL
cd solid && pnpm install && npx playwright install chromium && pnpm test:e2e

# React — 3 of 3 PASS
cd react && pnpm install && npx playwright install chromium && pnpm test:e2e

Expected behavior

The root route's errorComponent should catch errors re-thrown from a child route's errorComponent, regardless of whether the shell renders <Outlet /> directly or uses {props.children}. This is consistent with how @tanstack/react-router behaves.

Screenshots or Videos

No response

Platform

  • Router / Start Version: @tanstack/solid-router 1.166.2, @tanstack/solid-start 1.166.2
  • OS: macOS 15.4
  • Browser: Chrome 135
  • Bundler: Vite 7.3.1

Additional context

Root cause in source (@tanstack/solid-router/dist/esm/Match.js):

In the Outlet component, when routeId() === rootRouteId, the child <Match> is rendered inside a <Suspense> but not inside a CatchBoundary. The root's CatchBoundary only exists as a child of ShellComponent (passed via props.children), so if the shell doesn't render its children, the boundary is absent.

A possible fix is to wrap the child <Match> inside the Outlet with the root's CatchBoundary when shellComponent is in use:

// In Outlet, when routeId() === rootRouteId:
const rootRoute = () => router.routesById[rootRouteId];
const needsRootCatchBoundary = () =>
  routeId() === rootRouteId && !!rootRoute().options.shellComponent;

// Wrap child match with CatchBoundary if shellComponent bypasses children
if (needsRootCatchBoundary() && rootErrorComponent()) {
  return createComponent(CatchBoundary, {
    getResetKey: () => resetKey(),
    errorComponent: rootErrorComponent(),
    onCatch: (error) => { /* ... */ },
    children: childMatch(),
  });
}

The repo includes a working patch in the linked reproduction.

  1. Working patch (source-level diff)

    Here's a minimal patch against packages/solid-router that fixes this. The change is only in the Outlet function inside Match.jsx (~20 lines):

    --- a/dist/source/Match.jsx
    +++ b/dist/source/Match.jsx
    @@ -274,17 +274,33 @@ export const Outlet = () => {
                 return matches[index + 1]?.status;
             },
         });
         const shouldShowNotFound = () => childMatchStatus() !== 'redirected' && parentGlobalNotFound();
    +    const rootRoute = () => router.routesById[rootRouteId];
    +    const needsRootCatchBoundary = () => routeId() === rootRouteId && !!rootRoute().options.shellComponent;
    +    const rootErrorComponent = () => rootRoute().options.errorComponent ?? router.options.defaultErrorComponent;
    +    const rootOnCatch = () => rootRoute().options.onCatch ?? router.options.defaultOnCatch;
    +    const resetKey = useRouterState({ select: (s) => s.loadedAt });
         return (<Solid.Show when={!shouldShowNotFound() && childMatchId()} fallback={...}>
           {(matchIdAccessor) => {
                 const currentMatchId = Solid.createMemo(() => matchIdAccessor());
    +            const childMatch = () => <Match matchId={currentMatchId()}/>;
    +            const wrappedChild = () => {
    +                if (needsRootCatchBoundary() && rootErrorComponent()) {
    +                    return (<CatchBoundary
    +                        getResetKey={() => resetKey()}
    +                        errorComponent={rootErrorComponent() || ErrorComponent}
    +                        onCatch={(error) => {
    +                            if (isNotFound(error)) throw error;
    +                            warning(false, `Error in route match: ${rootRouteId}`);
    +                            rootOnCatch()?.(error);
    +                        }}>
    +                        {childMatch()}
    +                    </CatchBoundary>);
    +                }
    +                return childMatch();
    +            };
                 return (<Solid.Show when={routeId() === rootRouteId} fallback={<Match matchId={currentMatchId()}/>}>
                 <Solid.Suspense fallback={<Dynamic component={router.options.defaultPendingComponent}/>}>
    -              <Match matchId={currentMatchId()}/>
    +              {wrappedChild()}
                 </Solid.Suspense>
               </Solid.Show>);
             }}

    What it does: When the current Outlet belongs to the root route and shellComponent is configured, wrap the child <Match> with a CatchBoundary using the root's errorComponent. This puts the boundary back in the render tree regardless of whether shellComponent renders {props.children} or <Outlet /> directly.

    Verified with E2E tests — Solid goes from 1/3 → 3/3 passing (matching React behavior).

  2. Even simpler fix

    Realized the shellComponent check is unnecessary. We can just unconditionally wrap the root Outlet's child <Match> with CatchBoundary when errorComponent exists. A redundant boundary when shellComponent renders {props.children} is harmless — the inner one (from Match) catches first.

    The entire change is +2 declarations, +3 lines of JSX wrapping:

    --- a/packages/solid-router/src/Match.tsx (Outlet function)
    +++ b/packages/solid-router/src/Match.tsx (Outlet function)
    @@ Outlet function, before return:
         const shouldShowNotFound = () => ...
    +    const rootErrorComponent = () => router.routesById[rootRouteId]?.options.errorComponent ?? router.options.defaultErrorComponent;
    +    const rootResetKey = useRouterState({ select: (s) => s.loadedAt });
    
    @@ root route branch, inside <Suspense>:
    -              <Match matchId={currentMatchId()}/>
    +              {rootErrorComponent()
    +                ? <CatchBoundary getResetKey={() => rootResetKey()} errorComponent={rootErrorComponent()} onCatch={(e) => { if (isNotFound(e)) throw e }}>
    +                    <Match matchId={currentMatchId()}/>
    +                  </CatchBoundary>
    +                : <Match matchId={currentMatchId()}/>}

    No shellComponent check, no helper functions, no extra reactive deps. Verified 3/3 E2E tests pass.

  3. Shell component is not supposed to render Outlet.

  4. ljho01(GitHub profile, opens in a new tab)commented

    Correction & apology

    I confirmed this is not a Solid-only bug — React exhibits the exact same behavior when shellComponent renders <Outlet /> directly instead of {props.children}.

    PatternReactSolid
    component + <Outlet />✅ passN/A
    shellComponent + {props.children}✅ pass✅ pass
    shellComponent + <Outlet /> directly❌ fail❌ fail

    As @schiller-manuel pointed out, shellComponent is not supposed to render <Outlet /> — it should render {props.children}, which includes the internal CatchBoundary and matchContext.Provider. My original comparison between React (component pattern) and Solid (shellComponent + <Outlet />) was an apples-to-oranges comparison. I apologize for the noise.

    That said, this is a significant foot-gun: rendering <Outlet /> in shellComponent appears to work correctly (routes render, navigation works), but silently breaks errorComponent propagation with no warning. Since the TanStack Start Solid template's shellComponent renders the full <html> document, it's a very natural (but wrong) pattern.

    Proposal: dev-mode console warning

    Instead of the CatchBoundary fix I submitted in #6847, I'd like to propose a runtime warning that fires when <Outlet /> is rendered inside shellComponent context. This would apply to both react-router and solid-router.

    Detection approach — add a context flag set inside ShellComponent's children in Match:

    // In Match component (root route rendering):
    <ShellComponent>
      <rootBoundaryContext.Provider value={true}>
        <matchContext.Provider value={matchId}>
          <CatchBoundary> ... </CatchBoundary>
        </matchContext.Provider>
      </rootBoundaryContext.Provider>
    </ShellComponent>
    // In Outlet:
    if (routeId === rootRouteId) {
      const insideRootBoundary = useContext(rootBoundaryContext)
      const hasShellComponent = route.options.shellComponent
      warning(
        !hasShellComponent || insideRootBoundary,
        'Detected <Outlet /> rendered directly in shellComponent. ' +
        'shellComponent should render {props.children} instead of <Outlet /> — ' +
        'rendering <Outlet /> directly bypasses the error boundary and other internal wrappers, ' +
        'which will break errorComponent propagation.'
      )
    }

    This would only warn when the misuse pattern is detected (Outlet rendered for root route with shellComponent, but outside the normal boundary tree). Correct usage ({props.children}) would not trigger the warning.

    I'll close the PR (#6847) if you'd prefer this direction.

  5. the matchContext has been removed, what should I do ?

  6. birkskyum(GitHub profile, opens in a new tab) added labelspackage: solid-router

  7. birkskyum(GitHub profile, opens in a new tab) added labelspackage: solid-start

  8. Is this issue still relevant?

Sign in to join this conversation.