vite-plugin-build-version-file 0.1.15 → 0.1.17

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/README.md CHANGED
@@ -387,10 +387,21 @@ checkVersion()
387
387
  ```ts
388
388
  import { clearUrlCache } from 'vite-plugin-build-version-file/client'
389
389
 
390
- await clearUrlCache('/vue-app/')
390
+ await clearUrlCache({
391
+ urls: ['/vue-app/', '/vue-app/index.html'],
392
+ redirectUrl: '/vue-app/',
393
+ time: 5
394
+ })
391
395
  ```
392
396
 
393
- `url` 可省略;省略时默认使用 `location.origin + location.pathname`,不包含当前 query/hash:
397
+ 调用后会显示全屏 loading 和刷新倒计时。`time` 单位为秒,默认值为 `5`;缓存处理提前完成时会立即刷新,到达时限后则无论调用结果如何都会刷新。
398
+
399
+ 参数对象可省略。无参时按顺序检测以下两组 URL:
400
+
401
+ 1. `location.origin + location.pathname`
402
+ 2. 第一组 URL 拼接 `index.html`
403
+
404
+ 两组完成后,默认携带随机时间戳跳转到第一组 URL:
394
405
 
395
406
  ```ts
396
407
  await clearUrlCache()
@@ -853,10 +864,21 @@ Recommendations:
853
864
  ```ts
854
865
  import { clearUrlCache } from 'vite-plugin-build-version-file/client'
855
866
 
856
- await clearUrlCache('/vue-app/')
867
+ await clearUrlCache({
868
+ urls: ['/vue-app/', '/vue-app/index.html'],
869
+ redirectUrl: '/vue-app/',
870
+ time: 5
871
+ })
857
872
  ```
858
873
 
859
- The `url` argument is optional. When omitted, `location.origin + location.pathname` is used without the current query/hash:
874
+ The method displays a full-screen loading mask and refresh countdown after it is called. `time` is measured in seconds and defaults to `5`. It refreshes immediately if cache processing finishes early; once the limit is reached, it refreshes regardless of the processing result.
875
+
876
+ The options object is optional. Without arguments, the following URL groups are checked in order:
877
+
878
+ 1. `location.origin + location.pathname`
879
+ 2. The first URL with `index.html` appended
880
+
881
+ After both groups complete, the method navigates to the first URL with a random timestamp:
860
882
 
861
883
  ```ts
862
884
  await clearUrlCache()
