11import { QueryClient , useQueryClient } from '@tanstack/react-query' ;
22import { useMemo } from 'react' ;
3- import { RouterProvider , createBrowserRouter } from 'react-router-dom' ;
3+ import { createBrowserRouter } from 'react-router' ;
4+ import { RouterProvider } from 'react-router/dom' ;
45
56import { paths } from '@/config/paths' ;
67import { ProtectedRoute } from '@/lib/auth' ;
78
8- import { AppRoot , AppRootErrorBoundary } from './routes/app/root' ;
9+ import {
10+ default as AppRoot ,
11+ ErrorBoundary as AppRootErrorBoundary ,
12+ } from './routes/app/root' ;
13+
14+ const convert = ( queryClient : QueryClient ) => ( m : any ) => {
15+ const { clientLoader, clientAction, default : Component , ...rest } = m ;
16+ return {
17+ ...rest ,
18+ loader : clientLoader ?.( queryClient ) ,
19+ action : clientAction ?.( queryClient ) ,
20+ Component,
21+ } ;
22+ } ;
923
1024export const createAppRouter = ( queryClient : QueryClient ) =>
1125 createBrowserRouter ( [
1226 {
1327 path : paths . home . path ,
14- lazy : async ( ) => {
15- const { LandingRoute } = await import ( './routes/landing' ) ;
16- return { Component : LandingRoute } ;
17- } ,
28+ lazy : ( ) => import ( './routes/landing' ) . then ( convert ( queryClient ) ) ,
1829 } ,
1930 {
2031 path : paths . auth . register . path ,
21- lazy : async ( ) => {
22- const { RegisterRoute } = await import ( './routes/auth/register' ) ;
23- return { Component : RegisterRoute } ;
24- } ,
32+ lazy : ( ) => import ( './routes/auth/register' ) . then ( convert ( queryClient ) ) ,
2533 } ,
2634 {
2735 path : paths . auth . login . path ,
28- lazy : async ( ) => {
29- const { LoginRoute } = await import ( './routes/auth/login' ) ;
30- return { Component : LoginRoute } ;
31- } ,
36+ lazy : ( ) => import ( './routes/auth/login' ) . then ( convert ( queryClient ) ) ,
3237 } ,
3338 {
3439 path : paths . app . root . path ,
@@ -41,74 +46,36 @@ export const createAppRouter = (queryClient: QueryClient) =>
4146 children : [
4247 {
4348 path : paths . app . discussions . path ,
44- lazy : async ( ) => {
45- const { DiscussionsRoute, discussionsLoader } = await import (
46- './routes/app/discussions/discussions'
47- ) ;
48- return {
49- Component : DiscussionsRoute ,
50- loader : discussionsLoader ( queryClient ) ,
51- } ;
52- } ,
53- ErrorBoundary : AppRootErrorBoundary ,
49+ lazy : ( ) =>
50+ import ( './routes/app/discussions/discussions' ) . then (
51+ convert ( queryClient ) ,
52+ ) ,
5453 } ,
5554 {
5655 path : paths . app . discussion . path ,
57- lazy : async ( ) => {
58- const { DiscussionRoute, discussionLoader } = await import (
59- './routes/app/discussions/discussion'
60- ) ;
61- return {
62- Component : DiscussionRoute ,
63- loader : discussionLoader ( queryClient ) ,
64- } ;
65- } ,
66- ErrorBoundary : AppRootErrorBoundary ,
56+ lazy : ( ) =>
57+ import ( './routes/app/discussions/discussion' ) . then (
58+ convert ( queryClient ) ,
59+ ) ,
6760 } ,
6861 {
6962 path : paths . app . users . path ,
70- lazy : async ( ) => {
71- const { UsersRoute, usersLoader } = await import (
72- './routes/app/users'
73- ) ;
74- return {
75- Component : UsersRoute ,
76- loader : usersLoader ( queryClient ) ,
77- } ;
78- } ,
79- ErrorBoundary : AppRootErrorBoundary ,
63+ lazy : ( ) => import ( './routes/app/users' ) . then ( convert ( queryClient ) ) ,
8064 } ,
8165 {
8266 path : paths . app . profile . path ,
83- lazy : async ( ) => {
84- const { ProfileRoute } = await import ( './routes/app/profile' ) ;
85- return {
86- Component : ProfileRoute ,
87- } ;
88- } ,
89- ErrorBoundary : AppRootErrorBoundary ,
67+ lazy : ( ) => import ( './routes/app/profile' ) . then ( convert ( queryClient ) ) ,
9068 } ,
9169 {
9270 path : paths . app . dashboard . path ,
93- lazy : async ( ) => {
94- const { DashboardRoute } = await import ( './routes/app/dashboard' ) ;
95- return {
96- Component : DashboardRoute ,
97- } ;
98- } ,
99- ErrorBoundary : AppRootErrorBoundary ,
71+ lazy : ( ) =>
72+ import ( './routes/app/dashboard' ) . then ( convert ( queryClient ) ) ,
10073 } ,
10174 ] ,
10275 } ,
10376 {
10477 path : '*' ,
105- lazy : async ( ) => {
106- const { NotFoundRoute } = await import ( './routes/not-found' ) ;
107- return {
108- Component : NotFoundRoute ,
109- } ;
110- } ,
111- ErrorBoundary : AppRootErrorBoundary ,
78+ lazy : ( ) => import ( './routes/not-found' ) . then ( convert ( queryClient ) ) ,
11279 } ,
11380 ] ) ;
11481
0 commit comments