From f87914e4ea935c07d38c23462b372475092138cf Mon Sep 17 00:00:00 2001 From: jinzhongjia Date: Sun, 18 Oct 2026 18:00:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(pwa):=20=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E9=83=A8=E7=BD=B2=E5=90=8E=E8=87=AA=E5=8A=A8=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=EF=BC=8C=E9=A2=84=E8=A7=88=E7=AB=99=E7=82=B9?= =?UTF-8?q?=E4=B8=8D=E4=BD=BF=E7=94=A8=20Service=20Worker=20=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 站点通过 Workbox 预缓存全部页面并优先从缓存读取。原先使用插件注入的 registerSW.js, 它只注册 Service Worker,新版本在后台激活后页面不会刷新,读者需要再次刷新或跳转才能看到更新。 - 主题中改用 virtual:pwa-register 注册,autoUpdate 模式下新版本激活后自动刷新页面 - 新增 workbox-window 依赖(virtual:pwa-register 需要) - Preview Build 设置 PWA_SELF_DESTROYING,预览站点生成自我注销的 Service Worker --- .github/workflows/preview-build.yml | 3 +++ course/.vitepress/config.mts | 2 ++ course/.vitepress/theme/index.ts | 8 ++++++++ package.json | 3 ++- pnpm-lock.yaml | 3 +++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/preview-build.yml b/.github/workflows/preview-build.yml index 216db370..dd1745f7 100644 --- a/.github/workflows/preview-build.yml +++ b/.github/workflows/preview-build.yml @@ -34,6 +34,9 @@ jobs: - name: Install dependencies run: pnpm install --frozen-lockfile - name: Build with VitePress + env: + # 预览站点不使用 Service Worker 缓存,保证每次访问都拿到最新内容 + PWA_SELF_DESTROYING: "true" run: pnpm run build - name: Save PR metadata env: diff --git a/course/.vitepress/config.mts b/course/.vitepress/config.mts index 091db25d..b18519c9 100644 --- a/course/.vitepress/config.mts +++ b/course/.vitepress/config.mts @@ -31,6 +31,8 @@ export default withPwa( }, pwa: { registerType: "autoUpdate", + // PR 预览构建时生成会自我注销的 Service Worker,避免评审时看到缓存的旧预览 + selfDestroying: process.env.PWA_SELF_DESTROYING === "true", manifest: { name: title, short_name: "Zig 圣经", diff --git a/course/.vitepress/theme/index.ts b/course/.vitepress/theme/index.ts index 5ec8ad42..df099e9a 100644 --- a/course/.vitepress/theme/index.ts +++ b/course/.vitepress/theme/index.ts @@ -1,4 +1,5 @@ // .vitepress/theme/index.ts +/// import type { Theme } from "vitepress"; import DefaultTheme from "vitepress/theme"; @@ -28,6 +29,13 @@ export default { details.removeAttribute("open"); }); }); + + // 注册 Service Worker。插件默认注入的 registerSW.js 只负责注册,新版本激活后页面不会刷新, + // 读者会继续看到预缓存的旧内容;改用 virtual:pwa-register 后,autoUpdate 模式会在新版本激活时自动刷新页面。 + // 引入该模块后,插件也不再注入 registerSW.js。 + import("virtual:pwa-register").then(({ registerSW }) => { + registerSW({ immediate: true }); + }); } }, Layout() { diff --git a/package.json b/package.json index 03d74a57..67c15aa4 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "subset-font": "^2.9.0", "typescript": "^7.0.2", "vite-plugin-pwa": "^1.3.0", - "vitepress": "^1.6.4" + "vitepress": "^1.6.4", + "workbox-window": "^7.4.1" }, "dependencies": { "@giscus/vue": "^3.1.1" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index afa048fd..bca8a8ce 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -60,6 +60,9 @@ importers: vitepress: specifier: ^1.6.4 version: 1.6.4(@algolia/client-search@5.59.0)(@types/node@26.6.4)(less@4.9.1)(postcss@8.5.28)(search-insights@2.17.3)(terser@5.51.2)(typescript@7.0.2) + workbox-window: + specifier: ^7.4.1 + version: 7.4.1 packages: