Skip to content

Commit 9ca96da

Browse files
committed
feat(lazy load): add lazy loading for app routes for better code splitting
1 parent 9b3a2d9 commit 9ca96da

File tree

1 file changed

+79
-31
lines changed

1 file changed

+79
-31
lines changed

apps/web-app/src/app/router/AppRoutes.tsx

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,70 @@
1-
import { Routes, Route } from 'react-router-dom';
1+
import { lazy, Suspense } from 'react';
2+
import { Route, Routes } from 'react-router-dom';
23

3-
import Home from '@/features/home/Home';
4-
import Login from '@/features/auth/pages/Login';
5-
import Register from '@/features/auth/pages/Register';
4+
import ProtectedRoute from './ProtectedRoute';
5+
import AppLayout from '../layout/AppLayout';
6+
const Unauthorized = lazy (() => import('@/pages/Unauthorized'));
67

7-
import PatientDashboard from '@/features/dashboard/PatientDashboard';
8-
import DoctorDashboard from '@/features/dashboard/DoctorDashboard';
9-
import AdminDashboard from '@/features/dashboard/AdminDashboard';
8+
const Home = lazy(() => import('@/features/home/Home'));
9+
const Login = lazy(() => import('@/features/auth/pages/Login'));
10+
const Register = lazy(() => import('@/features/auth/pages/Register'));
11+
const ForgotPassword = lazy(() => import('@/features/auth/pages/ForgotPassword'));
12+
const ResetPassword = lazy(() => import('@/features/auth/pages/ResetPassword'));
1013

11-
import ProtectedRoute from './ProtectedRoute';
12-
import ForgotPassword from '@/features/auth/pages/ForgotPassword';
13-
import ResetPassword from '@/features/auth/pages/ResetPassword';
14-
import Profile from '@/features/profile/page/Profile';
15-
import CreateDoctor from '@/features/admin/pages/CreateDoctor';
16-
import CreateAdmin from '@/features/admin/pages/CreateAdmin';
14+
const PatientDashboard = lazy(() => import('@/features/dashboard/PatientDashboard'));
15+
const DoctorDashboard = lazy(() => import('@/features/dashboard/DoctorDashboard'));
16+
const AdminDashboard = lazy(() => import('@/features/dashboard/AdminDashboard'));
1717

18-
import CreatePatientProfile from '@/features/patient/pages/CreatePatientProfile';
19-
import PatientProfilePage from '@/features/patient/pages/PatientProfile';
20-
import PatientListPage from '@/features/patient/pages/PatientListPage';
21-
import EditPatientProfile from '@/features/patient/pages/EditPatientProfile';
18+
const Profile = lazy(() => import('@/features/profile/page/Profile'));
2219

23-
import BookAppointment from '@/features/patient/pages/BookAppointment';
24-
import MyAppointments from '@/features/patient/pages/MyAppointments';
25-
import DoctorAppointments from '@/features/doctor/pages/DoctorAppointment';
26-
import CreateSlot from '@/features/admin/pages/CreateSlot';
27-
import CreateBranch from '@/features/admin/pages/CreateBranch';
28-
import AppointmentList from '@/features/admin/pages/AppointmentList';
20+
const CreateDoctor = lazy(() => import('@/features/admin/pages/CreateDoctor'));
21+
const CreateAdmin = lazy(() => import('@/features/admin/pages/CreateAdmin'));
22+
const CreateSlot = lazy(() => import('@/features/admin/pages/CreateSlot'));
23+
const CreateBranch = lazy(() => import('@/features/admin/pages/CreateBranch'));
24+
const AppointmentList = lazy(() => import('@/features/admin/pages/AppointmentList'));
25+
const AdminClinicalRecords = lazy(
26+
() => import('@/features/admin/pages/AdminClinicalRecords')
27+
);
2928

30-
import Payment from '@/features/payment/page/Payment';
29+
const CreatePatientProfile = lazy(
30+
() => import('@/features/patient/pages/CreatePatientProfile')
31+
);
32+
const PatientProfilePage = lazy(
33+
() => import('@/features/patient/pages/PatientProfile')
34+
);
35+
const PatientListPage = lazy(() => import('@/features/patient/pages/PatientListPage'));
36+
const DoctorListPage = lazy(() => import('@/features/admin/pages/DoctorListPage'));
37+
const EditPatientProfile = lazy(
38+
() => import('@/features/patient/pages/EditPatientProfile')
39+
);
40+
const BookAppointment = lazy(() => import('@/features/patient/pages/BookAppointment'));
41+
const MyAppointments = lazy(() => import('@/features/patient/pages/MyAppointments'));
42+
const PatientTimeline = lazy(() => import('@/features/patient/pages/PatientTimeline'));
3143

32-
import DoctorConsultations from '@/features/doctor/pages/DoctorConsultations';
44+
const DoctorAppointments = lazy(
45+
() => import('@/features/doctor/pages/DoctorAppointment')
46+
);
47+
const DoctorConsultations = lazy(
48+
() => import('@/features/doctor/pages/DoctorConsultations')
49+
);
50+
const DoctorPatientProfile = lazy(
51+
() => import('@/features/clinical/pages/DoctorPatientProfile')
52+
);
3353

34-
import PatientTimeline from '@/features/patient/pages/PatientTimeline';
35-
import AdminClinicalRecords from '@/features/admin/pages/AdminClinicalRecords';
54+
const Payment = lazy(() => import('@/features/payment/page/Payment'));
55+
const NotFound = lazy(() => import('@/pages/notFound'));
3656

37-
import AppLayout from '../layout/AppLayout';
38-
import DoctorPatientProfile from '@/features/clinical/pages/DoctorPatientProfile';
57+
function RouteLoader() {
58+
return (
59+
<div className="min-h-screen flex items-center justify-center text-sm text-muted-foreground">
60+
Loading...
61+
</div>
62+
);
63+
}
3964

4065
export default function AppRoutes() {
4166
return (
67+
<Suspense fallback={<RouteLoader />}>
4268
<Routes>
4369
<Route path="/" element={<Home />} />
4470
<Route path="/login" element={<Login />} />
@@ -117,7 +143,7 @@ export default function AppRoutes() {
117143
/>
118144

119145
<Route
120-
path="/admin/patient"
146+
path="/admin/patient/patient-search"
121147
element={
122148
<ProtectedRoute allowedRoles={['admin']}>
123149
<AppLayout>
@@ -126,6 +152,18 @@ export default function AppRoutes() {
126152
</ProtectedRoute>
127153
}
128154
/>
155+
156+
<Route
157+
path="/admin/patient/doctor-search"
158+
element={
159+
<ProtectedRoute allowedRoles={['admin']}>
160+
<AppLayout>
161+
<DoctorListPage />
162+
</AppLayout>
163+
</ProtectedRoute>
164+
}
165+
/>
166+
129167
<Route
130168
path="/patient/edit-profile"
131169
element={
@@ -226,8 +264,18 @@ export default function AppRoutes() {
226264

227265
<Route
228266
path="/doctor/patient/:patientId"
229-
element={<DoctorPatientProfile/>}
267+
element={
268+
<ProtectedRoute allowedRoles={['doctor']}>
269+
<DoctorPatientProfile/>
270+
</ProtectedRoute>
271+
}
230272
/>
273+
274+
<Route path="/unauthorized" element={<Unauthorized/>} />
275+
276+
<Route path="*" element={<NotFound/>} />
277+
231278
</Routes>
279+
</Suspense>
232280
);
233281
}

0 commit comments

Comments
 (0)