From 0ad7764e3e91b597c2ae8c3089268e1f07f787e5 Mon Sep 17 00:00:00 2001 From: haorwen Date: Sun, 15 Mar 2026 07:59:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8F=90=E5=8F=96widget=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E9=80=BB=E8=BE=91=E5=88=B0=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E5=B9=B6=E5=A2=9E=E5=8A=A0=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/app/config.ts | 3 +++ src/index-widget.tsx | 22 ++-------------------- src/widget-init.ts | 20 ++++++++++++++++++++ 4 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 src/widget-init.ts diff --git a/package.json b/package.json index 286a1f26..79af5fb1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vocechat-web", - "version": "0.9.64", + "version": "0.9.65", "homepage": "https://voce.chat", "dependencies": { "@metamask/onboarding": "^1.0.1", diff --git a/src/app/config.ts b/src/app/config.ts index a57daa90..ce5a14b6 100644 --- a/src/app/config.ts +++ b/src/app/config.ts @@ -23,10 +23,13 @@ const getBaseOrigin = () => { // 检查是否有 widget origin(Shadow DOM 模式) const widgetOrigin = (window as any).__VOCECHAT_WIDGET_ORIGIN__; if (widgetOrigin) { + console.log('[Config] Using widget origin:', widgetOrigin); return widgetOrigin; } + console.log('[Config] Using location.origin:', location.origin); return location.origin; } + console.log('[Config] Using dev origin:', dev_origin); return dev_origin; }; diff --git a/src/index-widget.tsx b/src/index-widget.tsx index 8cd1369c..1f0f4db1 100644 --- a/src/index-widget.tsx +++ b/src/index-widget.tsx @@ -1,23 +1,5 @@ -// 在 Shadow DOM 模式下,动态设置 webpack 的 publicPath -// 这样 webpack 运行时加载的动态资源(如代码分割的 chunk)会从正确的源服务器加载 -let widgetOrigin = ''; -if (typeof document !== 'undefined') { - const currentScript = document.currentScript as HTMLScriptElement; - if (currentScript && currentScript.src) { - try { - const scriptUrl = new URL(currentScript.src); - // 获取脚本所在的目录路径(去掉文件名) - const scriptPath = scriptUrl.pathname.substring(0, scriptUrl.pathname.lastIndexOf('/') + 1); - // 设置为脚本所在的完整目录路径 - __webpack_public_path__ = scriptUrl.origin + scriptPath; - // 保存 origin 供 i18n 使用 - widgetOrigin = scriptUrl.origin; - (window as any).__VOCECHAT_WIDGET_ORIGIN__ = widgetOrigin; - } catch (e) { - console.warn('Failed to set webpack public path:', e); - } - } -} +// 必须首先导入初始化文件,设置 webpack publicPath 和 widget origin +import "./widget-init"; import ReactDOM from "react-dom/client"; import { Toaster } from "react-hot-toast"; diff --git a/src/widget-init.ts b/src/widget-init.ts new file mode 100644 index 00000000..e4dcbd87 --- /dev/null +++ b/src/widget-init.ts @@ -0,0 +1,20 @@ +// 这个文件必须在所有其他模块之前加载 +// 在 Shadow DOM 模式下,动态设置 webpack 的 publicPath 和 widget origin +if (typeof document !== 'undefined') { + const currentScript = document.currentScript as HTMLScriptElement; + if (currentScript && currentScript.src) { + try { + const scriptUrl = new URL(currentScript.src); + // 获取脚本所在的目录路径(去掉文件名) + const scriptPath = scriptUrl.pathname.substring(0, scriptUrl.pathname.lastIndexOf('/') + 1); + // 设置为脚本所在的完整目录路径 + __webpack_public_path__ = scriptUrl.origin + scriptPath; + // 保存 origin 供其他模块使用 + (window as any).__VOCECHAT_WIDGET_ORIGIN__ = scriptUrl.origin; + console.log('[Widget Init] Set origin to:', scriptUrl.origin); + console.log('[Widget Init] Set publicPath to:', scriptUrl.origin + scriptPath); + } catch (e) { + console.warn('[Widget Init] Failed to set webpack public path:', e); + } + } +}