Skip to content

Commit cf04659

Browse files
committed
refactor(router): enhance route initialization and user info prefetching with improved caching strategy. close #70
1 parent 3239a1b commit cf04659

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

src/features/router/initRouter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ export async function initAuthRoutes(addRoutes: (parent: string | null, route: R
3939
} else {
4040
// 动态模式
4141
try {
42-
const data = await fetchGetUserRoutes();
42+
const data = await queryClient.ensureQueryData<Api.Route.UserRoute>({
43+
gcTime: Infinity,
44+
queryFn: fetchGetUserRoutes,
45+
queryKey: QUERY_KEYS.ROUTE.USER_ROUTES,
46+
staleTime: Infinity
47+
});
4348

4449
store.dispatch(setHomePath(data.home));
4550

src/features/router/router.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ function initRouter() {
5858

5959
if (getIsLogin(store.getState()) && !isAlreadyPatch) {
6060
initAuthRoutes(reactRouter.patchRoutes);
61-
62-
isAlreadyPatch = true;
6361
}
6462

6563
function resetRoutes() {

src/pages/layout.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ function handleRouteSwitch(to: Router.Route, from: Router.Route | null) {
2323
// eslint-disable-next-line max-params
2424
function createRouteGuard(to: Router.Route, roles: string[], isSuper: boolean, previousRoute: Router.Route | null) {
2525
const loginRoute: RoutePath = '/login';
26+
2627
const isLogin = Boolean(localStg.get('token'));
2728

2829
const notFoundRoute = 'notFound';
30+
2931
const isNotFoundRoute = to.id === notFoundRoute;
3032

3133
if (!isLogin) {
@@ -135,8 +137,10 @@ export async function loader() {
135137

136138
if (hasToken) {
137139
await queryClient.prefetchQuery({
140+
gcTime: Infinity,
138141
queryFn: fetchGetUserInfo,
139-
queryKey: QUERY_KEYS.AUTH.USER_INFO
142+
queryKey: QUERY_KEYS.AUTH.USER_INFO,
143+
staleTime: Infinity
140144
});
141145
}
142146
}

src/service/hooks/useAuth.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export function useLogin() {
1919
});
2020
}
2121

22-
const hasToken = Boolean(localStg.get('token'));
23-
2422
/**
2523
* Get user info hook
2624
*
@@ -30,9 +28,11 @@ const hasToken = Boolean(localStg.get('token'));
3028
* @param enabled - Whether to enable the query (default: true)
3129
*/
3230
export function useUserInfo() {
31+
const hasToken = Boolean(localStg.get('token'));
32+
3333
return useQuery({
3434
enabled: hasToken,
35-
gcTime: 30 * 60 * 1000,
35+
gcTime: Infinity,
3636
placeholderData: () => ({
3737
buttons: [],
3838
roles: [],
@@ -41,9 +41,8 @@ export function useUserInfo() {
4141
}),
4242
queryFn: fetchGetUserInfo,
4343
queryKey: QUERY_KEYS.AUTH.USER_INFO,
44-
refetchOnWindowFocus: true,
4544
retry: false,
46-
staleTime: 2 * 60 * 1000
45+
staleTime: Infinity
4746
});
4847
}
4948

0 commit comments

Comments
 (0)