Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 所有 /latest 读取一律绕缓存,不再只有阻断屏核对

guyong4cbebfad7f60
diff --git a/apps/mobile/src/__tests__/forcedUpdateGate.test.ts b/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
index 27c148ea31..13eba8e3de 100644
--- a/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
+++ b/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
@@ -67,11 +67,12 @@ describe('强更阻断闸门', () => {
expect(recheck).toContain("return 'error'");
// 仍强更时必须刷新 target:服务端可能只修正了坏掉的安装地址。
expect(recheck).toContain('deps.onStillForced(evaluation.target, startRevision)');
- // 放行决定不能读缓存:/latest 是可变指针,minVersion 会被原地改(set-mobile-min-version),
- // 同 version 的边缘旧副本证明不了新鲜度,所以核对这条请求必须绕缓存。
- const recheckHook = read('src/update/useForcedUpdateRecheck.ts');
- expect(recheckHook).toContain('fetchLatestRelease(');
- expect(recheckHook).toMatch(/isCanary,\n\s*(\/\/[^\n]*\n\s*)*true,/);
+ // /latest 是可变指针,minVersion 还会被原地改(set-mobile-min-version):边缘旧副本
+ // 两个方向都会错判(旧记录带门槛 → 误挡;旧记录无门槛 → 误放行),所以所有 /latest
+ // 读取都必须绕缓存,不是只有核对这一条。
+ const fetchLatest = readCode('src/update/fetchLatestRelease.ts');
+ expect(fetchLatest).toContain("'cache-control': 'no-cache'");
+ expect(fetchLatest).toContain('&t=${Date.now()}');
// 在途期间若有更新的观察写入 store,本次旧结论必须作废(compare-and-set)。
expect(recheck).toContain('const startRevision = deps.getRevision?.();');
expect(recheck).toContain('deps.onCleared(startRevision)');
diff --git a/apps/mobile/src/update/fetchLatestRelease.test.ts b/apps/mobile/src/update/fetchLatestRelease.test.ts
index d25639027f..f02259592b 100644
--- a/apps/mobile/src/update/fetchLatestRelease.test.ts
+++ b/apps/mobile/src/update/fetchLatestRelease.test.ts
@@ -24,7 +24,10 @@ describe('fetchLatestRelease —— 区分"无更新"与"连不上"', () => {
const fetchMock = vi.fn(async () => resp(200, { runtimeVersion: 'rtv1', version: '1.2.0' }));
vi.stubGlobal('fetch', fetchMock);
await expect(fetchLatestRelease('ios', 8000, BASE)).resolves.toEqual({ runtimeVersion: 'rtv1', version: '1.2.0' });
- expect(fetchMock).toHaveBeenCalledWith(`${BASE}/latest?platform=ios`, expect.any(Object));
+ expect(fetchMock).toHaveBeenCalledWith(
+ expect.stringMatching(new RegExp(`^${BASE}/latest\\?platform=ios&t=\\d+$`)),
+ expect.any(Object),
+ );
});
it('canary 显式追加 channel,stable URL 保持旧契约', async () => {
@@ -32,27 +35,22 @@ describe('fetchLatestRelease —— 区分"无更新"与"连不上"', () => {
vi.stubGlobal('fetch', fetchMock);
await fetchLatestRelease('android', 8000, BASE, true);
expect(fetchMock).toHaveBeenCalledWith(
- `${BASE}/latest?platform=android&channel=canary`,
+ expect.stringMatching(
+ new RegExp(`^${BASE}/latest\\?platform=android&channel=canary&t=\\d+$`),
+ ),
expect.any(Object),
);
});
- it('noCache=true → 追加 cache-buster + no-cache 头(放行决定不能读边缘旧记录)', async () => {
- const fetchMock = vi.fn(async () => resp(200, { runtimeVersion: 'rtv1' }));
- vi.stubGlobal('fetch', fetchMock);
- await fetchLatestRelease('ios', 8000, BASE, false, true);
- const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
- expect(url).toMatch(new RegExp(`^${BASE}/latest\\?platform=ios&t=\\d+$`));
- expect(init.headers).toMatchObject({ 'cache-control': 'no-cache' });
- });
-
- it('默认不绕缓存:常规路径的 URL 与请求头保持旧契约', async () => {
+ it('一律绕缓存:每次请求都带 cache-buster + no-cache 头', async () => {
+ // 可变指针 + 原地改 minVersion:边缘旧副本两个方向都会错判(误挡 / 误放行),
+ // 所以四条调用路径统一不吃缓存。
const fetchMock = vi.fn(async () => resp(200, { runtimeVersion: 'rtv1' }));
vi.stubGlobal('fetch', fetchMock);
await fetchLatestRelease('ios', 8000, BASE);
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
- expect(url).toBe(`${BASE}/latest?platform=ios`);
- expect(init.headers).toEqual({ accept: 'application/json' });
+ expect(url).toMatch(new RegExp(`^${BASE}/latest\\?platform=ios&t=\\d+$`));
+ expect(init.headers).toEqual({ accept: 'application/json', 'cache-control': 'no-cache' });
});
it('404(服务端确认暂无记录)→ null(= 无更新)', async () => {
diff --git a/apps/mobile/src/update/fetchLatestRelease.ts b/apps/mobile/src/update/fetchLatestRelease.ts
index a70a35cb52..35811c0856 100644
--- a/apps/mobile/src/update/fetchLatestRelease.ts
+++ b/apps/mobile/src/update/fetchLatestRelease.ts
@@ -10,30 +10,28 @@ const DEFAULT_TIMEOUT_MS = 8000;
* - 网络失败 / 超时 / 5xx 等服务异常 → **抛错**(连不上,调用方需区分于"无更新",
* 否则手动检查会误报"已是最新")。
* baseUrl 可注入,便于单测。
+ *
+ * **一律绕开缓存**(cache-buster + no-cache 头,与发布链侧读同一指针的 fetchJsonPointer
+ * 同口径):`/latest` 背后是可变指针,`minVersion` 还会被原地改(set-mobile-min-version
+ * 读改写同一个 key),边缘旧副本会造成两个方向的错判 —— 旧记录还带门槛时把用户**误挡**在
+ * 强更闸门外,旧记录没门槛时又把该挡的用户**放行**。两者都不可接受,所以四条调用路径
+ * (启动 / resume / 设置页 / 阻断屏核对)统一不吃缓存,代价只是每次多一个小 JSON 请求。
* @param platform 目标平台(默认 ios)
- * @param noCache 绕开缓存(cache-buster + no-cache 头)。`/latest` 背后是**可变指针**,
- * 边缘缓存可能返回旧记录 —— 判定"是否还需要强更"这类**放行**决定不能建立在旧记录上
- * (发布链侧读同一指针的 fetchJsonPointer 一直是这么做的)。默认 false:启动 / resume /
- * 设置页三条常规路径的缓存行为保持不变,它们读到旧记录最多是少提示一次更新。
*/
export async function fetchLatestRelease(
platform = 'ios',
timeoutMs = DEFAULT_TIMEOUT_MS,
baseUrl = OTA_SERVER_BASE_URL,
isCanary = false,
- noCache = false,
): Promise<unknown | null> {
if (!baseUrl) return null; // 非自建变体,无自托管服务
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
const channelQuery = isCanary ? '&channel=canary' : '';
- const cacheQuery = noCache ? `&t=${Date.now()}` : '';
- const res = await fetch(`${baseUrl}/latest?platform=${encodeURIComponent(platform)}${channelQuery}${cacheQuery}`, {
+ const res = await fetch(`${baseUrl}/latest?platform=${encodeURIComponent(platform)}${channelQuery}&t=${Date.now()}`, {
signal: controller.signal,
- headers: noCache
- ? { accept: 'application/json', 'cache-control': 'no-cache' }
- : { accept: 'application/json' },
+ headers: { accept: 'application/json', 'cache-control': 'no-cache' },
});
if (res.status === 404) return null; // 服务端确认暂无记录 = 无更新
if (!res.ok) throw new Error(i18n.t('update.latestRequestFailed', { status: res.status })); // 5xx 等服务异常,不能当成"无更新"
diff --git a/apps/mobile/src/update/useForcedUpdateRecheck.ts b/apps/mobile/src/update/useForcedUpdateRecheck.ts
index 12e9c7fd07..183ffc87f3 100644
--- a/apps/mobile/src/update/useForcedUpdateRecheck.ts
+++ b/apps/mobile/src/update/useForcedUpdateRecheck.ts
@@ -27,9 +27,6 @@ export function useForcedUpdateRecheck(isCanary = isCanaryChannel()): void {
undefined,
undefined,
isCanary,
- // 绕开缓存:同一条记录被原地改过 minVersion 时,"同 version"证明不了新鲜度
- // (set-mobile-min-version 就是原地改)。**放行**用户的决定不能读边缘旧记录。
- true,
),
getCurrentRuntimeVersion: () => Updates.runtimeVersion,
getCurrentVersion: () => Constants.expoConfig?.version ?? null,