fix(update): 更新检查改走宿主中转,修复 Gitea 无 CORS 导致请求被拦 (v0.5.3-local.8)

0.5.3-local.6 把更新检查从上游 GitHub 改到自建 Gitea API,但仍不通:
页面来源是 http://127.0.0.1:<随机端口>,请求 https://gitea.ququclean.com/api/...
属跨源,而 Gitea 不返回 Access-Control-Allow-Origin,浏览器直接拦截 fetch。
表现为「立即检查」无反应、长期显示「上次检查未成功」。

改为宿主中转:
- src/index.ts 新增 /api/worktable/update-check 路由,服务端(Node 无 CORS
  约束)代取 Gitea releases/latest,客户端只请求同源路径
- updateCheck.ts / index.tsx 的 fetch 改指向该同源路由
- 该路由沿既有约定需要鉴权(request-auth.test.mjs 要求除 health/preview-static
  外所有路由都必须拒绝未鉴权请求),未传 publicAsset

测试:sidebar-popup.test.mjs 的 releaseUrl 改为 /api/worktable/update-check。

验证:npm run pack 全绿,双资产同源 sha256 225e8568…。
This commit is contained in:
wangheng
2026-12-02 17:30:41 +08:00
parent 6b8349073d
commit 1b053e5577
11 changed files with 96 additions and 28 deletions
+4 -3
View File
@@ -3,8 +3,9 @@ declare const __WT_VERSION__: string
export const LOCAL_VERSION = typeof __WT_VERSION__ === 'undefined' ? 'dev' : __WT_VERSION__
// 本仓是内部补丁分发仓,更新源必须是自己的 Gitea,而不是上游 GitHub:
// 上游只发布官方版(无本地补丁),查它既看不到本仓版本,国内网络也常失败。
const UPDATE_HOST = 'https://gitea.ququclean.com'
const UPDATE_REPO = 'xiaoqu/dsh-worktable'
// 走宿主中转(/api/worktable/update-check):页面来源是随机端口的 127.0.0.1,
// 直连 Gitea 属跨源且 Gitea 无 CORS 头,浏览器会拦截请求。
const UPDATE_API = '/api/worktable/update-check'
export type UpdateStatus = 'idle' | 'checking' | 'uptodate' | 'failed'
export type UpdateInfo = { latest: string; notes: string; url: string }
@@ -95,7 +96,7 @@ export async function checkUpdate(force = false): Promise<{ status: UpdateStatus
const ctrl = new AbortController()
const timer = setTimeout(() => ctrl.abort(), 8000)
try {
const r = await fetch(UPDATE_HOST + '/api/v1/repos/' + UPDATE_REPO + '/releases/latest', { headers: { Accept: 'application/json' }, cache: 'no-store', signal: ctrl.signal })
const r = await fetch(UPDATE_API, { headers: { Accept: 'application/json' }, cache: 'no-store', signal: ctrl.signal })
if (r.ok) d = (await r.json()) as { tag_name?: string; body?: string; html_url?: string }
else if (r.status === 403 || r.status === 404) break
} catch { /* 网络抖动/超时:下一轮重试 */ }