vite-plugin-build-version-file 0.1.16 → 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 +8 -2
- package/dist/client.cjs +44 -6
- package/dist/client.d.cts +1 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.mjs +44 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -389,10 +389,13 @@ import { clearUrlCache } from 'vite-plugin-build-version-file/client'
|
|
|
389
389
|
|
|
390
390
|
await clearUrlCache({
|
|
391
391
|
urls: ['/vue-app/', '/vue-app/index.html'],
|
|
392
|
-
redirectUrl: '/vue-app/'
|
|
392
|
+
redirectUrl: '/vue-app/',
|
|
393
|
+
time: 5
|
|
393
394
|
})
|
|
394
395
|
```
|
|
395
396
|
|
|
397
|
+
调用后会显示全屏 loading 和刷新倒计时。`time` 单位为秒,默认值为 `5`;缓存处理提前完成时会立即刷新,到达时限后则无论调用结果如何都会刷新。
|
|
398
|
+
|
|
396
399
|
参数对象可省略。无参时按顺序检测以下两组 URL:
|
|
397
400
|
|
|
398
401
|
1. `location.origin + location.pathname`
|
|
@@ -863,10 +866,13 @@ import { clearUrlCache } from 'vite-plugin-build-version-file/client'
|
|
|
863
866
|
|
|
864
867
|
await clearUrlCache({
|
|
865
868
|
urls: ['/vue-app/', '/vue-app/index.html'],
|
|
866
|
-
redirectUrl: '/vue-app/'
|
|
869
|
+
redirectUrl: '/vue-app/',
|
|
870
|
+
time: 5
|
|
867
871
|
})
|
|
868
872
|
```
|
|
869
873
|
|
|
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
|
+
|
|
870
876
|
The options object is optional. Without arguments, the following URL groups are checked in order:
|
|
871
877
|
|
|
872
878
|
1. `location.origin + location.pathname`
|
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;
|
|
@@ -83,18 +108,31 @@ async function clearTargetUrlCache(url) {
|
|
|
83
108
|
async function clearUrlCache(options = {}) {
|
|
84
109
|
const defaultUrl = window.location.origin + window.location.pathname;
|
|
85
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;
|
|
86
113
|
const redirectUrl = new URL(
|
|
87
114
|
options.redirectUrl ?? urls[0] ?? defaultUrl,
|
|
88
115
|
window.location.href
|
|
89
116
|
).href;
|
|
117
|
+
const hideLoading = showLoading(time);
|
|
118
|
+
let refreshTimeout = 0;
|
|
90
119
|
try {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
+
]);
|
|
97
133
|
} finally {
|
|
134
|
+
window.clearTimeout(refreshTimeout);
|
|
135
|
+
hideLoading();
|
|
98
136
|
window.location.replace(createCacheBustedUrl(redirectUrl));
|
|
99
137
|
}
|
|
100
138
|
}
|
package/dist/client.d.cts
CHANGED
package/dist/client.d.ts
CHANGED
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;
|
|
@@ -59,18 +84,31 @@ async function clearTargetUrlCache(url) {
|
|
|
59
84
|
async function clearUrlCache(options = {}) {
|
|
60
85
|
const defaultUrl = window.location.origin + window.location.pathname;
|
|
61
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;
|
|
62
89
|
const redirectUrl = new URL(
|
|
63
90
|
options.redirectUrl ?? urls[0] ?? defaultUrl,
|
|
64
91
|
window.location.href
|
|
65
92
|
).href;
|
|
93
|
+
const hideLoading = showLoading(time);
|
|
94
|
+
let refreshTimeout = 0;
|
|
66
95
|
try {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
+
]);
|
|
73
109
|
} finally {
|
|
110
|
+
window.clearTimeout(refreshTimeout);
|
|
111
|
+
hideLoading();
|
|
74
112
|
window.location.replace(createCacheBustedUrl(redirectUrl));
|
|
75
113
|
}
|
|
76
114
|
}
|