fix(tabbar): visitHistory field (#7543)

High Severity

The visitHistory field is a Stack<string> class instance persisted to sessionStorage via pinia-plugin-persistedstate. There's no custom serializer or hydration hook. When the page reloads, JSON.parse(JSON.stringify(stack)) produces a plain object {dedup, items, maxSize} that lacks all Stack methods (push, pop, remove, retain, etc.) and the size getter. Pinia's $patch replaces the Stack instance with this plain object, so subsequent calls like this.visitHistory.push(...) will throw a TypeError.
This commit is contained in:
AxiosLeo
2026-02-11 16:09:37 +08:00
committed by GitHub
parent 32379ba4b7
commit aa74a2535b

View File

@@ -1,9 +1,5 @@
import type { ComputedRef } from 'vue'; import type { ComputedRef } from 'vue';
import type { import type { RouteLocationNormalized, Router, RouteRecordNormalized } from 'vue-router';
RouteLocationNormalized,
Router,
RouteRecordNormalized,
} from 'vue-router';
import type { TabDefinition } from '@vben-core/typings'; import type { TabDefinition } from '@vben-core/typings';
@@ -70,9 +66,7 @@ export const useTabbarStore = defineStore('core-tabbar', {
*/ */
async _bulkCloseByKeys(keys: string[]) { async _bulkCloseByKeys(keys: string[]) {
const keySet = new Set(keys); const keySet = new Set(keys);
this.tabs = this.tabs.filter( this.tabs = this.tabs.filter((item) => !keySet.has(getTabKeyFromTab(item)));
(item) => !keySet.has(getTabKeyFromTab(item)),
);
if (isVisitHistory()) { if (isVisitHistory()) {
this.visitHistory.remove(...keys); this.visitHistory.remove(...keys);
} }
@@ -136,25 +130,20 @@ export const useTabbarStore = defineStore('core-tabbar', {
if (tabIndex === -1) { if (tabIndex === -1) {
const maxCount = preferences.tabbar.maxCount; const maxCount = preferences.tabbar.maxCount;
// 获取动态路由打开数,超过 0 即代表需要控制打开数 // 获取动态路由打开数,超过 0 即代表需要控制打开数
const maxNumOfOpenTab = (routeTab?.meta?.maxNumOfOpenTab ?? const maxNumOfOpenTab = (routeTab?.meta?.maxNumOfOpenTab ?? -1) as number;
-1) as number;
// 如果动态路由层级大于 0 了,那么就要限制该路由的打开数限制了 // 如果动态路由层级大于 0 了,那么就要限制该路由的打开数限制了
// 获取到已经打开的动态路由数, 判断是否大于某一个值 // 获取到已经打开的动态路由数, 判断是否大于某一个值
if ( if (
maxNumOfOpenTab > 0 && maxNumOfOpenTab > 0 &&
this.tabs.filter((tab) => tab.name === routeTab.name).length >= this.tabs.filter((tab) => tab.name === routeTab.name).length >= maxNumOfOpenTab
maxNumOfOpenTab
) { ) {
// 关闭第一个 // 关闭第一个
const index = this.tabs.findIndex( const index = this.tabs.findIndex((item) => item.name === routeTab.name);
(item) => item.name === routeTab.name,
);
index !== -1 && this.tabs.splice(index, 1); index !== -1 && this.tabs.splice(index, 1);
} else if (maxCount > 0 && this.tabs.length >= maxCount) { } else if (maxCount > 0 && this.tabs.length >= maxCount) {
// 关闭第一个 // 关闭第一个
const index = this.tabs.findIndex( const index = this.tabs.findIndex(
(item) => (item) => !Reflect.has(item.meta, 'affixTab') || !item.meta.affixTab
!Reflect.has(item.meta, 'affixTab') || !item.meta.affixTab,
); );
index !== -1 && this.tabs.splice(index, 1); index !== -1 && this.tabs.splice(index, 1);
} }
@@ -194,9 +183,7 @@ export const useTabbarStore = defineStore('core-tabbar', {
this.tabs = newTabs.length > 0 ? newTabs : [...this.tabs].splice(0, 1); this.tabs = newTabs.length > 0 ? newTabs : [...this.tabs].splice(0, 1);
// 设置访问历史记录 // 设置访问历史记录
if (isVisitHistory()) { if (isVisitHistory()) {
this.visitHistory.retain( this.visitHistory.retain(this.tabs.map((item) => getTabKeyFromTab(item)));
this.tabs.map((item) => getTabKeyFromTab(item)),
);
} }
await this._goToDefaultTab(router); await this._goToDefaultTab(router);
this.updateCacheTabs(); this.updateCacheTabs();
@@ -233,9 +220,7 @@ export const useTabbarStore = defineStore('core-tabbar', {
for (const key of closeKeys) { for (const key of closeKeys) {
if (key !== getTabKeyFromTab(tab)) { if (key !== getTabKeyFromTab(tab)) {
const closeTab = this.tabs.find( const closeTab = this.tabs.find((item) => getTabKeyFromTab(item) === key);
(item) => getTabKeyFromTab(item) === key,
);
if (!closeTab) { if (!closeTab) {
continue; continue;
} }
@@ -305,14 +290,12 @@ export const useTabbarStore = defineStore('core-tabbar', {
break; break;
} }
} }
await (previousTab await (previousTab ? this._goToTab(previousTab, router) : this._goToDefaultTab(router));
? this._goToTab(previousTab, router)
: this._goToDefaultTab(router));
return; return;
} }
// 未开启访问历史记录直接跳转下一个或上一个tab // 未开启访问历史记录直接跳转下一个或上一个tab
const index = this.getTabs.findIndex( const index = this.getTabs.findIndex(
(item) => getTabKeyFromTab(item) === getTabKey(currentRoute.value), (item) => getTabKeyFromTab(item) === getTabKey(currentRoute.value)
); );
const before = this.getTabs[index - 1]; const before = this.getTabs[index - 1];
@@ -336,9 +319,7 @@ export const useTabbarStore = defineStore('core-tabbar', {
*/ */
async closeTabByKey(key: string, router: Router) { async closeTabByKey(key: string, router: Router) {
const originKey = decodeURIComponent(key); const originKey = decodeURIComponent(key);
const index = this.tabs.findIndex( const index = this.tabs.findIndex((item) => getTabKeyFromTab(item) === originKey);
(item) => getTabKeyFromTab(item) === originKey,
);
if (index === -1) { if (index === -1) {
return; return;
} }
@@ -354,9 +335,7 @@ export const useTabbarStore = defineStore('core-tabbar', {
* @param key * @param key
*/ */
getTabByKey(key: string) { getTabByKey(key: string) {
return this.getTabs.find( return this.getTabs.find((item) => getTabKeyFromTab(item) === key) as TabDefinition;
(item) => getTabKeyFromTab(item) === key,
) as TabDefinition;
}, },
/** /**
* @zh_CN 新窗口打开标签页 * @zh_CN 新窗口打开标签页
@@ -583,6 +562,23 @@ export const useTabbarStore = defineStore('core-tabbar', {
{ {
pick: ['tabs', 'visitHistory'], pick: ['tabs', 'visitHistory'],
storage: sessionStorage, storage: sessionStorage,
serializer: {
serialize: JSON.stringify,
deserialize(value: string) {
const parsed = JSON.parse(value);
// Stack 类实例经 JSON 序列化后会变成普通对象 {dedup, items, maxSize}
// 丢失所有方法和 getter需要重新构建 Stack 实例
if (parsed.visitHistory && !(parsed.visitHistory instanceof Stack)) {
const raw = parsed.visitHistory;
const stack = createStack<string>(true, MAX_VISIT_HISTORY);
if (Array.isArray(raw.items)) {
stack.push(...raw.items);
}
parsed.visitHistory = stack;
}
return parsed;
},
},
}, },
], ],
state: (): TabbarState => ({ state: (): TabbarState => ({
@@ -660,21 +656,14 @@ function isTabShown(tab: TabDefinition) {
* @param tab * @param tab
*/ */
function getTabKey(tab: RouteLocationNormalized | RouteRecordNormalized) { function getTabKey(tab: RouteLocationNormalized | RouteRecordNormalized) {
const { const { fullPath, path, meta: { fullPathKey } = {}, query = {} } = tab as RouteLocationNormalized;
fullPath,
path,
meta: { fullPathKey } = {},
query = {},
} = tab as RouteLocationNormalized;
// pageKey可能是数组查询参数重复时可能出现 // pageKey可能是数组查询参数重复时可能出现
const pageKey = Array.isArray(query.pageKey) const pageKey = Array.isArray(query.pageKey) ? query.pageKey[0] : query.pageKey;
? query.pageKey[0]
: query.pageKey;
let rawKey; let rawKey;
if (pageKey) { if (pageKey) {
rawKey = pageKey; rawKey = pageKey;
} else { } else {
rawKey = fullPathKey === false ? path : (fullPath ?? path); rawKey = fullPathKey === false ? path : fullPath ?? path;
} }
try { try {
return decodeURIComponent(rawKey); return decodeURIComponent(rawKey);