Skip to content

修复同一域名下的多个 iframe 共享相同的缓存,可能导致页面展示混乱问题 #4811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions projects/app/src/web/core/chat/context/useChatStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ type State = {
const createCustomStorage = () => {
const sessionKeys = ['source', 'chatId', 'appId'];

// 从 URL 中获取 appId 作为存储键的一部分
const getStorageKey = (name: string) => {
let appId = '';
if (typeof window !== 'undefined') {
const urlParams = new URLSearchParams(window.location.search);
appId = urlParams.get('appId') || '';
}
return appId ? `${name}_${appId}` : name;
};

return {
getItem: (name: string) => {
const sessionData = JSON.parse(sessionStorage.getItem(name) || '{}');
const localData = JSON.parse(localStorage.getItem(name) || '{}');
const storageKey = getStorageKey(name);
const sessionData = JSON.parse(sessionStorage.getItem(storageKey) || '{}');
const localData = JSON.parse(localStorage.getItem(storageKey) || '{}');
return JSON.stringify({ ...localData, ...sessionData });
},
setItem: (name: string, value: string) => {
const storageKey = getStorageKey(name);
const data = JSON.parse(value);

// 分离 session 和 local 数据
Expand All @@ -42,15 +54,16 @@ const createCustomStorage = () => {

// 分别存储
if (Object.keys(sessionData).length > 0) {
sessionStorage.setItem(name, JSON.stringify({ state: sessionData, version: 0 }));
sessionStorage.setItem(storageKey, JSON.stringify({ state: sessionData, version: 0 }));
}
if (Object.keys(localData).length > 0) {
localStorage.setItem(name, JSON.stringify({ state: localData, version: 0 }));
localStorage.setItem(storageKey, JSON.stringify({ state: localData, version: 0 }));
}
},
removeItem: (name: string) => {
sessionStorage.removeItem(name);
localStorage.removeItem(name);
const storageKey = getStorageKey(name);
sessionStorage.removeItem(storageKey);
localStorage.removeItem(storageKey);
}
};
};
Expand Down