package/dist/client.cjs CHANGED
@@ -25,7 +25,32 @@ __export(client_exports, {
25
25
  module.exports = __toCommonJS(client_exports);
26
26
  var CACHE_BUST_PARAM = "_";
27
27
  var IFRAME_ID = "vite-plugin-build-version-file-cache-iframe";
28
+ var LOADING_ID = "vite-plugin-build-version-file-loading";
28
29
  var IFRAME_TIMEOUT = 5e3;
30
+ var DEFAULT_REFRESH_TIME = 5;
31
+ function showLoading(time) {
32
+ document.getElementById(LOADING_ID)?.remove();
33
+ const loading = document.createElement("div");
34
+ const spinner = document.createElement("div");
35
+ const text = document.createElement("div");
36
+ const deadline = Date.now() + time * 1e3;
37
+ loading.id = LOADING_ID;
38
+ loading.style.cssText = "position:fixed;inset:0;z-index:2147483647;display:flex;flex-direction:column;align-items:center;justify-content:center;background:rgba(0,0,0,.65);color:#fff;font:14px/1.5 sans-serif;";
39
+ spinner.style.cssText = "width:36px;height:36px;margin-bottom:16px;border:3px solid rgba(255,255,255,.35);border-top-color:#fff;border-radius:50%;animation:vite-build-version-loading .8s linear infinite;";
40
+ text.textContent = `\u8BF7\u7A0D\u7B49\uFF0C\u6B63\u5728\u68C0\u6D4B\u66F4\u65B0...\uFF08${Math.ceil(time)}\u79D2\uFF09`;
41
+ const style = document.createElement("style");
42
+ style.textContent = "@keyframes vite-build-version-loading{to{transform:rotate(360deg)}}";
43
+ loading.append(style, spinner, text);
44
+ document.documentElement.appendChild(loading);
45
+ const countdown = window.setInterval(() => {
46
+ const seconds = Math.max(0, Math.ceil((deadline - Date.now()) / 1e3));
47
+ text.textContent = `\u8BF7\u7A0D\u7B49\uFF0C\u6B63\u5728\u68C0\u6D4B\u66F4\u65B0...\uFF08${seconds}\u79D2\uFF09`;
48
+ }, 1e3);
49
+ return () => {
50
+ window.clearInterval(countdown);
51
+ loading.remove();
52
+ };
53
+ }
29
54
  function createCacheBustedUrl(url) {
30
55
  const targetUrl = new URL(url, window.location.href);
31
56
  const hash = targetUrl.hash;
@@ -37,6 +62,14 @@ function createCacheBustedUrl(url) {
37
62
  targetUrl.hash = hash;
38
63
  return targetUrl.href;
39
64
  }
65
+ function createIndexUrl(url) {
66
+ const targetUrl = new URL(url);
67
+ if (!targetUrl.pathname.endsWith("/")) {
68
+ targetUrl.pathname += "/";
69
+ }
70
+ targetUrl.pathname += "index.html";
71
+ return targetUrl.href;
72
+ }
40
73
  function loadIframe(url) {
41
74
  return new Promise((resolve) => {
42
75
  document.getElementById(IFRAME_ID)?.remove();
@@ -59,7 +92,7 @@ function loadIframe(url) {
59
92
  document.documentElement.appendChild(iframe);
60
93
  });
61
94
  }
62
- async function clearUrlCache(url = window.location.origin + window.location.pathname) {
95
+ async function clearTargetUrlCache(url) {
63
96
  const targetUrl = new URL(url, window.location.href).href;
64
97
  console.info(`[vite-plugin-build-version] Clear cache for ${targetUrl}`);
65
98
  await Promise.allSettled([
@@ -70,10 +103,37 @@ async function clearUrlCache(url = window.location.origin + window.location.path
70
103
  (response) => response.arrayBuffer()
71
104
  )
72
105
  ]);
106
+ await loadIframe(createCacheBustedUrl(targetUrl));
107
+ }
108
+ async function clearUrlCache(options = {}) {
109
+ const defaultUrl = window.location.origin + window.location.pathname;
110
+ const urls = options.urls ?? [defaultUrl, createIndexUrl(defaultUrl)];
111
+ const configuredTime = options.time;
112
+ const time = typeof configuredTime === "number" && Number.isFinite(configuredTime) && configuredTime > 0 ? configuredTime : DEFAULT_REFRESH_TIME;
113
+ const redirectUrl = new URL(
114
+ options.redirectUrl ?? urls[0] ?? defaultUrl,
115
+ window.location.href
116
+ ).href;
117
+ const hideLoading = showLoading(time);
118
+ let refreshTimeout = 0;
73
119
  try {
74
- await loadIframe(createCacheBustedUrl(targetUrl));
120
+ await Promise.race([
121
+ (async () => {
122
+ for (const url of urls) {
123
+ try {
124
+ await clearTargetUrlCache(url);
125
+ } catch {
126
+ }
127
+ }
128
+ })(),
129
+ new Promise((resolve) => {
130
+ refreshTimeout = window.setTimeout(resolve, time * 1e3);
131
+ })
132
+ ]);
75
133
  } finally {
76
- window.location.replace(createCacheBustedUrl(targetUrl));
134
+ window.clearTimeout(refreshTimeout);
135
+ hideLoading();
136
+ window.location.replace(createCacheBustedUrl(redirectUrl));
77
137
  }
78
138
  }
79
139
  // Annotate the CommonJS export names for ESM import in node:
package/dist/client.d.cts CHANGED
@@ -1,3 +1,8 @@
1
- declare function clearUrlCache(url?: string): Promise<void>;
1
+ interface ClearUrlCacheOptions {
2
+ urls?: string[];
3
+ redirectUrl?: string;
4
+ time?: number;
5
+ }
6
+ declare function clearUrlCache(options?: ClearUrlCacheOptions): Promise<void>;
2
7
 
3
- export { clearUrlCache };
8
+ export { type ClearUrlCacheOptions, clearUrlCache };
package/dist/client.d.ts CHANGED
@@ -1,3 +1,8 @@
1
- declare function clearUrlCache(url?: string): Promise<void>;
1
+ interface ClearUrlCacheOptions {
2
+ urls?: string[];
3
+ redirectUrl?: string;
4
+ time?: number;
5
+ }
6
+ declare function clearUrlCache(options?: ClearUrlCacheOptions): Promise<void>;
2
7
 
3
- export { clearUrlCache };
8
+ export { type ClearUrlCacheOptions, clearUrlCache };
package/dist/client.mjs CHANGED
@@ -1,7 +1,32 @@
1
1
  // src/client.ts
2
2
  var CACHE_BUST_PARAM = "_";
3
3
  var IFRAME_ID = "vite-plugin-build-version-file-cache-iframe";
4
+ var LOADING_ID = "vite-plugin-build-version-file-loading";
4
5
  var IFRAME_TIMEOUT = 5e3;
6
+ var DEFAULT_REFRESH_TIME = 5;
7
+ function showLoading(time) {
8
+ document.getElementById(LOADING_ID)?.remove();
9
+ const loading = document.createElement("div");
10
+ const spinner = document.createElement("div");
11
+ const text = document.createElement("div");
12
+ const deadline = Date.now() + time * 1e3;
13
+ loading.id = LOADING_ID;
14
+ loading.style.cssText = "position:fixed;inset:0;z-index:2147483647;display:flex;flex-direction:column;align-items:center;justify-content:center;background:rgba(0,0,0,.65);color:#fff;font:14px/1.5 sans-serif;";
15
+ spinner.style.cssText = "width:36px;height:36px;margin-bottom:16px;border:3px solid rgba(255,255,255,.35);border-top-color:#fff;border-radius:50%;animation:vite-build-version-loading .8s linear infinite;";
16
+ text.textContent = `\u8BF7\u7A0D\u7B49\uFF0C\u6B63\u5728\u68C0\u6D4B\u66F4\u65B0...\uFF08${Math.ceil(time)}\u79D2\uFF09`;
17
+ const style = document.createElement("style");
18
+ style.textContent = "@keyframes vite-build-version-loading{to{transform:rotate(360deg)}}";
19
+ loading.append(style, spinner, text);
20
+ document.documentElement.appendChild(loading);
21
+ const countdown = window.setInterval(() => {
22
+ const seconds = Math.max(0, Math.ceil((deadline - Date.now()) / 1e3));
23
+ text.textContent = `\u8BF7\u7A0D\u7B49\uFF0C\u6B63\u5728\u68C0\u6D4B\u66F4\u65B0...\uFF08${seconds}\u79D2\uFF09`;
24
+ }, 1e3);
25
+ return () => {
26
+ window.clearInterval(countdown);
27
+ loading.remove();
28
+ };
29
+ }
5
30
  function createCacheBustedUrl(url) {
6
31
  const targetUrl = new URL(url, window.location.href);
7
32
  const hash = targetUrl.hash;
@@ -13,6 +38,14 @@ function createCacheBustedUrl(url) {
13
38
  targetUrl.hash = hash;
14
39
  return targetUrl.href;
15
40
  }
41
+ function createIndexUrl(url) {
42
+ const targetUrl = new URL(url);
43
+ if (!targetUrl.pathname.endsWith("/")) {
44
+ targetUrl.pathname += "/";
45
+ }
46
+ targetUrl.pathname += "index.html";
47
+ return targetUrl.href;
48
+ }
16
49
  function loadIframe(url) {
17
50
  return new Promise((resolve) => {
18
51
  document.getElementById(IFRAME_ID)?.remove();
@@ -35,7 +68,7 @@ function loadIframe(url) {
35
68
  document.documentElement.appendChild(iframe);
36
69
  });
37
70
  }
38
- async function clearUrlCache(url = window.location.origin + window.location.pathname) {
71
+ async function clearTargetUrlCache(url) {
39
72
  const targetUrl = new URL(url, window.location.href).href;
40
73
  console.info(`[vite-plugin-build-version] Clear cache for ${targetUrl}`);
41
74
  await Promise.allSettled([
@@ -46,10 +79,37 @@ async function clearUrlCache(url = window.location.origin + window.location.path
46
79
  (response) => response.arrayBuffer()
47
80
  )
48
81
  ]);
82
+ await loadIframe(createCacheBustedUrl(targetUrl));
83
+ }
84
+ async function clearUrlCache(options = {}) {
85
+ const defaultUrl = window.location.origin + window.location.pathname;
86
+ const urls = options.urls ?? [defaultUrl, createIndexUrl(defaultUrl)];
87
+ const configuredTime = options.time;
88
+ const time = typeof configuredTime === "number" && Number.isFinite(configuredTime) && configuredTime > 0 ? configuredTime : DEFAULT_REFRESH_TIME;
89
+ const redirectUrl = new URL(
90
+ options.redirectUrl ?? urls[0] ?? defaultUrl,
91
+ window.location.href
92
+ ).href;
93
+ const hideLoading = showLoading(time);
94
+ let refreshTimeout = 0;
49
95
  try {
50
- await loadIframe(createCacheBustedUrl(targetUrl));
96
+ await Promise.race([
97
+ (async () => {
98
+ for (const url of urls) {
99
+ try {
100
+ await clearTargetUrlCache(url);
101
+ } catch {
102
+ }
103
+ }
104
+ })(),
105
+ new Promise((resolve) => {
106
+ refreshTimeout = window.setTimeout(resolve, time * 1e3);
107
+ })
108
+ ]);
51
109
  } finally {
52
- window.location.replace(createCacheBustedUrl(targetUrl));
110
+ window.clearTimeout(refreshTimeout);
111
+ hideLoading();
112
+ window.location.replace(createCacheBustedUrl(redirectUrl));
53
113
  }
54
114
  }
55
115
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-build-version-file",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "A Vite plugin that emits version.json and injects version metadata into HTML.",
5
5
  "license": "MIT",
6
6
  "type": "module",