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

Conversation

LOUSANPANG
Copy link

1、appld 和 chatld 的存储方式: 在 usechatStore.ts 中,appld、chatld 和 source 被存储在 sessionStorage中,而其他状态被存储在 localstorage 中。
2、iframe 共享存储: 同一域名下的多个 iframe 共享相同的 sessionStorage 和 localStorage,这可能导致状态混乱。
3、状态恢复逻辑: 当页面加载时,FastGPT 会尝试从存储中恢复状态,但这个恢复逻辑可能在多个 iframe 快速切换时出现问题。

Copy link
Contributor

gru-agent bot commented May 14, 2025

TestGru Assignment

Summary

Link CommitId Status Reason
Detail e3eb002 ✅ Finished

Files

File Pull Request
projects/app/src/web/core/chat/context/useChatStore.ts 🔴 Closed #4812

Tip

You can @gru-agent and leave your feedback. TestGru will make adjustments based on your input

Copy link

cla-assistant bot commented May 14, 2025

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link

Preview mcp_server Image: ghcr.io/labring/fastgpt-pr:fatsgpt_mcp_server_e3eb002222350bc4d9a26e74b5c66a187d6e04a9

Copy link

Preview sandbox Image: ghcr.io/labring/fastgpt-pr:fatsgpt_sandbox_e3eb002222350bc4d9a26e74b5c66a187d6e04a9

Copy link

Preview fastgpt Image: ghcr.io/labring/fastgpt-pr:fatsgpt_e3eb002222350bc4d9a26e74b5c66a187d6e04a9

@c121914yu
Copy link
Collaborator

c121914yu commented May 20, 2025

多个 iframe, sessionStorage 不共享的呢,只有 localStorage 共享。
“当页面加载时,FastGPT 会尝试从存储中恢复状态,但这个恢复逻辑可能在多个 iframe 快速切换时出现问题。”,这个可以具体讲讲么?

@LOUSANPANG
Copy link
Author

多个 iframe, sessionStorage 不共享的呢,只有 localStorage 共享。 “当页面加载时,FastGPT 会尝试从存储中恢复状态,但这个恢复逻辑可能在多个 iframe 快速切换时出现问题。”,这个可以具体讲讲么?

问题描述:
基于FastGPT生成多个chat外部链接后,在同一域名下例如 https://aaa.com/chat?appId=123、https://aaa.com/chat?appId=456,嵌入到https://aaa.com网页下,两个chat频繁切换会导致页面紊乱。

思考问题发生的过程:

  • 快速在父页面中切换并显示不同的 iframe 时,这些 iframe 实际上在竞争同一个 sessionStorage (如果多个 iframe 嵌入在同一个父页面中,并且这些 iframe 与父页面 同源 (协议、域名、端口都相同),那么它们将 共享同一个父窗口的 sessionStorage )
  • iframe A (例如 chatId=111 ) 加载,它可能会将 chatId: "111" 写入 sessionStorage 。
  • 紧接着, iframe B (例如 chatId=222 ) 加载。在它从 URL 读取并设置自己的 chatId 之前, useChatStore 的初始化逻辑可能会先运行。
  • 如果 iframe B 的 setSource 被调用,它可能会看到 sessionStorage 中由 iframe A(或其他 iframe)留下的 chatId 或从 localStorage 恢复的 lastChatId ,导致它错误地将自己的 chatId 设置为不属于它的值。
  • 即使 iframe B 稍后通过 URL 参数正确调用了 setChatId("222") ,但如果在这之前已经基于错误的 chatId 发起了数据请求(例如在 useEffect 中依赖了 store 中的 chatId ),那么就可能拉取到错误的数据。
  • 由于 lastChatId 和 lastChatAppId 存储在 localStorage 中,它们是所有 iframe 和标签页共享的。当多个 iframe 同时初始化时,它们都可能尝试从相同的 lastChatId 和 lastChatAppId 恢复状态,这进一步加剧了混乱

代码示例:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
</head>

<body>

辩论
<iframe style="width: 49vw; height: 49vh; display: inline-block;" src="https://aaa.com/chat?appId=123&token=xxx"></iframe>

面试
<iframe style="width: 49vw; height: 49vh; display: inline-block;" src="https://aaa.com/chat?appId=456&token=xxx"></iframe>

编程
<iframe style="width: 49vw; height: 49vh; display: inline-block;" src="https://aaa.com/chat?appId=789&token=xxx"></iframe>

英语
<iframe style="width: 49vw; height: 49vh; display: inline-block;" src="https://aaa.com/chat?appId=000&token=xxx"></iframe>

</body>

页面展示:(多次刷新后会发现辩论助手变成了英语助手页面不相符问题)
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants