vite-plugin-refresh-guard 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,12 +1,17 @@
1
- # Changelog
1
+ # 更新日志
2
+
3
+ ## v0.1.1 (2026-08-09)
4
+
5
+ - README 改为中文正文 + 英文版(`README.en.md`),CHANGELOG/package.json 描述同步改中文为主
6
+ - 源码注释保持英文不变
2
7
 
3
8
  ## v0.1.0 (2026-08-09)
4
9
 
5
- Initial release.
10
+ 首次发布。
6
11
 
7
- - Vite plugin: emits `version.json` at build time, bakes `__REFRESH_GUARD_VERSION__` into the bundle, optional `virtual:refresh-guard-changelog` module
8
- - Framework-agnostic `UpdateChecker` core: polling + visibility re-check + stale-chunk guard + reload-loop cooldown
9
- - Three configurable modes: `silent`, `toast-auto` (default), `modal-blocking`
10
- - Vue composable + `UpdatePrompt.vue`
11
- - React hook + `UpdatePrompt` component
12
- - `notifyExternalUpdate()` escape hatch to drive the same mode logic from `vite-plugin-pwa`'s `onNeedRefresh` or any other external signal
12
+ - Vite 插件:构建期产出 `version.json`,把 `__REFRESH_GUARD_VERSION__` 烘焙进 bundle,另有可选的 `virtual:refresh-guard-changelog` 虚拟模块
13
+ - 框架无关的 `UpdateChecker` 核心:轮询 + 标签页重新可见时检测 + 旧 chunk 加载失败兜底 + 刷新防抖冷却
14
+ - 三档可配置强度:`silent`、`toast-auto`(默认)、`modal-blocking`
15
+ - Vue 组合式函数 + `UpdatePrompt.vue`
16
+ - React hook + `UpdatePrompt` 组件
17
+ - `notifyExternalUpdate()` 逃生舱:把 `vite-plugin-pwa` 的 `onNeedRefresh` 或任何其他外部信号接进同一套档位逻辑
package/README.en.md ADDED
@@ -0,0 +1,151 @@
1
+ # vite-plugin-refresh-guard
2
+
3
+ [中文](./README.md) | English
4
+
5
+ [![CI](https://github.com/webkubor/vite-plugin-refresh-guard/actions/workflows/ci.yml/badge.svg)](https://github.com/webkubor/vite-plugin-refresh-guard/actions/workflows/ci.yml)
6
+ [![npm version](https://img.shields.io/npm/v/vite-plugin-refresh-guard.svg)](https://www.npmjs.com/package/vite-plugin-refresh-guard)
7
+ [![license](https://img.shields.io/npm/l/vite-plugin-refresh-guard.svg)](./LICENSE)
8
+ [![vite](https://img.shields.io/badge/vite-%3E%3D4%20%3C9-646CFF?logo=vite&logoColor=white)](https://vitejs.dev)
9
+ [![vue](https://img.shields.io/badge/vue-%3E%3D3-4FC08D?logo=vue.js&logoColor=white)](https://vuejs.org)
10
+ [![react](https://img.shields.io/badge/react-%3E%3D18-61DAFB?logo=react&logoColor=white)](https://react.dev)
11
+
12
+ Configurable "there's a new version, here's what happens next" for Vite apps.
13
+
14
+ Most projects that solve this end up with one of three answers, usually picked by accident and
15
+ never revisited:
16
+
17
+ - **Silent** — just refresh in the background, the user never notices.
18
+ - **Toast, then auto-refresh** — a small non-blocking notice, then refresh after a short delay.
19
+ - **Blocking modal** — stop the user, make them click "update now" before they can continue.
20
+
21
+ This package makes that an explicit, per-project choice instead of copy-pasted glue code that
22
+ drifts every time someone touches it. Framework-agnostic core, thin Vue and React adapters.
23
+
24
+ ## Install
25
+
26
+ ```bash
27
+ npm i -D vite-plugin-refresh-guard
28
+ ```
29
+
30
+ `vue`, `react`/`react-dom`, and `vite` are peer dependencies — install whichever you actually use.
31
+
32
+ ## Quick start
33
+
34
+ **`vite.config.ts`**
35
+
36
+ ```ts
37
+ import { defineConfig } from 'vite'
38
+ import { refreshGuard } from 'vite-plugin-refresh-guard'
39
+
40
+ export default defineConfig({
41
+ plugins: [refreshGuard()],
42
+ })
43
+ ```
44
+
45
+ This does two things at build time:
46
+ 1. Writes `version.json` into your build output (`{ version, buildId, commit, time }`).
47
+ 2. Bakes the current version into your bundle as a global, `__REFRESH_GUARD_VERSION__`.
48
+
49
+ **Vue**
50
+
51
+ ```vue
52
+ <script setup lang="ts">
53
+ import { useVersionCheck } from 'vite-plugin-refresh-guard/vue'
54
+ import UpdatePrompt from 'vite-plugin-refresh-guard/vue/UpdatePrompt.vue'
55
+
56
+ const { hasUpdate, latestInfo, applyUpdate, mode } = useVersionCheck(__REFRESH_GUARD_VERSION__, {
57
+ mode: 'toast-auto', // 'silent' | 'toast-auto' | 'modal-blocking'
58
+ })
59
+ </script>
60
+
61
+ <template>
62
+ <UpdatePrompt :visible="hasUpdate" :mode="mode" :version="latestInfo?.version" @refresh="applyUpdate" />
63
+ </template>
64
+ ```
65
+
66
+ **React**
67
+
68
+ ```tsx
69
+ import { useVersionCheck } from 'vite-plugin-refresh-guard/react'
70
+ import { UpdatePrompt } from 'vite-plugin-refresh-guard/react/UpdatePrompt'
71
+
72
+ function App() {
73
+ const { hasUpdate, latestInfo, applyUpdate, mode } = useVersionCheck(__REFRESH_GUARD_VERSION__, {
74
+ mode: 'modal-blocking',
75
+ })
76
+ return <UpdatePrompt visible={hasUpdate} mode={mode} version={latestInfo?.version} onRefresh={applyUpdate} />
77
+ }
78
+ ```
79
+
80
+ Add `/// <reference types="vite-plugin-refresh-guard/client" />` to your `env.d.ts` (or wherever
81
+ your other `vite/client` reference lives) so TypeScript knows about `__REFRESH_GUARD_VERSION__`.
82
+
83
+ Don't want the prebuilt UI? Use `useVersionCheck`'s `hasUpdate`/`latestInfo`/`applyUpdate` and
84
+ render your own — the components are a starting point, not a requirement.
85
+
86
+ ## Picking a mode
87
+
88
+ | Mode | User sees | When to use it |
89
+ |---|---|---|
90
+ | `silent` | Nothing. Refreshes in the background. | Internal tools, dashboards nobody's mid-task in. Pairs well with `vite-plugin-pwa`'s own `registerType: 'autoUpdate'` doing the same job at the service-worker level — you may not need this package's polling at all in that case, just wire `notifyExternalUpdate()` (see below) if you want changelog display too. |
91
+ | `toast-auto` (default) | A small corner notice, then refresh after `autoRefreshDelay` (default 1.2s). | Most consumer-facing apps. Gives the user a beat to notice, doesn't block them. |
92
+ | `modal-blocking` | A centered, non-dismissible prompt. Refresh only on click. | Apps where refreshing mid-task loses real work (a half-filled form, an in-progress upload), or where you want confirmation before applying (matches what we found in ModelGo's console app). |
93
+
94
+ You're not locked into version.json polling only — `checker.notifyExternalUpdate(info)` (also
95
+ exposed by both adapters) feeds an update signal from anywhere else into the same mode logic,
96
+ most usefully from `vite-plugin-pwa`'s `registerSW({ onNeedRefresh })` callback if you're also
97
+ running a service worker and want its update event to drive the same toast/modal instead of
98
+ (or in addition to) polling.
99
+
100
+ ## Options
101
+
102
+ ```ts
103
+ interface VersionCheckerOptions {
104
+ versionUrl?: string // default '/version.json'
105
+ interval?: number // default 5 minutes; 0 disables polling
106
+ checkOnVisible?: boolean // re-check on tab focus, default true
107
+ mode?: 'silent' | 'toast-auto' | 'modal-blocking' // default 'toast-auto'
108
+ autoRefreshDelay?: number // toast-auto only, default 1200ms
109
+ refreshCooldown?: number // min ms between forced refreshes, default 90s — guards against reload loops
110
+ guardChunkErrors?: boolean // force-refresh once on a stale-chunk load failure, default true
111
+ onUpdateAvailable?: (info) => void | boolean // return false to take over the UI yourself
112
+ onBeforeRefresh?: () => void // flush analytics etc. right before location.reload()
113
+ }
114
+ ```
115
+
116
+ Plugin options (`refreshGuard({ ... })`):
117
+
118
+ ```ts
119
+ interface RefreshGuardPluginOptions {
120
+ version?: string // default: read from package.json
121
+ includeCommit?: boolean // default true; silently omitted outside a git repo
122
+ outFile?: string // default 'version.json'
123
+ changelog?: string | false // default 'CHANGELOG.md'; false disables the virtual module
124
+ }
125
+ ```
126
+
127
+ ## Changelog display (optional, decoupled from the refresh decision)
128
+
129
+ ```ts
130
+ import { latestVersion, latestContent } from 'virtual:refresh-guard-changelog'
131
+ ```
132
+
133
+ Parses `## vX.Y.Z (date)` headings (same format as Keep a Changelog / most auto-generated
134
+ changelogs) out of your `CHANGELOG.md` and exposes the latest section's version and markdown
135
+ body. This is independent of the refresh-trigger logic on purpose — showing "what's new" and
136
+ deciding "when do we reload" are different product decisions, and coupling them (as several
137
+ of the projects this package was extracted from originally did) is how you end up with two
138
+ mechanisms fighting each other.
139
+
140
+ ## Why not just service workers?
141
+
142
+ `vite-plugin-pwa`'s `autoUpdate` mode is a perfectly good `silent` implementation if you're
143
+ already running a service worker — this package doesn't replace that. What it adds is: a
144
+ uniform way to also get a `toast-auto` or `modal-blocking` experience (which a bare service
145
+ worker can't give you a clean hook for without writing this exact plumbing yourself), a
146
+ non-PWA `version.json`-polling fallback for apps that don't want a service worker at all, and
147
+ the changelog-display layer. Use whichever pieces you need; they're independent.
148
+
149
+ ## License
150
+
151
+ MIT
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # vite-plugin-refresh-guard
2
2
 
3
+ 中文 | [English](./README.en.md)
4
+
3
5
  [![CI](https://github.com/webkubor/vite-plugin-refresh-guard/actions/workflows/ci.yml/badge.svg)](https://github.com/webkubor/vite-plugin-refresh-guard/actions/workflows/ci.yml)
4
6
  [![npm version](https://img.shields.io/npm/v/vite-plugin-refresh-guard.svg)](https://www.npmjs.com/package/vite-plugin-refresh-guard)
5
7
  [![license](https://img.shields.io/npm/l/vite-plugin-refresh-guard.svg)](./LICENSE)
@@ -7,27 +9,25 @@
7
9
  [![vue](https://img.shields.io/badge/vue-%3E%3D3-4FC08D?logo=vue.js&logoColor=white)](https://vuejs.org)
8
10
  [![react](https://img.shields.io/badge/react-%3E%3D18-61DAFB?logo=react&logoColor=white)](https://react.dev)
9
11
 
10
- Configurable "there's a new version, here's what happens next" for Vite apps.
12
+ 给 Vite 项目做"部署了新版本,接下来怎么办"的可配置方案。
11
13
 
12
- Most projects that solve this end up with one of three answers, usually picked by accident and
13
- never revisited:
14
+ 大多数项目最后都会落在这三种答案里的一种,而且往往是随手写的、后来再也没人回头审视过:
14
15
 
15
- - **Silent** — just refresh in the background, the user never notices.
16
- - **Toast, then auto-refresh** — a small non-blocking notice, then refresh after a short delay.
17
- - **Blocking modal** — stop the user, make them click "update now" before they can continue.
16
+ - **静默**——后台自动刷新,用户完全无感
17
+ - **提示+自动刷新**——弹个小提示,短暂延迟后自动刷新
18
+ - **强制弹窗**——拦住用户,必须点"立即更新"才能继续用
18
19
 
19
- This package makes that an explicit, per-project choice instead of copy-pasted glue code that
20
- drifts every time someone touches it. Framework-agnostic core, thin Vue and React adapters.
20
+ 这个包把这个选择显式化、可配置化,而不是散落在各项目里、每次改动都会跑偏的胶水代码。核心逻辑跟框架无关,Vue 和 React 各有一层薄适配。
21
21
 
22
- ## Install
22
+ ## 安装
23
23
 
24
24
  ```bash
25
25
  npm i -D vite-plugin-refresh-guard
26
26
  ```
27
27
 
28
- `vue`, `react`/`react-dom`, and `vite` are peer dependencies — install whichever you actually use.
28
+ `vue`、`react`/`react-dom`、`vite` 都是 peerDependency——用哪个装哪个。
29
29
 
30
- ## Quick start
30
+ ## 快速开始
31
31
 
32
32
  **`vite.config.ts`**
33
33
 
@@ -40,9 +40,9 @@ export default defineConfig({
40
40
  })
41
41
  ```
42
42
 
43
- This does two things at build time:
44
- 1. Writes `version.json` into your build output (`{ version, buildId, commit, time }`).
45
- 2. Bakes the current version into your bundle as a global, `__REFRESH_GUARD_VERSION__`.
43
+ 这一步在构建期做两件事:
44
+ 1. 往构建产物里写一个 `version.json`(`{ version, buildId, commit, time }`)
45
+ 2. 把当前版本号烘焙进 bundle,成为全局常量 `__REFRESH_GUARD_VERSION__`
46
46
 
47
47
  **Vue**
48
48
 
@@ -75,74 +75,62 @@ function App() {
75
75
  }
76
76
  ```
77
77
 
78
- Add `/// <reference types="vite-plugin-refresh-guard/client" />` to your `env.d.ts` (or wherever
79
- your other `vite/client` reference lives) so TypeScript knows about `__REFRESH_GUARD_VERSION__`.
78
+ 在 `env.d.ts`(或者你放 `vite/client` reference 的地方)加一行:
79
+ ```ts
80
+ /// <reference types="vite-plugin-refresh-guard/client" />
81
+ ```
82
+ 不然 TypeScript 不认识 `__REFRESH_GUARD_VERSION__`。
80
83
 
81
- Don't want the prebuilt UI? Use `useVersionCheck`'s `hasUpdate`/`latestInfo`/`applyUpdate` and
82
- render your own — the components are a starting point, not a requirement.
84
+ 不想用现成 UI?直接用 `useVersionCheck` 返回的 `hasUpdate`/`latestInfo`/`applyUpdate` 自己渲染——预置组件只是个起点,不是必须用它。
83
85
 
84
- ## Picking a mode
86
+ ## 怎么选档位
85
87
 
86
- | Mode | User sees | When to use it |
88
+ | 档位 | 用户看到什么 | 什么时候用 |
87
89
  |---|---|---|
88
- | `silent` | Nothing. Refreshes in the background. | Internal tools, dashboards nobody's mid-task in. Pairs well with `vite-plugin-pwa`'s own `registerType: 'autoUpdate'` doing the same job at the service-worker level — you may not need this package's polling at all in that case, just wire `notifyExternalUpdate()` (see below) if you want changelog display too. |
89
- | `toast-auto` (default) | A small corner notice, then refresh after `autoRefreshDelay` (default 1.2s). | Most consumer-facing apps. Gives the user a beat to notice, doesn't block them. |
90
- | `modal-blocking` | A centered, non-dismissible prompt. Refresh only on click. | Apps where refreshing mid-task loses real work (a half-filled form, an in-progress upload), or where you want confirmation before applying (matches what we found in ModelGo's console app). |
90
+ | `silent` | 什么都看不到,后台自动刷新 | 内部工具、没人会"做到一半"的看板类页面。如果项目已经在用 `vite-plugin-pwa` 的 `registerType:'autoUpdate'`,同一件事它在 SW 层已经做了,这种情况下你可能根本不需要本包的轮询,只想要 changelog 展示的话接 `notifyExternalUpdate()`(见下文)就够 |
91
+ | `toast-auto`(默认) | 角落小提示,`autoRefreshDelay`(默认1.2秒)后自动刷新 | 大多数 to-C 产品。给用户一个心理准备,但不打断 |
92
+ | `modal-blocking` | 居中弹窗,不可点遮罩关闭,点了才刷新 | 刷新会丢失用户没保存的东西(表单填一半、正在上传),或者想要用户明确确认才应用更新的场景 |
91
93
 
92
- You're not locked into version.json polling only — `checker.notifyExternalUpdate(info)` (also
93
- exposed by both adapters) feeds an update signal from anywhere else into the same mode logic,
94
- most usefully from `vite-plugin-pwa`'s `registerSW({ onNeedRefresh })` callback if you're also
95
- running a service worker and want its update event to drive the same toast/modal instead of
96
- (or in addition to) polling.
94
+ 不是只能靠轮询 version.json——`checker.notifyExternalUpdate(info)`(两个框架适配层都暴露了这个方法)可以把任何来源的更新信号接进同一套档位逻辑,最常见的用法是接 `vite-plugin-pwa` 的 `registerSW({ onNeedRefresh })` 回调,如果你本来就在跑 service worker,想让它的更新事件也走同一套 toast/弹窗,而不是(或者加上)轮询。
97
95
 
98
- ## Options
96
+ ## 配置项
99
97
 
100
98
  ```ts
101
99
  interface VersionCheckerOptions {
102
- versionUrl?: string // default '/version.json'
103
- interval?: number // default 5 minutes; 0 disables polling
104
- checkOnVisible?: boolean // re-check on tab focus, default true
105
- mode?: 'silent' | 'toast-auto' | 'modal-blocking' // default 'toast-auto'
106
- autoRefreshDelay?: number // toast-auto only, default 1200ms
107
- refreshCooldown?: number // min ms between forced refreshes, default 90s — guards against reload loops
108
- guardChunkErrors?: boolean // force-refresh once on a stale-chunk load failure, default true
109
- onUpdateAvailable?: (info) => void | boolean // return false to take over the UI yourself
110
- onBeforeRefresh?: () => void // flush analytics etc. right before location.reload()
100
+ versionUrl?: string // 默认 '/version.json'
101
+ interval?: number // 默认 5 分钟;设 0 关闭轮询
102
+ checkOnVisible?: boolean // 标签页重新可见时也查一次,默认 true
103
+ mode?: 'silent' | 'toast-auto' | 'modal-blocking' // 默认 'toast-auto'
104
+ autoRefreshDelay?: number // 仅 toast-auto 用,默认 1200ms
105
+ refreshCooldown?: number // 两次强制刷新之间的最短间隔,默认 90 秒——防止刷新死循环
106
+ guardChunkErrors?: boolean // 旧 chunk 加载失败时强刷一次,默认 true
107
+ onUpdateAvailable?: (info) => void | boolean // 返回 false 可以自己接管 UI
108
+ onBeforeRefresh?: () => void // 真正 location.reload() 前调用,适合在这里补埋点
111
109
  }
112
110
  ```
113
111
 
114
- Plugin options (`refreshGuard({ ... })`):
112
+ 插件配置(`refreshGuard({ ... })`):
115
113
 
116
114
  ```ts
117
115
  interface RefreshGuardPluginOptions {
118
- version?: string // default: read from package.json
119
- includeCommit?: boolean // default true; silently omitted outside a git repo
120
- outFile?: string // default 'version.json'
121
- changelog?: string | false // default 'CHANGELOG.md'; false disables the virtual module
116
+ version?: string // 默认读 package.json
117
+ includeCommit?: boolean // 默认 true;非 git 仓库时静默省略,不报错
118
+ outFile?: string // 默认 'version.json'
119
+ changelog?: string | false // 默认 'CHANGELOG.md';false 关闭虚拟模块
122
120
  }
123
121
  ```
124
122
 
125
- ## Changelog display (optional, decoupled from the refresh decision)
123
+ ## 更新日志展示(可选,跟"要不要刷新"这个决定解耦)
126
124
 
127
125
  ```ts
128
126
  import { latestVersion, latestContent } from 'virtual:refresh-guard-changelog'
129
127
  ```
130
128
 
131
- Parses `## vX.Y.Z (date)` headings (same format as Keep a Changelog / most auto-generated
132
- changelogs) out of your `CHANGELOG.md` and exposes the latest section's version and markdown
133
- body. This is independent of the refresh-trigger logic on purpose — showing "what's new" and
134
- deciding "when do we reload" are different product decisions, and coupling them (as several
135
- of the projects this package was extracted from originally did) is how you end up with two
136
- mechanisms fighting each other.
137
-
138
- ## Why not just service workers?
139
-
140
- `vite-plugin-pwa`'s `autoUpdate` mode is a perfectly good `silent` implementation if you're
141
- already running a service worker — this package doesn't replace that. What it adds is: a
142
- uniform way to also get a `toast-auto` or `modal-blocking` experience (which a bare service
143
- worker can't give you a clean hook for without writing this exact plumbing yourself), a
144
- non-PWA `version.json`-polling fallback for apps that don't want a service worker at all, and
145
- the changelog-display layer. Use whichever pieces you need; they're independent.
129
+ 从你的 `CHANGELOG.md` 里解析 `## vX.Y.Z (date)` 这种格式的标题(跟 Keep a Changelog 及大多数自动生成的更新日志一致),暴露最新一条的版本号和 markdown 正文。这个功能故意跟"什么时候刷新"的逻辑解耦——"展示更新了什么"和"什么时候该刷新"是两个不同的产品决策,本包最初就是从几个把这两件事强行绑死在一起的项目里抽出来的,绑死的下场就是两套机制互相打架。
130
+
131
+ ## 为什么不干脆只用 service worker
132
+
133
+ 如果你已经在跑 service worker,`vite-plugin-pwa` 的 `autoUpdate` 模式本身就是一个很好的 `silent` 实现——本包不是要替代它。本包补的是:一套统一的方式,能拿到 `toast-auto` 或 `modal-blocking` 的体验(裸 service worker 想要这个得自己手写这整套管线);一个不需要 service worker 的、纯靠轮询 `version.json` 的兜底方案;以及独立的更新日志展示层。哪块需要用哪块,互相不绑定。
146
134
 
147
135
  ## License
148
136
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-refresh-guard",
3
- "version": "0.1.0",
4
- "description": "Configurable new-version detection + refresh for Vite apps: silent auto-refresh, toast-and-auto-refresh, or a blocking modal — pick the strength that fits your product, framework-agnostic core with Vue and React adapters.",
3
+ "version": "0.1.1",
4
+ "description": "Vite 应用的可配置版本更新检测+刷新方案:静默自动刷新 / 提示后自动刷新 / 弹窗阻断,按产品需要选档位,核心逻辑框架无关,附 Vue/React 适配层。Configurable new-version detection + refresh for Vite apps — silent / toast-auto / modal-blocking, framework-agnostic core with Vue and React adapters.",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "repository": {