Skip to content

Commit b667eab

Browse files
committed
refactor(projects)!: refactor route cache & support reset route cache strategy
1 parent 4b3ac11 commit b667eab

File tree

11 files changed

+76
-65
lines changed

11 files changed

+76
-65
lines changed

src/constants/app.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,10 @@ export const themePageAnimationModeRecord: Record<UnionKey.ThemePageAnimateMode,
5454
};
5555

5656
export const themePageAnimationModeOptions = transformRecordToOption(themePageAnimationModeRecord);
57+
58+
export const resetCacheStrategyRecord: Record<UnionKey.ResetCacheStrategy, App.I18n.I18nKey> = {
59+
close: 'theme.resetCacheStrategy.close',
60+
refresh: 'theme.resetCacheStrategy.refresh'
61+
};
62+
63+
export const resetCacheStrategyOptions = transformRecordToOption(resetCacheStrategyRecord);

src/layouts/modules/global-content/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function resetScroll() {
4242
@after-leave="resetScroll"
4343
@after-enter="appStore.setContentXScrollable(false)"
4444
>
45-
<KeepAlive :include="routeStore.cacheRoutes">
45+
<KeepAlive :include="routeStore.cacheRoutes" :exclude="routeStore.excludeCacheRoutes">
4646
<component
4747
:is="Component"
4848
v-if="appStore.reloadFlag"

src/layouts/modules/global-tab/index.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ function getContextMenuDisabledKeys(tabId: string) {
8484
8585
async function handleCloseTab(tab: App.Global.Tab) {
8686
await tabStore.removeTab(tab.id);
87-
await routeStore.reCacheRoutesByKey(tab.routeKey);
87+
88+
if (themeStore.resetCacheStrategy === 'close') {
89+
routeStore.resetRouteCache(tab.routeKey);
90+
}
8891
}
8992
9093
async function refresh() {

src/layouts/modules/theme-drawer/modules/page-fun.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
import { computed } from 'vue';
33
import { $t } from '@/locales';
44
import { useThemeStore } from '@/store/modules/theme';
5-
import { themePageAnimationModeOptions, themeScrollModeOptions, themeTabModeOptions } from '@/constants/app';
5+
import {
6+
resetCacheStrategyOptions,
7+
themePageAnimationModeOptions,
8+
themeScrollModeOptions,
9+
themeTabModeOptions
10+
} from '@/constants/app';
611
import { translateOptions } from '@/utils/common';
712
import SettingItem from '../components/setting-item.vue';
813
@@ -22,6 +27,14 @@ const isWrapperScrollMode = computed(() => themeStore.layout.scrollMode === 'wra
2227
<template>
2328
<NDivider>{{ $t('theme.pageFunTitle') }}</NDivider>
2429
<TransitionGroup tag="div" name="setting-list" class="flex-col-stretch gap-12px">
30+
<SettingItem key="0" :label="$t('theme.resetCacheStrategy.title')">
31+
<NSelect
32+
v-model:value="themeStore.resetCacheStrategy"
33+
:options="translateOptions(resetCacheStrategyOptions)"
34+
size="small"
35+
class="w-120px"
36+
/>
37+
</SettingItem>
2538
<SettingItem key="1" :label="$t('theme.scrollMode.title')">
2639
<NSelect
2740
v-model:value="themeStore.layout.scrollMode"

src/locales/langs/en-us.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ const local: App.I18n.Schema = {
141141
},
142142
themeDrawerTitle: 'Theme Configuration',
143143
pageFunTitle: 'Page Function',
144+
resetCacheStrategy: {
145+
title: 'Reset Cache Strategy',
146+
close: 'Close Page',
147+
refresh: 'Refresh Page'
148+
},
144149
configOperation: {
145150
copyConfig: 'Copy Config',
146151
copySuccessMsg: 'Copy Success, Please replace the variable "themeSettings" in "src/theme/settings.ts"',

src/locales/langs/zh-cn.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ const local: App.I18n.Schema = {
141141
},
142142
themeDrawerTitle: '主题配置',
143143
pageFunTitle: '页面功能',
144+
resetCacheStrategy: {
145+
title: '重置缓存策略',
146+
close: '关闭页面',
147+
refresh: '刷新页面'
148+
},
144149
configOperation: {
145150
copyConfig: '复制配置',
146151
copySuccessMsg: '复制成功,请替换 src/theme/settings.ts 中的变量 themeSettings',

src/store/modules/app/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export const useAppStore = defineStore(SetupStoreId.App, () => {
4646
});
4747

4848
setReloadFlag(true);
49+
50+
if (themeStore.resetCacheStrategy === 'refresh') {
51+
routeStore.resetRouteCache();
52+
}
4953
}
5054

5155
const locale = ref<App.I18n.LangType>(localStg.get('lang') || 'zh-CN');

src/store/modules/route/index.ts

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, ref, shallowRef } from 'vue';
1+
import { computed, nextTick, ref, shallowRef } from 'vue';
22
import type { RouteRecordRaw } from 'vue-router';
33
import { defineStore } from 'pinia';
44
import { useBoolean } from '@sa/hooks';
@@ -9,7 +9,6 @@ import { createStaticRoutes, getAuthVueRoutes } from '@/router/routes';
99
import { ROOT_ROUTE } from '@/router/routes/builtin';
1010
import { getRouteName, getRoutePath } from '@/router/elegant/transform';
1111
import { fetchGetConstantRoutes, fetchGetUserRoutes, fetchIsRouteExist } from '@/service/api';
12-
import { useAppStore } from '../app';
1312
import { useAuthStore } from '../auth';
1413
import { useTabStore } from '../tab';
1514
import {
@@ -25,7 +24,6 @@ import {
2524
} from './shared';
2625

2726
export const useRouteStore = defineStore(SetupStoreId.Route, () => {
28-
const appStore = useAppStore();
2927
const authStore = useAuthStore();
3028
const tabStore = useTabStore();
3129
const { bool: isInitConstantRoute, setBool: setIsInitConstantRoute } = useBoolean();
@@ -97,78 +95,36 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
9795
/** Cache routes */
9896
const cacheRoutes = ref<RouteKey[]>([]);
9997

100-
/** All cache routes */
101-
const allCacheRoutes = shallowRef<RouteKey[]>([]);
102-
103-
/**
104-
* Get cache routes
105-
*
106-
* @param routes Vue routes
107-
*/
108-
function getCacheRoutes(routes: RouteRecordRaw[]) {
109-
const alls = getCacheRouteNames(routes);
110-
111-
cacheRoutes.value = alls;
112-
allCacheRoutes.value = [...alls];
113-
}
114-
11598
/**
116-
* Add cache routes
99+
* Exclude cache routes
117100
*
118-
* @param routeKey
101+
* for reset route cache
119102
*/
120-
function addCacheRoutes(routeKey: RouteKey) {
121-
if (cacheRoutes.value.includes(routeKey)) return;
122-
123-
cacheRoutes.value.push(routeKey);
124-
}
103+
const excludeCacheRoutes = ref<RouteKey[]>([]);
125104

126105
/**
127-
* Remove cache routes
128-
*
129-
* @param routeKey
130-
*/
131-
function removeCacheRoutes(routeKey: RouteKey) {
132-
const index = cacheRoutes.value.findIndex(item => item === routeKey);
133-
134-
if (index === -1) return;
135-
136-
cacheRoutes.value.splice(index, 1);
137-
}
138-
139-
/**
140-
* Is cached route
106+
* Get cache routes
141107
*
142-
* @param routeKey
108+
* @param routes Vue routes
143109
*/
144-
function isCachedRoute(routeKey: RouteKey) {
145-
return allCacheRoutes.value.includes(routeKey);
110+
function getCacheRoutes(routes: RouteRecordRaw[]) {
111+
cacheRoutes.value = getCacheRouteNames(routes);
146112
}
147113

148114
/**
149-
* Re cache routes by route key
115+
* Reset route cache
150116
*
117+
* @default router.currentRoute.value.name current route name
151118
* @param routeKey
152119
*/
153-
async function reCacheRoutesByKey(routeKey: RouteKey) {
154-
if (!isCachedRoute(routeKey)) return;
155-
156-
removeCacheRoutes(routeKey);
120+
async function resetRouteCache(routeKey?: RouteKey) {
121+
const routeName = routeKey || (router.currentRoute.value.name as RouteKey);
157122

158-
await appStore.reloadPage();
123+
excludeCacheRoutes.value.push(routeName);
159124

160-
addCacheRoutes(routeKey);
161-
}
125+
await nextTick();
162126

163-
/**
164-
* Re cache routes by route keys
165-
*
166-
* @param routeKeys
167-
*/
168-
async function reCacheRoutesByKeys(routeKeys: RouteKey[]) {
169-
for await (const key of routeKeys) {
170-
await reCacheRoutesByKey(key);
171-
}
127+
excludeCacheRoutes.value = [];
172128
}
173129

174130
/** Global breadcrumbs */
@@ -361,8 +317,8 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
361317
searchMenus,
362318
updateGlobalMenusByLocale,
363319
cacheRoutes,
364-
reCacheRoutesByKey,
365-
reCacheRoutesByKeys,
320+
excludeCacheRoutes,
321+
resetRouteCache,
366322
breadcrumbs,
367323
initConstantRoute,
368324
isInitConstantRoute,

src/theme/settings.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const themeSettings: App.Theme.ThemeSetting = {
1212
error: '#f5222d'
1313
},
1414
isInfoFollowPrimary: true,
15+
resetCacheStrategy: 'close',
1516
layout: {
1617
mode: 'vertical',
1718
scrollMode: 'content',
@@ -82,4 +83,10 @@ export const themeSettings: App.Theme.ThemeSetting = {
8283
*
8384
* If publish new version, use `overrideThemeSettings` to override certain theme settings
8485
*/
85-
export const overrideThemeSettings: Partial<App.Theme.ThemeSetting> = {};
86+
export const overrideThemeSettings: Partial<App.Theme.ThemeSetting> = {
87+
resetCacheStrategy: 'close',
88+
watermark: {
89+
visible: false,
90+
text: 'SoybeanAdmin'
91+
}
92+
};

src/typings/app.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ declare namespace App {
2020
otherColor: OtherColor;
2121
/** Whether info color is followed by the primary color */
2222
isInfoFollowPrimary: boolean;
23+
/** Reset cache strategy */
24+
resetCacheStrategy?: UnionKey.ResetCacheStrategy;
2325
/** Layout */
2426
layout: {
2527
/** Layout mode */
@@ -388,6 +390,7 @@ declare namespace App {
388390
};
389391
themeDrawerTitle: string;
390392
pageFunTitle: string;
393+
resetCacheStrategy: { title: string } & Record<UnionKey.ResetCacheStrategy, string>;
391394
configOperation: {
392395
copyConfig: string;
393396
copySuccessMsg: string;

0 commit comments

Comments
 (0)