From e23b2207b125657b8e4f95f5248d94d0964d6f10 Mon Sep 17 00:00:00 2001 From: haorwen Date: Sun, 15 Mar 2026 07:28:10 +0800 Subject: [PATCH] chore: bump version to v0.9.64 and enhance i18n support for Shadow DOM mode --- package.json | 2 +- src/app/config.ts | 15 ++++++++++++++- src/i18n.ts | 8 ++++++++ src/index-widget.tsx | 4 ++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0d3972d5..286a1f26 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vocechat-web", - "version": "0.9.63", + "version": "0.9.64", "homepage": "https://voce.chat", "dependencies": { "@metamask/onboarding": "^1.0.1", diff --git a/src/app/config.ts b/src/app/config.ts index 26da179a..a57daa90 100644 --- a/src/app/config.ts +++ b/src/app/config.ts @@ -17,7 +17,20 @@ const local_dev = `http://localhost:3000`; const dev_origin = process.env.REACT_APP_OFFICIAL_DEMO ? official_dev : local_dev; // const local_dev = `https://im.ttt.td`; -export const BASE_ORIGIN = process.env.REACT_APP_RELEASE ? `${location.origin}` : dev_origin; +// 在 widget 模式下,使用 widget 脚本所在的 origin +const getBaseOrigin = () => { + if (process.env.REACT_APP_RELEASE) { + // 检查是否有 widget origin(Shadow DOM 模式) + const widgetOrigin = (window as any).__VOCECHAT_WIDGET_ORIGIN__; + if (widgetOrigin) { + return widgetOrigin; + } + return location.origin; + } + return dev_origin; +}; + +export const BASE_ORIGIN = getBaseOrigin(); export const IS_OFFICIAL_DEMO = BASE_ORIGIN === official_dev; const BASE_URL = `${BASE_ORIGIN}/api`; diff --git a/src/i18n.ts b/src/i18n.ts index 1357a60d..b7e9a335 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -66,6 +66,14 @@ i18n returnNull: false, // for backend middleware backend: { + loadPath: (() => { + // 在 Shadow DOM 模式下,使用 widget.js 所在的 origin 来加载翻译文件 + const widgetOrigin = (window as any).__VOCECHAT_WIDGET_ORIGIN__; + if (widgetOrigin) { + return `${widgetOrigin}/locales/{{lng}}/{{ns}}.json`; + } + return '/locales/{{lng}}/{{ns}}.json'; + })(), queryStringParams: { v: pkg.version } } }); diff --git a/src/index-widget.tsx b/src/index-widget.tsx index 9ccbf5b6..8cd1369c 100644 --- a/src/index-widget.tsx +++ b/src/index-widget.tsx @@ -1,5 +1,6 @@ // 在 Shadow DOM 模式下,动态设置 webpack 的 publicPath // 这样 webpack 运行时加载的动态资源(如代码分割的 chunk)会从正确的源服务器加载 +let widgetOrigin = ''; if (typeof document !== 'undefined') { const currentScript = document.currentScript as HTMLScriptElement; if (currentScript && currentScript.src) { @@ -9,6 +10,9 @@ if (typeof document !== 'undefined') { 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); }