1+ import type { ComponentType } from "react" ;
12import { lazy , Suspense } from "react" ;
23import { createBrowserRouter } from "react-router-dom" ;
34import App from "@/App" ;
4- import Skeleton from "@/components/Skeleton " ;
5+ import Spinner from "@/components/Spinner " ;
56import MainLayout from "@/layouts/MainLayout" ;
67import RootLayout from "@/layouts/RootLayout" ;
78import Home from "@/pages/Home" ;
@@ -27,6 +28,19 @@ import { ROUTES } from "./routes";
2728export const Routes = ROUTES ;
2829export { ROUTES } ;
2930
31+ // Helper component to reduce Suspense boilerplate for lazy routes
32+ const LazyRoute = ( { component : Component } : { component : ComponentType } ) => (
33+ < Suspense
34+ fallback = {
35+ < div className = "w-full h-64 flex items-center justify-center" >
36+ < Spinner size = "lg" />
37+ </ div >
38+ }
39+ >
40+ < Component />
41+ </ Suspense >
42+ ) ;
43+
3044const router = createBrowserRouter ( [
3145 {
3246 path : "/" ,
@@ -35,38 +49,10 @@ const router = createBrowserRouter([
3549 {
3650 path : Routes . AUTH ,
3751 children : [
38- {
39- path : "" ,
40- element : (
41- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
42- < SignIn />
43- </ Suspense >
44- ) ,
45- } ,
46- {
47- path : "admin" ,
48- element : (
49- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
50- < AdminSignIn />
51- </ Suspense >
52- ) ,
53- } ,
54- {
55- path : "signup" ,
56- element : (
57- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
58- < SignUp />
59- </ Suspense >
60- ) ,
61- } ,
62- {
63- path : "callback" ,
64- element : (
65- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
66- < AuthCallback />
67- </ Suspense >
68- ) ,
69- } ,
52+ { path : "" , element : < LazyRoute component = { SignIn } /> } ,
53+ { path : "admin" , element : < LazyRoute component = { AdminSignIn } /> } ,
54+ { path : "signup" , element : < LazyRoute component = { SignUp } /> } ,
55+ { path : "callback" , element : < LazyRoute component = { AuthCallback } /> } ,
7056 ] ,
7157 } ,
7258 {
@@ -76,101 +62,21 @@ const router = createBrowserRouter([
7662 {
7763 element : < MainLayout /> ,
7864 children : [
79- {
80- path : "" ,
81- element : < Home /> ,
82- } ,
83- {
84- path : Routes . EXPLORE ,
85- element : (
86- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
87- < Explore />
88- </ Suspense >
89- ) ,
90- } ,
91- {
92- path : Routes . ARCHIVED ,
93- element : (
94- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
95- < Archived />
96- </ Suspense >
97- ) ,
98- } ,
99- {
100- path : "u/:username" ,
101- element : (
102- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
103- < UserProfile />
104- </ Suspense >
105- ) ,
106- } ,
65+ { path : "" , element : < Home /> } ,
66+ { path : Routes . EXPLORE , element : < LazyRoute component = { Explore } /> } ,
67+ { path : Routes . ARCHIVED , element : < LazyRoute component = { Archived } /> } ,
68+ { path : "u/:username" , element : < LazyRoute component = { UserProfile } /> } ,
10769 ] ,
10870 } ,
109- {
110- path : Routes . ATTACHMENTS ,
111- element : (
112- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
113- < Attachments />
114- </ Suspense >
115- ) ,
116- } ,
117- {
118- path : Routes . INBOX ,
119- element : (
120- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
121- < Inboxes />
122- </ Suspense >
123- ) ,
124- } ,
125- {
126- path : Routes . SETTING ,
127- element : (
128- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
129- < Setting />
130- </ Suspense >
131- ) ,
132- } ,
133- {
134- path : "memos/:uid" ,
135- element : (
136- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
137- < MemoDetail />
138- </ Suspense >
139- ) ,
140- } ,
141- // Redirect old path to new path.
142- {
143- path : "m/:uid" ,
144- element : (
145- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
146- < MemoDetailRedirect />
147- </ Suspense >
148- ) ,
149- } ,
150- {
151- path : "403" ,
152- element : (
153- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
154- < PermissionDenied />
155- </ Suspense >
156- ) ,
157- } ,
158- {
159- path : "404" ,
160- element : (
161- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
162- < NotFound />
163- </ Suspense >
164- ) ,
165- } ,
166- {
167- path : "*" ,
168- element : (
169- < Suspense fallback = { < Skeleton type = "route" showEditor = { false } /> } >
170- < NotFound />
171- </ Suspense >
172- ) ,
173- } ,
71+ { path : Routes . ATTACHMENTS , element : < LazyRoute component = { Attachments } /> } ,
72+ { path : Routes . INBOX , element : < LazyRoute component = { Inboxes } /> } ,
73+ { path : Routes . SETTING , element : < LazyRoute component = { Setting } /> } ,
74+ { path : "memos/:uid" , element : < LazyRoute component = { MemoDetail } /> } ,
75+ // Redirect old path to new path
76+ { path : "m/:uid" , element : < LazyRoute component = { MemoDetailRedirect } /> } ,
77+ { path : "403" , element : < LazyRoute component = { PermissionDenied } /> } ,
78+ { path : "404" , element : < LazyRoute component = { NotFound } /> } ,
79+ { path : "*" , element : < LazyRoute component = { NotFound } /> } ,
17480 ] ,
17581 } ,
17682 ] ,
0 commit comments