Skip to content

Commit

Permalink
Merge pull request #19 from asyncink/error-to-layout
Browse files Browse the repository at this point in the history
feat: pass error to layout
  • Loading branch information
andrepolischuk authored Feb 29, 2024
2 parents d25e4ef + 601e01b commit bc8b8ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
},
{
"path": "dist/server/index.js",
"limit": "7.07 KB"
"limit": "7.08 KB"
}
]
27 changes: 19 additions & 8 deletions src/components/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const Routes: React.FC<RoutesProps> = ({

const [currentLocation, setCurrentLocation] = useState(location)
const [routeData, setRouteData] = useState(data)
const [error, setError] = useState<Error | null>(null)

const isLoading = location !== currentLocation
const isWaitingMode = transition === TransitionMode.WAIT_FOR_DATA
Expand All @@ -62,15 +63,25 @@ export const Routes: React.FC<RoutesProps> = ({
window.scrollTo(0, 0)
}

const {data, meta = {}} = await loadRouteData({pathname, routes, context})
try {
const {data, meta = {}} = await loadRouteData({
pathname,
routes,
context
})

if (scrollToTop && isBlockedMode) {
window.scrollTo(0, 0)
}
if (scrollToTop && isBlockedMode) {
window.scrollTo(0, 0)
}

setRouteData((previousData) => ({...previousData, ...data}))
setCurrentLocation(location)
onChangeMetaData?.(meta)
setRouteData((previousData) => ({...previousData, ...data}))
setCurrentLocation(location)
onChangeMetaData?.(meta)
} catch (error) {
if (error instanceof Error) {
setError(error)
}
}
}

if (isLoading) {
Expand All @@ -84,7 +95,7 @@ export const Routes: React.FC<RoutesProps> = ({

return (
<Suspense fallback={Fallback ? <Fallback /> : undefined}>
<Layout {...rest}>
<Layout {...rest} error={error}>
<BaseRoutes location={routerLocation}>
{routeData?.redirect && <Navigate to={routeData.redirect} />}
{routes.map(({path, Component, Fallback, ...routeProps}) => (
Expand Down

0 comments on commit bc8b8ab

Please sign in to comment.