feat: SW 更新后自动 reload + 隐私友好的 Cloudflare Web Analytics#98
Merged
Conversation
Two independent changes to the standalone app entry: 1. SW freshness — after a deploy the new service worker skipWaiting()s and claims clients but never reloads the already-rendered page, so users keep seeing the previously-cached build until a manual refresh (the "refresh once more and it's fixed" symptom, which is what surfaced the stale PPTX font bug). index.ts now reloads once on `controllerchange`, guarded so it fires only on a real update (not first install), only once, and never while a document is open (a reload would discard unsaved edits). 2. Analytics — add cookieless Cloudflare Web Analytics (lib/analytics.ts) instead of Google Analytics, to stay consistent with the local-only privacy pitch (no cookies, no consent banner). The beacon loads only when a token (VITE_CF_BEACON_TOKEN) is set AND the app is top-level (never in embed iframe). With no token the whole beacon path is tree-shaken out, so the default build ships tracking-free. pages-build-site.yml injects the token from a repo variable. Verified end-to-end with chrome-devtools: first-install no reload, update reload once, doc-open no reload; beacon injects top-level, absent in embed, absent (tree-shaken) with no token. lint:ts + sw-routing tests + build pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
chaxus
added a commit
that referenced
this pull request
Jul 5, 2026
Includes two docs carried in from #98 (privacy-analytics, sw-stale-build) that were never formatted.
chaxus
added a commit
that referenced
this pull request
Jul 5, 2026
Includes two docs carried in from #98 (privacy-analytics, sw-stale-build) that were never formatted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
两个独立的小问题,都与"部署后用户拿到的是否是最新版本 + 隐私友好的访问统计"有关。
改动
1. SW 更新后自动 reload(修「要再手动刷一次才生效」)
每次部署
bin/build.sh会用新的CACHE_VERSION重建sw.js,新 SWskipWaiting()+ 接管客户端触发controllerchange。此前当前页仍渲染旧缓存构建,直到用户手动刷新。新逻辑在
controllerchange时 reload 一次,三重保护:reloadingForUpdate兜底getDocmentObj().fileName非空则跳过,避免丢未保存编辑;落地页 fileName 为空,reload 无感2. 隐私友好的 Cloudflare Web Analytics(新增
lib/analytics.ts)刻意不用 Google Analytics——本项目卖点就是本地、无服务器、隐私。Cloudflare beacon 无 cookie、无需同意横幅(GDPR 友好)。仅在同时满足两条时加载:
VITE_CF_BEACON_TOKEN(未设 → 完全 tree-shake,fork / 本地开发零外部请求、无追踪)token 是公开的客户端值,构建时从 env 注入安全。CI 通过
vars.VITE_CF_BEACON_TOKEN传入。影响面
index.tslib/analytics.ts(新)vite-env.d.ts(新)pages-build-site.yml;含两篇docs/explorations/记录测试
pnpm run lint:ts/format:check🤖 Generated with Claude Code