vite-plugin-build-version-file 0.1.12 → 0.1.13

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
@@ -382,6 +382,30 @@ checkVersion()
382
382
  - 弹窗、通知、静默刷新等策略放在业务项目里
383
383
  - 请求失败后继续下一轮检查,避免一次失败中断流程
384
384
 
385
+ ## 刷新指定项目缓存
386
+
387
+ ```ts
388
+ import { clearUrlCache } from 'vite-plugin-build-version-file/client'
389
+
390
+ await clearUrlCache('/vue-app/')
391
+ ```
392
+
393
+ `url` 可省略;省略时默认使用当前 Vue 项目页面 URL:
394
+
395
+ ```ts
396
+ await clearUrlCache()
397
+ ```
398
+
399
+ Vue hash 路由会原样保留,缓存时间戳写在 `#` 前,确保服务器能够收到:
400
+
401
+ ```text
402
+ https://cus.smarex.com.cn/pc/?_=随机值#/login?redirect=/dashboard/analysis
403
+ ```
404
+
405
+ 该方法仅支持同源 URL。它会绕过缓存请求 URL、强制刷新原 URL、通过隐藏 iframe 再次加载页面,最后携带随机时间戳跳转到该 URL。创建 iframe 前会销毁同一页面中已有的缓存刷新 iframe。单次请求或 iframe 加载失败不会阻断最终跳转。
406
+
407
+ 受浏览器限制,前端代码无法直接删除指定 URL 下的全部 HTTP 缓存;该方法只能尽可能刷新对应 Vue 项目的缓存。
408
+
385
409
  ## 开发态行为
386
410
 
387
411
  开发态下,插件不会向业务项目目录写入文件。
@@ -824,6 +848,30 @@ Recommendations:
824
848
  - Keep prompts, notifications, silent reloads, and retry rules in application code.
825
849
  - Continue polling after failed requests.
826
850
 
851
+ ## Refreshing a Project Cache
852
+
853
+ ```ts
854
+ import { clearUrlCache } from 'vite-plugin-build-version-file/client'
855
+
856
+ await clearUrlCache('/vue-app/')
857
+ ```
858
+
859
+ The `url` argument is optional. When omitted, the current Vue project page URL is used:
860
+
861
+ ```ts
862
+ await clearUrlCache()
863
+ ```
864
+
865
+ Vue hash routes are preserved. The cache-busting timestamp is added before `#` so the server receives it:
866
+
867
+ ```text
868
+ https://cus.smarex.com.cn/pc/?_=random-value#/login?redirect=/dashboard/analysis
869
+ ```
870
+
871
+ This method only supports same-origin URLs. It bypasses the cached URL, forces a refresh of the original URL, loads the page again in a hidden iframe, and finally navigates to the URL with a random timestamp. Any existing cache-refresh iframe created by this method is removed before a new one is created. A failed request or iframe load does not prevent the final navigation.
872
+
873
+ Browser code cannot directly delete every HTTP cache entry under a specific URL. This method refreshes the corresponding Vue application cache on a best-effort basis.
874
+
827
875
  ## Development Behavior
828
876
 
829
877
  In development, the plugin does not write files into your project directory.
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/client.ts
21
+ var client_exports = {};
22
+ __export(client_exports, {
23
+ clearUrlCache: () => clearUrlCache
24
+ });
25
+ module.exports = __toCommonJS(client_exports);
26
+ var CACHE_BUST_PARAM = "_";
27
+ var IFRAME_ID = "vite-plugin-build-version-file-cache-iframe";
28
+ var IFRAME_TIMEOUT = 5e3;
29
+ function createCacheBustedUrl(url) {
30
+ const targetUrl = new URL(url, window.location.href);
31
+ const hash = targetUrl.hash;
32
+ targetUrl.hash = "";
33
+ targetUrl.searchParams.set(
34
+ CACHE_BUST_PARAM,
35
+ `${Date.now()}-${Math.random().toString(36).slice(2)}`
36
+ );
37
+ targetUrl.hash = hash;
38
+ return targetUrl.href;
39
+ }
40
+ function loadIframe(url) {
41
+ return new Promise((resolve) => {
42
+ document.getElementById(IFRAME_ID)?.remove();
43
+ const iframe = document.createElement("iframe");
44
+ let completed = false;
45
+ const complete = () => {
46
+ if (completed) {
47
+ return;
48
+ }
49
+ completed = true;
50
+ window.clearTimeout(timeout);
51
+ resolve();
52
+ };
53
+ const timeout = window.setTimeout(complete, IFRAME_TIMEOUT);
54
+ iframe.hidden = true;
55
+ iframe.id = IFRAME_ID;
56
+ iframe.addEventListener("load", complete, { once: true });
57
+ iframe.addEventListener("error", complete, { once: true });
58
+ iframe.src = url;
59
+ document.documentElement.appendChild(iframe);
60
+ });
61
+ }
62
+ async function clearUrlCache(url = window.location.href) {
63
+ const targetUrl = new URL(url, window.location.href).href;
64
+ await Promise.allSettled([
65
+ fetch(createCacheBustedUrl(targetUrl), { cache: "no-store" }).then(
66
+ (response) => response.arrayBuffer()
67
+ ),
68
+ fetch(targetUrl, { cache: "reload" }).then(
69
+ (response) => response.arrayBuffer()
70
+ )
71
+ ]);
72
+ try {
73
+ await loadIframe(createCacheBustedUrl(targetUrl));
74
+ } finally {
75
+ window.location.replace(createCacheBustedUrl(targetUrl));
76
+ }
77
+ }
78
+ // Annotate the CommonJS export names for ESM import in node:
79
+ 0 && (module.exports = {
80
+ clearUrlCache
81
+ });
@@ -0,0 +1,3 @@
1
+ declare function clearUrlCache(url?: string): Promise<void>;
2
+
3
+ export { clearUrlCache };
@@ -0,0 +1,3 @@
1
+ declare function clearUrlCache(url?: string): Promise<void>;
2
+
3
+ export { clearUrlCache };
@@ -0,0 +1,56 @@
1
+ // src/client.ts
2
+ var CACHE_BUST_PARAM = "_";
3
+ var IFRAME_ID = "vite-plugin-build-version-file-cache-iframe";
4
+ var IFRAME_TIMEOUT = 5e3;
5
+ function createCacheBustedUrl(url) {
6
+ const targetUrl = new URL(url, window.location.href);
7
+ const hash = targetUrl.hash;
8
+ targetUrl.hash = "";
9
+ targetUrl.searchParams.set(
10
+ CACHE_BUST_PARAM,
11
+ `${Date.now()}-${Math.random().toString(36).slice(2)}`
12
+ );
13
+ targetUrl.hash = hash;
14
+ return targetUrl.href;
15
+ }
16
+ function loadIframe(url) {
17
+ return new Promise((resolve) => {
18
+ document.getElementById(IFRAME_ID)?.remove();
19
+ const iframe = document.createElement("iframe");
20
+ let completed = false;
21
+ const complete = () => {
22
+ if (completed) {
23
+ return;
24
+ }
25
+ completed = true;
26
+ window.clearTimeout(timeout);
27
+ resolve();
28
+ };
29
+ const timeout = window.setTimeout(complete, IFRAME_TIMEOUT);
30
+ iframe.hidden = true;
31
+ iframe.id = IFRAME_ID;
32
+ iframe.addEventListener("load", complete, { once: true });
33
+ iframe.addEventListener("error", complete, { once: true });
34
+ iframe.src = url;
35
+ document.documentElement.appendChild(iframe);
36
+ });
37
+ }
38
+ async function clearUrlCache(url = window.location.href) {
39
+ const targetUrl = new URL(url, window.location.href).href;
40
+ await Promise.allSettled([
41
+ fetch(createCacheBustedUrl(targetUrl), { cache: "no-store" }).then(
42
+ (response) => response.arrayBuffer()
43
+ ),
44
+ fetch(targetUrl, { cache: "reload" }).then(
45
+ (response) => response.arrayBuffer()
46
+ )
47
+ ]);
48
+ try {
49
+ await loadIframe(createCacheBustedUrl(targetUrl));
50
+ } finally {
51
+ window.location.replace(createCacheBustedUrl(targetUrl));
52
+ }
53
+ }
54
+ export {
55
+ clearUrlCache
56
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-build-version-file",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "A Vite plugin that emits version.json and injects version metadata into HTML.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -13,6 +13,11 @@
13
13
  "types": "./dist/index.d.ts",
14
14
  "import": "./dist/index.mjs",
15
15
  "require": "./dist/index.cjs"
16
+ },
17
+ "./client": {
18
+ "types": "./dist/client.d.ts",
19
+ "import": "./dist/client.mjs",
20
+ "require": "./dist/client.cjs"
16
21
  }
17
22
  },
18
23
  "files": [