feat(chat): 页面级 basePath 引导(路径模式静态面)

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-10-19 18:28:22 +08:00
parent 0baa9ab2ed
commit a1dc1ff7a3
3 changed files with 81 additions and 7 deletions
+32 -5
View File
@@ -4,13 +4,42 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>AI Support</title>
<link rel="stylesheet" href="/static/css/style.css">
<script src="/static/js/theme.js"></script>
<script>
(function () {
/* spec 2026-10-19 §4:路径前缀模式(/t/<slug>/)按当前地址推导基础路径;
Host 模式(根路径 "/")推导为空串——全部引用零变化。
拼接安全:match 命中即字符集白名单([a-z0-9-]),base 只做 URL 属性
赋值,绝不拼 HTML(防线 §2/§3);找不到标记(如 /admin 域)同样得空串。 */
var m = location.pathname.match(/^\/t\/[a-z0-9-]+\/$/);
var base = m ? location.pathname.slice(0, -1) : "";
window.__BASE_PATH__ = base;
function css(href) {
var l = document.createElement("link");
l.rel = "stylesheet"; l.href = base + href;
document.head.appendChild(l);
}
function js(src) {
var s = document.createElement("script");
s.src = base + src; s.async = false; /* 保序:theme 先于 chat */
document.head.appendChild(s);
}
css("/static/css/style.css");
document.addEventListener("DOMContentLoaded", function () {
/* js 装载放 DOMContentLoaded:head 动态插入的 async=false 脚本与
解析进度无形式化顺序保证,且 chat.js 顶层有同步 DOM 查询——
DOM 就绪后装载与原 body 末尾形态等价且保序 */
js("/static/js/theme.js");
js("/static/js/chat.js");
var img = document.querySelector(".avatar-img");
if (img) img.src = base + "/api/public/avatar";
});
})();
</script>
</head>
<body>
<div class="chat-container">
<div class="chat-header">
<div class="avatar"><span class="avatar-emoji">🤖</span><img class="avatar-img" alt="" src="/api/public/avatar"></div>
<div class="avatar"><span class="avatar-emoji">🤖</span><img class="avatar-img" alt=""></div>
<div class="info">
<h1>AI Support</h1>
<div class="availability">Online now - Usually replies in minutes</div>
@@ -76,7 +105,5 @@
</div>
<div class="chat-footer"><span class="footer-left">By chatting, you agree to our <a target="_blank" rel="noopener">privacy policy</a>.</span></div>
</div>
<script src="/static/js/chat.js"></script>
</body>
</html>
+6 -2
View File
@@ -1,8 +1,12 @@
// ---- basePath(spec 2026-10-19 §4):chat.html 引导脚本按 location.pathname ----
// ---- 推导并挂载 window.__BASE_PATH__(路径模式 /t/<slug>,Host 模式空串)。 ----
var B = window.__BASE_PATH__ || "";
// ---- Task 10: brand 拉取(同源 /api/public/brand,下方两个 IIFE 共用) ----
// 失败/超时(3s) → resolve(null) → 中性兜底,页面照常可用不白屏。
var chatBrandReady = (function () {
try {
var p = fetch("/api/public/brand", { cache: "no-store" }).then(function (r) {
var p = fetch(B + "/api/public/brand", { cache: "no-store" }).then(function (r) {
return r.ok ? r.json() : null;
}).catch(function () {
return null;
@@ -129,7 +133,7 @@ var chatBrandReady = (function () {
function getWsUrl() {
var loc = window.location;
var proto = loc.protocol === "https:" ? "wss:" : "ws:";
return proto + "//" + loc.host + "/ws/chat";
return proto + "//" + loc.host + B + "/ws/chat";
}
function connect(sessionKey) {