Skip to content

Commit de3693b

Browse files
committed
Improve frontend performance with route-level code splitting
1 parent 360a8e7 commit de3693b

2 files changed

Lines changed: 71 additions & 44 deletions

File tree

App.tsx

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { Suspense, lazy } from "react";
22
import {
33
BrowserRouter,
44
Routes,
@@ -11,59 +11,67 @@ import { NotificationsProvider } from "./contexts/NotificationsContext";
1111
import { ChatProvider } from "./contexts/ChatContext";
1212
import { PaymentProvider } from "./contexts/PaymentContext";
1313

14-
import SplashPage from "./pages/SplashPage";
15-
import LoginPage from "./pages/LoginPage";
16-
import HomePage from "./pages/HomePage";
17-
import PaymentPage from "./pages/PaymentPage";
18-
import HistoryPage from "./pages/HistoryPage";
19-
import NotificationsPage from "./pages/NotificationsPage";
20-
import ProfilePage from "./pages/ProfilePage";
21-
import ChatPage from "./pages/ChatPage";
22-
import DrinksPage from "./pages/DrinksPage";
14+
const SplashPage = lazy(() => import("./pages/SplashPage"));
15+
const LoginPage = lazy(() => import("./pages/LoginPage"));
16+
const HomePage = lazy(() => import("./pages/HomePage"));
17+
const PaymentPage = lazy(() => import("./pages/PaymentPage"));
18+
const HistoryPage = lazy(() => import("./pages/HistoryPage"));
19+
const NotificationsPage = lazy(() => import("./pages/NotificationsPage"));
20+
const ProfilePage = lazy(() => import("./pages/ProfilePage"));
21+
const ChatPage = lazy(() => import("./pages/ChatPage"));
22+
const DrinksPage = lazy(() => import("./pages/DrinksPage"));
2323

24-
import DashboardLayout from "./components/layout/DashboardLayout";
25-
import ProtectedRoute from "./components/auth/ProtectedRoute";
26-
import TargetCursor from "./components/common/TargetCursor";
24+
const DashboardLayout = lazy(() => import("./components/layout/DashboardLayout"));
25+
const ProtectedRoute = lazy(() => import("./components/auth/ProtectedRoute"));
26+
const TargetCursor = lazy(() => import("./components/common/TargetCursor"));
27+
28+
const RouteFallback: React.FC = () => (
29+
<div className="min-h-screen flex items-center justify-center bg-[#1a1a1a] text-white">
30+
Loading...
31+
</div>
32+
);
2733

2834
const App: React.FC = () => {
2935
return (
3036
<AuthProvider>
3137
<NotificationsProvider>
3238
<PaymentProvider>
3339
<ChatProvider>
34-
<TargetCursor
35-
spinDuration={2}
36-
hideDefaultCursor
37-
parallaxOn
38-
hoverDuration={0.2}
39-
/>
40-
<BrowserRouter>
41-
<Routes>
42-
<Route path="/" element={<SplashPage />} />
43-
<Route path="/login" element={<LoginPage />} />
40+
<Suspense fallback={<RouteFallback />}>
41+
<TargetCursor
42+
spinDuration={2}
43+
hideDefaultCursor
44+
parallaxOn
45+
hoverDuration={0.2}
46+
/>
47+
<BrowserRouter>
48+
<Routes>
49+
<Route path="/" element={<SplashPage />} />
50+
<Route path="/login" element={<LoginPage />} />
4451

45-
<Route
46-
path="/dashboard"
47-
element={
48-
<ProtectedRoute>
49-
<DashboardLayout />
50-
</ProtectedRoute>
51-
}
52-
>
53-
<Route index element={<Navigate to="home" replace />} />
54-
<Route path="home" element={<HomePage />} />
55-
<Route path="payment" element={<PaymentPage />} />
56-
<Route path="drinks" element={<DrinksPage />} />
57-
<Route path="history" element={<HistoryPage />} />
58-
<Route path="notifications" element={<NotificationsPage />} />
59-
<Route path="chat" element={<ChatPage />} />
60-
<Route path="profile" element={<ProfilePage />} />
61-
<Route path="profile/:userId" element={<ProfilePage />} />
62-
</Route>
52+
<Route
53+
path="/dashboard"
54+
element={
55+
<ProtectedRoute>
56+
<DashboardLayout />
57+
</ProtectedRoute>
58+
}
59+
>
60+
<Route index element={<Navigate to="home" replace />} />
61+
<Route path="home" element={<HomePage />} />
62+
<Route path="payment" element={<PaymentPage />} />
63+
<Route path="drinks" element={<DrinksPage />} />
64+
<Route path="history" element={<HistoryPage />} />
65+
<Route path="notifications" element={<NotificationsPage />} />
66+
<Route path="chat" element={<ChatPage />} />
67+
<Route path="profile" element={<ProfilePage />} />
68+
<Route path="profile/:userId" element={<ProfilePage />} />
69+
</Route>
6370

64-
<Route path="*" element={<Navigate to="/" />} />
65-
</Routes>
66-
</BrowserRouter>
71+
<Route path="*" element={<Navigate to="/" />} />
72+
</Routes>
73+
</BrowserRouter>
74+
</Suspense>
6775
</ChatProvider>
6876
</PaymentProvider>
6977
</NotificationsProvider>

ISSUE_BUNDLE_SIZE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Issue: App ships as a single ~1MB JS bundle
2+
3+
## Summary
4+
`npm run build` showed the main JavaScript bundle was ~981kB minified, which slows initial page load on mobile networks and lower-end devices.
5+
6+
## Impact
7+
- Slower first contentful paint and interaction readiness.
8+
- Higher data usage for users.
9+
- Poor scalability as more features are added.
10+
11+
## Root cause
12+
`App.tsx` eagerly imported all route pages and shared UI modules, so Vite emitted one large entry chunk.
13+
14+
## Resolution in this PR
15+
- Added route-level lazy loading with `React.lazy` and `Suspense` for all pages and dashboard components.
16+
- Added a lightweight loading fallback while split chunks load.
17+
18+
## Validation
19+
After the fix, `npm run build` emits multiple chunks and the main application entry (`index-*.js`) drops from ~981kB to ~70kB, with code split across route chunks.

0 commit comments

Comments
 (0)