fix: loading only outlet instead of whole page for child subroutes

This commit is contained in:
Sajad Mirjalili
2025-09-29 11:47:11 +03:30
parent ac9a1a3a10
commit fcf56dffc7
3 changed files with 9 additions and 13 deletions

View File

@@ -4,9 +4,10 @@ import { appRoutes } from '@/routes/config';
import { Outlet, useLocation } from 'react-router-dom'; import { Outlet, useLocation } from 'react-router-dom';
import { Box, useMediaQuery, useTheme } from '@mui/material'; import { Box, useMediaQuery, useTheme } from '@mui/material';
import { Header } from './Header'; import { Header } from './Header';
import { useState } from 'react'; import { Suspense, useState } from 'react';
import { Toolbar } from './Toolbar'; import { Toolbar } from './Toolbar';
import { useAuth } from '@/hooks/useAuth'; import { useAuth } from '@/hooks/useAuth';
import { Loading } from '../routes/Loading';
export const Layout = () => { export const Layout = () => {
const navItemConfigs = buildNavItems(appRoutes); const navItemConfigs = buildNavItems(appRoutes);
@@ -53,7 +54,9 @@ export const Layout = () => {
overflowInline: 'auto', overflowInline: 'auto',
}} }}
> >
<Outlet /> <Suspense fallback={<Loading />}>
<Outlet />
</Suspense>
</Box> </Box>
</Box> </Box>

View File

@@ -1,8 +1,7 @@
import { Suspense, type ReactNode } from 'react'; import { type ReactNode } from 'react';
import { createBrowserRouter, type RouteObject } from 'react-router-dom'; import { createBrowserRouter, type RouteObject } from 'react-router-dom';
import { appRoutes, type RouteConfig } from './config'; import { appRoutes, type RouteConfig } from './config';
import { ProtectedRoute } from '@/components/routes/ProtectedRoute'; import { ProtectedRoute } from '@/components/routes/ProtectedRoute';
import { Loading } from '@/components/routes/Loading';
/** /**
* A recursive function to map our custom route config to the format * A recursive function to map our custom route config to the format
@@ -10,15 +9,9 @@ import { Loading } from '@/components/routes/Loading';
*/ */
function mapRoutes(routes: RouteConfig[]): RouteObject[] { function mapRoutes(routes: RouteConfig[]): RouteObject[] {
return routes.map((route) => { return routes.map((route) => {
// Start with the base element, wrapped in Suspense for lazy loading // Remove the suspense from here and move to layout outlet
let element: ReactNode = ( // Avoid loading layout for rendering different children
<Suspense fallback={<Loading />}>{route.element}</Suspense> let element: ReactNode = route.element;
);
// Conditionally wrap the element in the specified layout
// if (route.layout) {
// element = <route.layout>{element}</route.layout>;
// }
if (route.authorize) { if (route.authorize) {
element = <ProtectedRoute>{element}</ProtectedRoute>; element = <ProtectedRoute>{element}</ProtectedRoute>;