fix(theme): appearance 走同源 ctx 前缀,不被跨源跳过
deploy-bschclass-front / build-and-deploy (push) Failing after 3m2s
build / build (push) Successful in 3m24s

主题通道 yh-theme-init.js 的 appearance 有硬性同源限制
(appearanceSameOrigin 不通过直接 return,连请求都不发)。

- host.config.ts:仅当最终 serviceUrl 与当前页面同源时,才回写
  __APP_CONFIG__.serviceHost;dev 的绝对地址(跨源)不回写,保持
  '/api/appearance' 同源相对,交由 vite 代理转发,避免被整段跳过。
- prod:serviceUrl = 同域 ctx(https://base.../userclass),回写后
  appearance 命中 https://base.../userclass/api/appearance。
- vite.config.ts:/api 代理支持 YH_API_PROXY_TARGET 指向真实后端
  (target 带 /userclass 前缀即可,无需 rewrite)。
This commit is contained in:
lijing
2026-10-20 15:16:55 +08:00
parent 542002bb85
commit c2a1f16915
2 changed files with 29 additions and 0 deletions
+26
View File
@@ -69,4 +69,30 @@ try {
/* 无运行时配置时沿用兜底 */
}
/**
* 回写 __APP_CONFIG__.serviceHost(仅同源场景)。
* 主题通道 yh-theme-init.js 的 appearance 请求有硬性同源限制:
* if (!url || !appearanceSameOrigin(url)) return; → 跨源会被直接跳过、连请求都不发。
* 本地 dev:serviceUrl 是 VITE_SERVICE_HOST 的绝对地址(跨源),此时不能回写,
* 保持空串让 init 走同源 "/api/appearance",由 vite 代理转发到后端。
* 生产:serviceUrl = 同域 ctx(https://base.../userclass),同源,回写后 init 请求正确前缀。
* 已有 env-config 下发值时不动(以平台配置为准)。
*/
try {
const w = window as any;
w.__APP_CONFIG__ = w.__APP_CONFIG__ || {};
if (!w.__APP_CONFIG__.serviceHost) {
const candidate = conf.serviceUrl || conf.serviceHost || "";
let sameOrigin = !candidate;
try {
sameOrigin = new URL(candidate, window.location.href).origin === window.location.origin;
} catch {
sameOrigin = false;
}
if (sameOrigin) w.__APP_CONFIG__.serviceHost = candidate;
}
} catch {
/* 极端环境无 window:忽略,保持同源兜底 */
}
export default conf;
+3
View File
@@ -43,6 +43,9 @@ export default defineConfig(({ mode }) => {
open: false,
proxy: {
// 本地后端:serviceUrl 在 dev 下为空 → /api/* 同源相对,由本代理转发
// 想直接连真实后端(含主题通道「同源」的 appearance):
// YH_API_PROXY_TARGET=http://base.yunhaixingtong.com/userclass npm run dev
// 注意 target 要带上下文前缀 /userclass,http-proxy 会把它拼在 /api 前
"/api": {
target: process.env.YH_API_PROXY_TARGET || "http://localhost:8101",
changeOrigin: true,