Skip to content

fix(web/sw): shell 资源用 network-first,硬刷新一次即生效#110

Merged
github-actions[bot] merged 1 commit into
mainfrom
judy/fix-sw-network-first
Jun 29, 2026
Merged

fix(web/sw): shell 资源用 network-first,硬刷新一次即生效#110
github-actions[bot] merged 1 commit into
mainfrom
judy/fix-sw-network-first

Conversation

@SymbolStar

Copy link
Copy Markdown
Owner

问题

Scott 反馈「PR #106/#109 已 merge、服务器 serve 的文件也是新的,但浏览器里还是抖」。

排查发现:web/sw.jsapp.js / style.css / xiaof.* / src/* 这些 shell 资源用的是 stale-while-revalidate——先吐 cache 里的旧版给页面,后台再异步拉新版放进 cache。意味着每次部署后用户第一次刷新看到的还是旧版,要再刷一次(或 clear site data)才能用上新版。

修复

shell 资源改成 network-first

event.respondWith((async () => {
  const cache = await caches.open(CACHE_VERSION);
  try {
    const res = await fetch(req);
    if (res && res.ok && res.type === 'basic') {
      cache.put(req, res.clone()).catch(() => {});
    }
    return res;
  } catch {
    const cached = await cache.match(req);
    if (cached) return cached;
    throw _e;
  }
})());
  • 在线场景:每次都拿最新,零陈旧。
  • 离线场景:网络失败才用 cache 兜底,PWA shell 体验不变。
  • 顺便 CACHE_VERSION 从 v1 bump 到 v2,activate 时清掉旧 cache。

验证

  • node --check web/sw.js 通过。
  • 部署后用户硬刷新一次(Cmd+Shift+R)即可看到新 app.js / style.css,不需要 clear site data,也不需要刷两次。
  • 离线打开 OpenForge 仍然能加载 shell(之前的行为)。

之前 sw.js 对 app.js / style.css / xiaof.* / src/* 这些 shell 资源用的
是 stale-while-revalidate:先吐 cache 里的旧版,后台再拉新版放进 cache,
意味着每次部署后用户第一次刷新看到的还是旧版,要再刷一次才能用上新版。
Scott 之前反馈「修了还是抖」就是因为浏览器 SW 还在用旧 app.js。

现在改成 network-first:每次请求都先走网络,只有网络真的失败(离线/
DNS 等)才 fallback 到 cache。视觉上跟之前一样在线快,离线也一样能
用 shell;但部署新版后硬刷新(Cmd+Shift+R)一次就生效,不需要 clear
site data,也不需要刷两次。

顺便把 CACHE_VERSION 从 v1 bump 到 v2,强制清掉旧 cache。
@github-actions

Copy link
Copy Markdown

🤖 bot-review (comment-only · phase 1)

Diff: 2 files changed, 26 insertions(+), 15 deletions(-) @ bec0e27

Red-line checks:

  • ✅ A-7.5: no new 'forbidden' code in xiaof

Phase 2: auto-approve + auto-merge fire only when red-lines are clean, author is internal, and no needs-human path is touched. Block with no-auto-merge label or [no-auto-merge] in title.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Auto-approved by bot-review: red-line checks clean, internal author, no needs-human paths.

@github-actions github-actions Bot enabled auto-merge (squash) June 29, 2026 02:56
@github-actions github-actions Bot merged commit b76ec16 into main Jun 29, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant