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

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,18 @@ 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
+ })
391
394
  ```
392
395
 
393
- `url` 可省略;省略时默认使用 `location.origin + location.pathname`,不包含当前 query/hash:
396
+ 参数对象可省略。无参时按顺序检测以下两组 URL:
397
+
398
+ 1. `location.origin + location.pathname`
399
+ 2. 第一组 URL 拼接 `index.html`
400
+
401
+ 两组完成后,默认携带随机时间戳跳转到第一组 URL:
394
402
 
395
403
  ```ts
396
404
  await clearUrlCache()
@@ -853,10 +861,18 @@ Recommendations:
853
861
  ```ts
854
862
  import { clearUrlCache } from 'vite-plugin-build-version-file/client'
855
863
 
856
- await clearUrlCache('/vue-app/')
864
+ await clearUrlCache({
865
+ urls: ['/vue-app/', '/vue-app/index.html'],
866
+ redirectUrl: '/vue-app/'
867
+ })
857
868
  ```
858
869
 
859
- The `url` argument is optional. When omitted, `location.origin + location.pathname` is used without the current query/hash:
870
+ The options object is optional. Without arguments, the following URL groups are checked in order:
871
+
872
+ 1. `location.origin + location.pathname`
873
+ 2. The first URL with `index.html` appended
874
+
875
+ After both groups complete, the method navigates to the first URL with a random timestamp:
860
876
 
861
877
  ```ts
862
878
  await clearUrlCache()
package/dist/client.cjs CHANGED
@@ -37,6 +37,14 @@ function createCacheBustedUrl(url) {
37
37
  targetUrl.hash = hash;
38
38
  return targetUrl.href;
39
39
  }
40
+ function createIndexUrl(url) {
41
+ const targetUrl = new URL(url);
42
+ if (!targetUrl.pathname.endsWith("/")) {
43
+ targetUrl.pathname += "/";
44
+ }
45
+ targetUrl.pathname += "index.html";
46
+ return targetUrl.href;
47
+ }
40
48
  function loadIframe(url) {
41
49
  return new Promise((resolve) => {
42
50
  document.getElementById(IFRAME_ID)?.remove();
@@ -59,7 +67,7 @@ function loadIframe(url) {
59
67
  document.documentElement.appendChild(iframe);
60
68
  });
61
69
  }
62
- async function clearUrlCache(url = window.location.origin + window.location.pathname) {
70
+ async function clearTargetUrlCache(url) {
63
71
  const targetUrl = new URL(url, window.location.href).href;
64
72
  console.info(`[vite-plugin-build-version] Clear cache for ${targetUrl}`);
65
73
  await Promise.allSettled([
@@ -70,10 +78,24 @@ async function clearUrlCache(url = window.location.origin + window.location.path
70
78
  (response) => response.arrayBuffer()
71
79
  )
72
80
  ]);
81
+ await loadIframe(createCacheBustedUrl(targetUrl));
82
+ }
83
+ async function clearUrlCache(options = {}) {
84
+ const defaultUrl = window.location.origin + window.location.pathname;
85
+ const urls = options.urls ?? [defaultUrl, createIndexUrl(defaultUrl)];
86
+ const redirectUrl = new URL(
87
+ options.redirectUrl ?? urls[0] ?? defaultUrl,
88
+ window.location.href
89
+ ).href;
73
90
  try {
74
- await loadIframe(createCacheBustedUrl(targetUrl));
91
+ for (const url of urls) {
92
+ try {
93
+ await clearTargetUrlCache(url);
94
+ } catch {
95
+ }
96
+ }
75
97
  } finally {
76
- window.location.replace(createCacheBustedUrl(targetUrl));
98
+ window.location.replace(createCacheBustedUrl(redirectUrl));
77
99
  }
78
100
  }
79
101
  // Annotate the CommonJS export names for ESM import in node:
package/dist/client.d.cts CHANGED
@@ -1,3 +1,7 @@
1
- declare function clearUrlCache(url?: string): Promise<void>;
1
+ interface ClearUrlCacheOptions {
2
+ urls?: string[];
3
+ redirectUrl?: string;
4
+ }
5
+ declare function clearUrlCache(options?: ClearUrlCacheOptions): Promise<void>;
2
6
 
3
- export { clearUrlCache };
7
+ export { type ClearUrlCacheOptions, clearUrlCache };
package/dist/client.d.ts CHANGED
@@ -1,3 +1,7 @@
1
- declare function clearUrlCache(url?: string): Promise<void>;
1
+ interface ClearUrlCacheOptions {
2
+ urls?: string[];
3
+ redirectUrl?: string;
4
+ }
5
+ declare function clearUrlCache(options?: ClearUrlCacheOptions): Promise<void>;
2
6
 
3
- export { clearUrlCache };
7
+ export { type ClearUrlCacheOptions, clearUrlCache };
package/dist/client.mjs CHANGED
@@ -13,6 +13,14 @@ function createCacheBustedUrl(url) {
13
13
  targetUrl.hash = hash;
14
14
  return targetUrl.href;
15
15
  }
16
+ function createIndexUrl(url) {
17
+ const targetUrl = new URL(url);
18
+ if (!targetUrl.pathname.endsWith("/")) {
19
+ targetUrl.pathname += "/";
20
+ }
21
+ targetUrl.pathname += "index.html";
22
+ return targetUrl.href;
23
+ }
16
24
  function loadIframe(url) {
17
25
  return new Promise((resolve) => {
18
26
  document.getElementById(IFRAME_ID)?.remove();
@@ -35,7 +43,7 @@ function loadIframe(url) {
35
43
  document.documentElement.appendChild(iframe);
36
44
  });
37
45
  }
38
- async function clearUrlCache(url = window.location.origin + window.location.pathname) {
46
+ async function clearTargetUrlCache(url) {
39
47
  const targetUrl = new URL(url, window.location.href).href;
40
48
  console.info(`[vite-plugin-build-version] Clear cache for ${targetUrl}`);
41
49
  await Promise.allSettled([
@@ -46,10 +54,24 @@ async function clearUrlCache(url = window.location.origin + window.location.path
46
54
  (response) => response.arrayBuffer()
47
55
  )
48
56
  ]);
57
+ await loadIframe(createCacheBustedUrl(targetUrl));
58
+ }
59
+ async function clearUrlCache(options = {}) {
60
+ const defaultUrl = window.location.origin + window.location.pathname;
61
+ const urls = options.urls ?? [defaultUrl, createIndexUrl(defaultUrl)];
62
+ const redirectUrl = new URL(
63
+ options.redirectUrl ?? urls[0] ?? defaultUrl,
64
+ window.location.href
65
+ ).href;
49
66
  try {
50
- await loadIframe(createCacheBustedUrl(targetUrl));
67
+ for (const url of urls) {
68
+ try {
69
+ await clearTargetUrlCache(url);
70
+ } catch {
71
+ }
72
+ }
51
73
  } finally {
52
- window.location.replace(createCacheBustedUrl(targetUrl));
74
+ window.location.replace(createCacheBustedUrl(redirectUrl));
53
75
  }
54
76
  }
55
77
  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.16",
4
4
  "description": "A Vite plugin that emits version.json and injects version metadata into HTML.",
5
5
  "license": "MIT",
6
6
  "type": "module",