-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(router-core,ssr-client): Catch errors thrown during hydrate #5417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
44f561c
b343aaa
1c27c12
9729f07
2b309c5
5347508
20b60af
1be11a3
e53cf57
08c5f8e
7cb7450
254defe
3abb562
5dd98c4
fec4a1b
8238499
366e4af
b12398d
625c901
d9b1951
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { createFileRoute, notFound } from '@tanstack/react-router' | ||
|
|
||
| export const Route = createFileRoute('/not-found/via-head')({ | ||
| head: () => { | ||
| throw notFound() | ||
| }, | ||
| component: RouteComponent, | ||
| notFoundComponent: () => { | ||
| return ( | ||
| <div data-testid="via-head-notFound-component"> | ||
| Not Found "/not-found/via-head"! | ||
| </div> | ||
| ) | ||
| }, | ||
| }) | ||
|
|
||
| function RouteComponent() { | ||
| return ( | ||
| <div data-testid="via-head-route-component" data-server={typeof window}> | ||
| Hello "/not-found/via-head"! | ||
| </div> | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { createFileRoute, notFound } from '@tanstack/solid-router' | ||
|
|
||
| export const Route = createFileRoute('/not-found/via-head')({ | ||
| head: () => { | ||
| throw notFound() | ||
| }, | ||
| component: RouteComponent, | ||
| notFoundComponent: () => { | ||
| return ( | ||
| <div data-testid="via-head-notFound-component"> | ||
| Not Found "/not-found/via-head"! | ||
| </div> | ||
| ) | ||
| }, | ||
| }) | ||
|
|
||
| function RouteComponent() { | ||
| return ( | ||
| <div data-testid="via-head-route-component" data-server={typeof window}> | ||
| Hello "/not-found/via-head"! | ||
| </div> | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,9 +53,18 @@ const resolvePreload = (inner: InnerLoadContext, matchId: string): boolean => { | |
| const _handleNotFound = (inner: InnerLoadContext, err: NotFoundError) => { | ||
| // Find the route that should handle the not found error | ||
| // First check if a specific route is requested to show the error | ||
| const routeCursor = | ||
| let routeCursor = | ||
| inner.router.routesById[err.routeId ?? ''] ?? inner.router.routeTree | ||
|
|
||
| // For BEFORE_LOAD errors, find a parent route with a notFoundComponent that can handle the error | ||
| if ((err as any).routerCode === 'BEFORE_LOAD' && routeCursor.parentRoute) { | ||
| while (routeCursor.parentRoute && !routeCursor.options.notFoundComponent) { | ||
| routeCursor = routeCursor.parentRoute | ||
| } | ||
| // Update the error to point to the error handling route | ||
| err.routeId = routeCursor.id | ||
| } | ||
|
Comment on lines
56
to
73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major 🧩 Analysis chainVerify the NotFound ancestor routing behavior. The new logic correctly climbs the route tree to find an ancestor with a Run the following script to verify this logic is exercised in tests: 🏁 Script executed: #!/bin/bash
# Description: Find test coverage for BEFORE_LOAD NotFound handling
# Search for tests that exercise NotFound errors during beforeLoad
rg -nP -A5 -B5 'BEFORE_LOAD|beforeLoad.*notFound|notFound.*beforeLoad' --type=ts --type=tsx -g '**/*test*' -g '**/*spec*'
# Search for routes with notFoundComponent to understand the hierarchy
ast-grep --pattern $'notFoundComponent: $_'Length of output: 27 🏁 Script executed: #!/bin/bash
# Retry search for test coverage of BEFORE_LOAD NotFound handling without type filters
# Search for tests mentioning BEFORE_LOAD or notFound in .ts/.js test files
rg -nP -C3 'BEFORE_LOAD|beforeLoad|notFound' -g '**/*.{test,spec}.{ts,js}'
# Search for definitions of notFoundComponent across codebase
rg -nP 'notFoundComponent' -g '**/*.{ts,js}'Length of output: 64948 Add unit tests for BEFORE_LOAD notFound ancestor routing. |
||
|
|
||
| // Ensure a NotFoundComponent exists on the route | ||
| if ( | ||
| !routeCursor.options.notFoundComponent && | ||
|
|
@@ -84,11 +93,6 @@ const _handleNotFound = (inner: InnerLoadContext, err: NotFoundError) => { | |
| error: err, | ||
| isFetching: false, | ||
| })) | ||
|
|
||
| if ((err as any).routerCode === 'BEFORE_LOAD' && routeCursor.parentRoute) { | ||
| err.routeId = routeCursor.parentRoute.id | ||
| _handleNotFound(inner, err) | ||
| } | ||
| } | ||
|
|
||
| const handleRedirectAndNotFound = ( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.