Skip to content

Commit

Permalink
fix: wrap layout into suspense to prevent update it
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Dec 19, 2023
1 parent 5563aea commit 997fab1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
13 changes: 6 additions & 7 deletions src/client/stream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ export const hydrateFromStream = async (
<AppContextProvider value={appContext}>
<Document>
<BrowserRouter>
<Layout {...rest}>
<Routes
routes={routes}
scrollToTop={scrollToTop}
transition={transition}
/>
</Layout>
<Routes
routes={routes}
scrollToTop={scrollToTop}
transition={transition}
Layout={Layout}
/>
</BrowserRouter>
</Document>
</AppContextProvider>
Expand Down
58 changes: 33 additions & 25 deletions src/components/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import {
} from 'react-router-dom'
import {type PageRoute, TransitionMode} from '../common/types'
import {useAppContext} from './context'
import {loadRouteData} from './loader'
import {loadRouteData, matchRoute} from './loader'

/** Routing props */
export interface RoutesProps {
routes: PageRoute[]
scrollToTop?: boolean
transition?: TransitionMode
Layout: React.FC<any>
}

/** Routing component with initial and meta data */
export const Routes: React.FC<RoutesProps> = ({
routes,
scrollToTop,
transition
transition,
Layout
}) => {
const location = useLocation()
const navigate = useNavigate()
Expand Down Expand Up @@ -79,30 +81,36 @@ export const Routes: React.FC<RoutesProps> = ({
}, [location, currentLocation, scrollToTop]) // eslint-disable-line react-hooks/exhaustive-deps

const routerLocation = isBlockedMode ? currentLocation : location
const match = matchRoute({pathname: routerLocation.pathname, routes})
const Fallback = match?.route.Fallback

return (
<BaseRoutes location={routerLocation}>
{routeData.data?.redirect && <Navigate to={routeData.data.redirect} />}
{routes.map(({path, Component, Fallback, ...routeProps}) => (
<Route
{...routeProps}
key={path}
path={path}
element={
<Suspense fallback={Fallback ? <Fallback /> : undefined}>
{isWaitingMode && routeData.isLoading && Fallback ? (
<Fallback />
) : (
<Component
{...rest}
{...routeData.data}
isLoading={routeData.isLoading}
/>
)}
</Suspense>
}
/>
))}
</BaseRoutes>
<Suspense fallback={Fallback ? <Fallback /> : undefined}>
<Layout {...rest}>
<BaseRoutes location={routerLocation}>
{routeData.data?.redirect && (
<Navigate to={routeData.data.redirect} />
)}
{routes.map(({path, Component, Fallback, ...routeProps}) => (
<Route
{...routeProps}
key={path}
path={path}
element={
isWaitingMode && routeData.isLoading && Fallback ? (
<Fallback />
) : (
<Component
{...rest}
{...routeData.data}
isLoading={routeData.isLoading}
/>
)
}
/>
))}
</BaseRoutes>
</Layout>
</Suspense>
)
}
4 changes: 1 addition & 3 deletions src/server/stream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ export const renderToStream = async (
<AppContextProvider value={appContext}>
<Document>
<StaticRouter location={pathname}>
<Layout {...rest}>
<Routes routes={routes} />
</Layout>
<Routes routes={routes} Layout={Layout} />
</StaticRouter>
</Document>
</AppContextProvider>
Expand Down

0 comments on commit 997fab1

Please sign in to comment.