refactor(projects)!: refactor route cache & support reset route cache strategy

This commit is contained in:
Soybean 2024-10-25 00:54:17 +08:00
parent 4b3ac11bd5
commit b667eab76a
11 changed files with 76 additions and 65 deletions

View File

@ -54,3 +54,10 @@ export const themePageAnimationModeRecord: Record<UnionKey.ThemePageAnimateMode,
}; };
export const themePageAnimationModeOptions = transformRecordToOption(themePageAnimationModeRecord); export const themePageAnimationModeOptions = transformRecordToOption(themePageAnimationModeRecord);
export const resetCacheStrategyRecord: Record<UnionKey.ResetCacheStrategy, App.I18n.I18nKey> = {
close: 'theme.resetCacheStrategy.close',
refresh: 'theme.resetCacheStrategy.refresh'
};
export const resetCacheStrategyOptions = transformRecordToOption(resetCacheStrategyRecord);

View File

@ -42,7 +42,7 @@ function resetScroll() {
@after-leave="resetScroll" @after-leave="resetScroll"
@after-enter="appStore.setContentXScrollable(false)" @after-enter="appStore.setContentXScrollable(false)"
> >
<KeepAlive :include="routeStore.cacheRoutes"> <KeepAlive :include="routeStore.cacheRoutes" :exclude="routeStore.excludeCacheRoutes">
<component <component
:is="Component" :is="Component"
v-if="appStore.reloadFlag" v-if="appStore.reloadFlag"

View File

@ -84,7 +84,10 @@ function getContextMenuDisabledKeys(tabId: string) {
async function handleCloseTab(tab: App.Global.Tab) { async function handleCloseTab(tab: App.Global.Tab) {
await tabStore.removeTab(tab.id); await tabStore.removeTab(tab.id);
await routeStore.reCacheRoutesByKey(tab.routeKey);
if (themeStore.resetCacheStrategy === 'close') {
routeStore.resetRouteCache(tab.routeKey);
}
} }
async function refresh() { async function refresh() {

View File

@ -2,7 +2,12 @@
import { computed } from 'vue'; import { computed } from 'vue';
import { $t } from '@/locales'; import { $t } from '@/locales';
import { useThemeStore } from '@/store/modules/theme'; import { useThemeStore } from '@/store/modules/theme';
import { themePageAnimationModeOptions, themeScrollModeOptions, themeTabModeOptions } from '@/constants/app'; import {
resetCacheStrategyOptions,
themePageAnimationModeOptions,
themeScrollModeOptions,
themeTabModeOptions
} from '@/constants/app';
import { translateOptions } from '@/utils/common'; import { translateOptions } from '@/utils/common';
import SettingItem from '../components/setting-item.vue'; import SettingItem from '../components/setting-item.vue';
@ -22,6 +27,14 @@ const isWrapperScrollMode = computed(() => themeStore.layout.scrollMode === 'wra
<template> <template>
<NDivider>{{ $t('theme.pageFunTitle') }}</NDivider> <NDivider>{{ $t('theme.pageFunTitle') }}</NDivider>
<TransitionGroup tag="div" name="setting-list" class="flex-col-stretch gap-12px"> <TransitionGroup tag="div" name="setting-list" class="flex-col-stretch gap-12px">
<SettingItem key="0" :label="$t('theme.resetCacheStrategy.title')">
<NSelect
v-model:value="themeStore.resetCacheStrategy"
:options="translateOptions(resetCacheStrategyOptions)"
size="small"
class="w-120px"
/>
</SettingItem>
<SettingItem key="1" :label="$t('theme.scrollMode.title')"> <SettingItem key="1" :label="$t('theme.scrollMode.title')">
<NSelect <NSelect
v-model:value="themeStore.layout.scrollMode" v-model:value="themeStore.layout.scrollMode"

View File

@ -141,6 +141,11 @@ const local: App.I18n.Schema = {
}, },
themeDrawerTitle: 'Theme Configuration', themeDrawerTitle: 'Theme Configuration',
pageFunTitle: 'Page Function', pageFunTitle: 'Page Function',
resetCacheStrategy: {
title: 'Reset Cache Strategy',
close: 'Close Page',
refresh: 'Refresh Page'
},
configOperation: { configOperation: {
copyConfig: 'Copy Config', copyConfig: 'Copy Config',
copySuccessMsg: 'Copy Success, Please replace the variable "themeSettings" in "src/theme/settings.ts"', copySuccessMsg: 'Copy Success, Please replace the variable "themeSettings" in "src/theme/settings.ts"',

View File

@ -141,6 +141,11 @@ const local: App.I18n.Schema = {
}, },
themeDrawerTitle: '主题配置', themeDrawerTitle: '主题配置',
pageFunTitle: '页面功能', pageFunTitle: '页面功能',
resetCacheStrategy: {
title: '重置缓存策略',
close: '关闭页面',
refresh: '刷新页面'
},
configOperation: { configOperation: {
copyConfig: '复制配置', copyConfig: '复制配置',
copySuccessMsg: '复制成功,请替换 src/theme/settings.ts 中的变量 themeSettings', copySuccessMsg: '复制成功,请替换 src/theme/settings.ts 中的变量 themeSettings',

View File

@ -46,6 +46,10 @@ export const useAppStore = defineStore(SetupStoreId.App, () => {
}); });
setReloadFlag(true); setReloadFlag(true);
if (themeStore.resetCacheStrategy === 'refresh') {
routeStore.resetRouteCache();
}
} }
const locale = ref<App.I18n.LangType>(localStg.get('lang') || 'zh-CN'); const locale = ref<App.I18n.LangType>(localStg.get('lang') || 'zh-CN');

View File

@ -1,4 +1,4 @@
import { computed, ref, shallowRef } from 'vue'; import { computed, nextTick, ref, shallowRef } from 'vue';
import type { RouteRecordRaw } from 'vue-router'; import type { RouteRecordRaw } from 'vue-router';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { useBoolean } from '@sa/hooks'; import { useBoolean } from '@sa/hooks';
@ -9,7 +9,6 @@ import { createStaticRoutes, getAuthVueRoutes } from '@/router/routes';
import { ROOT_ROUTE } from '@/router/routes/builtin'; import { ROOT_ROUTE } from '@/router/routes/builtin';
import { getRouteName, getRoutePath } from '@/router/elegant/transform'; import { getRouteName, getRoutePath } from '@/router/elegant/transform';
import { fetchGetConstantRoutes, fetchGetUserRoutes, fetchIsRouteExist } from '@/service/api'; import { fetchGetConstantRoutes, fetchGetUserRoutes, fetchIsRouteExist } from '@/service/api';
import { useAppStore } from '../app';
import { useAuthStore } from '../auth'; import { useAuthStore } from '../auth';
import { useTabStore } from '../tab'; import { useTabStore } from '../tab';
import { import {
@ -25,7 +24,6 @@ import {
} from './shared'; } from './shared';
export const useRouteStore = defineStore(SetupStoreId.Route, () => { export const useRouteStore = defineStore(SetupStoreId.Route, () => {
const appStore = useAppStore();
const authStore = useAuthStore(); const authStore = useAuthStore();
const tabStore = useTabStore(); const tabStore = useTabStore();
const { bool: isInitConstantRoute, setBool: setIsInitConstantRoute } = useBoolean(); const { bool: isInitConstantRoute, setBool: setIsInitConstantRoute } = useBoolean();
@ -97,8 +95,12 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
/** Cache routes */ /** Cache routes */
const cacheRoutes = ref<RouteKey[]>([]); const cacheRoutes = ref<RouteKey[]>([]);
/** All cache routes */ /**
const allCacheRoutes = shallowRef<RouteKey[]>([]); * Exclude cache routes
*
* for reset route cache
*/
const excludeCacheRoutes = ref<RouteKey[]>([]);
/** /**
* Get cache routes * Get cache routes
@ -106,69 +108,23 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
* @param routes Vue routes * @param routes Vue routes
*/ */
function getCacheRoutes(routes: RouteRecordRaw[]) { function getCacheRoutes(routes: RouteRecordRaw[]) {
const alls = getCacheRouteNames(routes); cacheRoutes.value = getCacheRouteNames(routes);
cacheRoutes.value = alls;
allCacheRoutes.value = [...alls];
} }
/** /**
* Add cache routes * Reset route cache
* *
* @default router.currentRoute.value.name current route name
* @param routeKey * @param routeKey
*/ */
function addCacheRoutes(routeKey: RouteKey) { async function resetRouteCache(routeKey?: RouteKey) {
if (cacheRoutes.value.includes(routeKey)) return; const routeName = routeKey || (router.currentRoute.value.name as RouteKey);
cacheRoutes.value.push(routeKey); excludeCacheRoutes.value.push(routeName);
}
/** await nextTick();
* Remove cache routes
*
* @param routeKey
*/
function removeCacheRoutes(routeKey: RouteKey) {
const index = cacheRoutes.value.findIndex(item => item === routeKey);
if (index === -1) return; excludeCacheRoutes.value = [];
cacheRoutes.value.splice(index, 1);
}
/**
* Is cached route
*
* @param routeKey
*/
function isCachedRoute(routeKey: RouteKey) {
return allCacheRoutes.value.includes(routeKey);
}
/**
* Re cache routes by route key
*
* @param routeKey
*/
async function reCacheRoutesByKey(routeKey: RouteKey) {
if (!isCachedRoute(routeKey)) return;
removeCacheRoutes(routeKey);
await appStore.reloadPage();
addCacheRoutes(routeKey);
}
/**
* Re cache routes by route keys
*
* @param routeKeys
*/
async function reCacheRoutesByKeys(routeKeys: RouteKey[]) {
for await (const key of routeKeys) {
await reCacheRoutesByKey(key);
}
} }
/** Global breadcrumbs */ /** Global breadcrumbs */
@ -361,8 +317,8 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
searchMenus, searchMenus,
updateGlobalMenusByLocale, updateGlobalMenusByLocale,
cacheRoutes, cacheRoutes,
reCacheRoutesByKey, excludeCacheRoutes,
reCacheRoutesByKeys, resetRouteCache,
breadcrumbs, breadcrumbs,
initConstantRoute, initConstantRoute,
isInitConstantRoute, isInitConstantRoute,

View File

@ -12,6 +12,7 @@ export const themeSettings: App.Theme.ThemeSetting = {
error: '#f5222d' error: '#f5222d'
}, },
isInfoFollowPrimary: true, isInfoFollowPrimary: true,
resetCacheStrategy: 'close',
layout: { layout: {
mode: 'vertical', mode: 'vertical',
scrollMode: 'content', scrollMode: 'content',
@ -82,4 +83,10 @@ export const themeSettings: App.Theme.ThemeSetting = {
* *
* If publish new version, use `overrideThemeSettings` to override certain theme settings * If publish new version, use `overrideThemeSettings` to override certain theme settings
*/ */
export const overrideThemeSettings: Partial<App.Theme.ThemeSetting> = {}; export const overrideThemeSettings: Partial<App.Theme.ThemeSetting> = {
resetCacheStrategy: 'close',
watermark: {
visible: false,
text: 'SoybeanAdmin'
}
};

View File

@ -20,6 +20,8 @@ declare namespace App {
otherColor: OtherColor; otherColor: OtherColor;
/** Whether info color is followed by the primary color */ /** Whether info color is followed by the primary color */
isInfoFollowPrimary: boolean; isInfoFollowPrimary: boolean;
/** Reset cache strategy */
resetCacheStrategy?: UnionKey.ResetCacheStrategy;
/** Layout */ /** Layout */
layout: { layout: {
/** Layout mode */ /** Layout mode */
@ -388,6 +390,7 @@ declare namespace App {
}; };
themeDrawerTitle: string; themeDrawerTitle: string;
pageFunTitle: string; pageFunTitle: string;
resetCacheStrategy: { title: string } & Record<UnionKey.ResetCacheStrategy, string>;
configOperation: { configOperation: {
copyConfig: string; copyConfig: string;
copySuccessMsg: string; copySuccessMsg: string;

View File

@ -14,6 +14,14 @@ declare namespace UnionKey {
/** Theme scheme */ /** Theme scheme */
type ThemeScheme = 'light' | 'dark' | 'auto'; type ThemeScheme = 'light' | 'dark' | 'auto';
/**
* Reset cache strategy
*
* - close: re-cache when close page
* - refresh: re-cache when refresh page
*/
type ResetCacheStrategy = 'close' | 'refresh';
/** /**
* The layout mode * The layout mode
* *