Skip to content

Commit

Permalink
refactor(projects)!: refactor route cache & support reset route cache…
Browse files Browse the repository at this point in the history
… strategy
  • Loading branch information
honghuangdc committed Oct 24, 2024
1 parent 4b3ac11 commit b667eab
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 65 deletions.
7 changes: 7 additions & 0 deletions src/constants/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ export const themePageAnimationModeRecord: Record<UnionKey.ThemePageAnimateMode,
};

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);
2 changes: 1 addition & 1 deletion src/layouts/modules/global-content/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function resetScroll() {
@after-leave="resetScroll"
@after-enter="appStore.setContentXScrollable(false)"
>
<KeepAlive :include="routeStore.cacheRoutes">
<KeepAlive :include="routeStore.cacheRoutes" :exclude="routeStore.excludeCacheRoutes">
<component
:is="Component"
v-if="appStore.reloadFlag"
Expand Down
5 changes: 4 additions & 1 deletion src/layouts/modules/global-tab/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ function getContextMenuDisabledKeys(tabId: string) {
async function handleCloseTab(tab: App.Global.Tab) {
await tabStore.removeTab(tab.id);
await routeStore.reCacheRoutesByKey(tab.routeKey);
if (themeStore.resetCacheStrategy === 'close') {
routeStore.resetRouteCache(tab.routeKey);
}
}
async function refresh() {
Expand Down
15 changes: 14 additions & 1 deletion src/layouts/modules/theme-drawer/modules/page-fun.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import { computed } from 'vue';
import { $t } from '@/locales';
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 SettingItem from '../components/setting-item.vue';
Expand All @@ -22,6 +27,14 @@ const isWrapperScrollMode = computed(() => themeStore.layout.scrollMode === 'wra
<template>
<NDivider>{{ $t('theme.pageFunTitle') }}</NDivider>
<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')">
<NSelect
v-model:value="themeStore.layout.scrollMode"
Expand Down
5 changes: 5 additions & 0 deletions src/locales/langs/en-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ const local: App.I18n.Schema = {
},
themeDrawerTitle: 'Theme Configuration',
pageFunTitle: 'Page Function',
resetCacheStrategy: {
title: 'Reset Cache Strategy',
close: 'Close Page',
refresh: 'Refresh Page'
},
configOperation: {
copyConfig: 'Copy Config',
copySuccessMsg: 'Copy Success, Please replace the variable "themeSettings" in "src/theme/settings.ts"',
Expand Down
5 changes: 5 additions & 0 deletions src/locales/langs/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ const local: App.I18n.Schema = {
},
themeDrawerTitle: '主题配置',
pageFunTitle: '页面功能',
resetCacheStrategy: {
title: '重置缓存策略',
close: '关闭页面',
refresh: '刷新页面'
},
configOperation: {
copyConfig: '复制配置',
copySuccessMsg: '复制成功,请替换 src/theme/settings.ts 中的变量 themeSettings',
Expand Down
4 changes: 4 additions & 0 deletions src/store/modules/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export const useAppStore = defineStore(SetupStoreId.App, () => {
});

setReloadFlag(true);

if (themeStore.resetCacheStrategy === 'refresh') {
routeStore.resetRouteCache();
}
}

const locale = ref<App.I18n.LangType>(localStg.get('lang') || 'zh-CN');
Expand Down
78 changes: 17 additions & 61 deletions src/store/modules/route/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, ref, shallowRef } from 'vue';
import { computed, nextTick, ref, shallowRef } from 'vue';
import type { RouteRecordRaw } from 'vue-router';
import { defineStore } from 'pinia';
import { useBoolean } from '@sa/hooks';
Expand All @@ -9,7 +9,6 @@ import { createStaticRoutes, getAuthVueRoutes } from '@/router/routes';
import { ROOT_ROUTE } from '@/router/routes/builtin';
import { getRouteName, getRoutePath } from '@/router/elegant/transform';
import { fetchGetConstantRoutes, fetchGetUserRoutes, fetchIsRouteExist } from '@/service/api';
import { useAppStore } from '../app';
import { useAuthStore } from '../auth';
import { useTabStore } from '../tab';
import {
Expand All @@ -25,7 +24,6 @@ import {
} from './shared';

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

/** All cache routes */
const allCacheRoutes = shallowRef<RouteKey[]>([]);

/**
* Get cache routes
*
* @param routes Vue routes
*/
function getCacheRoutes(routes: RouteRecordRaw[]) {
const alls = getCacheRouteNames(routes);

cacheRoutes.value = alls;
allCacheRoutes.value = [...alls];
}

/**
* Add cache routes
* Exclude cache routes
*
* @param routeKey
* for reset route cache
*/
function addCacheRoutes(routeKey: RouteKey) {
if (cacheRoutes.value.includes(routeKey)) return;

cacheRoutes.value.push(routeKey);
}
const excludeCacheRoutes = ref<RouteKey[]>([]);

/**
* Remove cache routes
*
* @param routeKey
*/
function removeCacheRoutes(routeKey: RouteKey) {
const index = cacheRoutes.value.findIndex(item => item === routeKey);

if (index === -1) return;

cacheRoutes.value.splice(index, 1);
}

/**
* Is cached route
* Get cache routes
*
* @param routeKey
* @param routes Vue routes
*/
function isCachedRoute(routeKey: RouteKey) {
return allCacheRoutes.value.includes(routeKey);
function getCacheRoutes(routes: RouteRecordRaw[]) {
cacheRoutes.value = getCacheRouteNames(routes);
}

/**
* Re cache routes by route key
* Reset route cache
*
* @default router.currentRoute.value.name current route name
* @param routeKey
*/
async function reCacheRoutesByKey(routeKey: RouteKey) {
if (!isCachedRoute(routeKey)) return;

removeCacheRoutes(routeKey);
async function resetRouteCache(routeKey?: RouteKey) {
const routeName = routeKey || (router.currentRoute.value.name as RouteKey);

await appStore.reloadPage();
excludeCacheRoutes.value.push(routeName);

addCacheRoutes(routeKey);
}
await nextTick();

/**
* Re cache routes by route keys
*
* @param routeKeys
*/
async function reCacheRoutesByKeys(routeKeys: RouteKey[]) {
for await (const key of routeKeys) {
await reCacheRoutesByKey(key);
}
excludeCacheRoutes.value = [];
}

/** Global breadcrumbs */
Expand Down Expand Up @@ -361,8 +317,8 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
searchMenus,
updateGlobalMenusByLocale,
cacheRoutes,
reCacheRoutesByKey,
reCacheRoutesByKeys,
excludeCacheRoutes,
resetRouteCache,
breadcrumbs,
initConstantRoute,
isInitConstantRoute,
Expand Down
9 changes: 8 additions & 1 deletion src/theme/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const themeSettings: App.Theme.ThemeSetting = {
error: '#f5222d'
},
isInfoFollowPrimary: true,
resetCacheStrategy: 'close',
layout: {
mode: 'vertical',
scrollMode: 'content',
Expand Down Expand Up @@ -82,4 +83,10 @@ export const themeSettings: App.Theme.ThemeSetting = {
*
* 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'
}
};
3 changes: 3 additions & 0 deletions src/typings/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ declare namespace App {
otherColor: OtherColor;
/** Whether info color is followed by the primary color */
isInfoFollowPrimary: boolean;
/** Reset cache strategy */
resetCacheStrategy?: UnionKey.ResetCacheStrategy;
/** Layout */
layout: {
/** Layout mode */
Expand Down Expand Up @@ -388,6 +390,7 @@ declare namespace App {
};
themeDrawerTitle: string;
pageFunTitle: string;
resetCacheStrategy: { title: string } & Record<UnionKey.ResetCacheStrategy, string>;
configOperation: {
copyConfig: string;
copySuccessMsg: string;
Expand Down
8 changes: 8 additions & 0 deletions src/typings/union-key.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ declare namespace UnionKey {
/** Theme scheme */
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
*
Expand Down

0 comments on commit b667eab

Please sign in to comment.