version-check-js 1.1.1 → 1.1.2

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.en.md CHANGED
@@ -99,6 +99,7 @@ versionCheck.check().then(hasUpdate => {
99
99
  | `onError` | `Function` | `(err) => console.error('Version check failed:', err)` | Error callback function, receives error object as parameter |
100
100
  | `onLog` | `Function` | `null` | Operation log callback function, used to record normal operation information |
101
101
  | `storage` | `Object` | `null` | Custom storage configuration (requires `get`, `set`, `remove` methods), defaults to localStorage |
102
+ | `t` | `string` | `t` | The timestamp parameter name when reloading |
102
103
 
103
104
  ### Configuration Best Practices
104
105
 
@@ -232,7 +233,7 @@ axios.interceptors.request.use(
232
233
  },
233
234
  error => {
234
235
  return Promise.reject(error);
235
- }
236
+ },
236
237
  );
237
238
 
238
239
  versionCheck.start();
@@ -268,7 +269,7 @@ axios.interceptors.request.use(
268
269
  },
269
270
  error => {
270
271
  return Promise.reject(error);
271
- }
272
+ },
272
273
  );
273
274
 
274
275
  versionCheck.start();
package/README.md CHANGED
@@ -101,6 +101,7 @@ versionCheck.check().then(hasUpdate => {
101
101
  | `onError` | `Function` | `(err) => console.error('版本检测失败:', err)` | 错误回调函数,接收错误对象作为参数 |
102
102
  | `onLog` | `Function` | `null` | 操作日志回调函数,用于记录正常操作信息 |
103
103
  | `storage` | `Object` | `null` | 自定义存储配置(需提供 `get`、`set`、`remove` 方法),默认使用 localStorage |
104
+ | `t` | `string` | `t` | 重新加载时的时间戳参数名 |
104
105
 
105
106
  ### 配置项最佳实践
106
107
 
@@ -239,7 +240,7 @@ axios.interceptors.request.use(
239
240
  },
240
241
  error => {
241
242
  return Promise.reject(error);
242
- }
243
+ },
243
244
  );
244
245
 
245
246
  versionCheck.start();
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- class t{constructor(t={}){if("object"!=typeof t||null===t)throw new TypeError("options \u5fc5\u987b\u662f\u5bf9\u8c61\u7c7b\u578b");this.config={url:"/",interval:6e5,message:"\u68c0\u6d4b\u5230\u65b0\u7248\u672c\uff0c\u662f\u5426\u7acb\u5373\u5237\u65b0\uff1f",onUpdate:null,onError:t=>{},onLog:null,storage:null,...t},this.timer=null,this.memoryStorage=null,this.storageApi=this.t(),this.versionKey="version_check_key",this.checkMode=this.i(),this.isRunning=!1,this.h()}o(t){"function"==typeof this.config.onLog&&this.config.onLog(t)}h(){this.l(),this.u=this.$.bind(this),document.addEventListener("visibilitychange",this.u)}l(){this.u&&(document.removeEventListener("visibilitychange",this.u),this.u=null)}$(){this.isRunning&&(document.hidden?this.p():this.m())}p(){this.stop(!0),this.o("\u9875\u9762\u9690\u85cf\uff0c\u6682\u505cVersionCheck")}m(){this.start(),this.o("\u9875\u9762\u663e\u793a\uff0c\u6062\u590dVersionCheck")}t(){const t={get:t=>{if("string"!=typeof t)return this.config.onError(new Error("\u5b58\u50a8\u952e\u5fc5\u987b\u662f\u5b57\u7b26\u4e32")),null;try{if(window.localStorage)return localStorage.getItem(t)}catch(t){this.config.onError(new Error(`localStorage get \u5931\u8d25: ${t.message}`))}return this.memoryStorage},set:(t,i)=>{if("string"!=typeof t)return this.config.onError(new Error("\u5b58\u50a8\u952e\u5fc5\u987b\u662f\u5b57\u7b26\u4e32")),!1;try{if(window.localStorage)return localStorage.setItem(t,String(i)),!0}catch(t){this.config.onError(new Error(`localStorage set \u5931\u8d25: ${t.message}`))}return this.memoryStorage=String(i),!0}};return this.config.storage||t}i(){const{url:t}=this.config;return"/"!==t&&/\.\w+$/.test(t)?"file":"etag"}async _(t,i){try{const s=await fetch(t,i);if(s.status>=400&&s.status<500)return this.config.onError(new Error(`\u5ba2\u6237\u7aef\u9519\u8bef HTTP ${s.status}: ${s.statusText}`)),s;if(!s.ok)throw new Error(`HTTP ${s.status}: ${s.statusText}`);return s}catch(t){throw this.config.onError(new Error(`\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25: ${t.message}`)),t}}async T(){const{url:t}=this.config;try{const i=await this._(t,{method:"HEAD",cache:"no-cache",credentials:"same-origin"});if(!i.ok)throw new Error(`ETag \u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a${i.status}`);const s=i.headers.get("ETag");if(!s)throw new Error("\u670d\u52a1\u5668\u672a\u8fd4\u56de ETag\uff0c\u68c0\u6d4b\u5931\u8d25");return this.k(s)}catch(t){return this.config.onError(t),!1}}async V(){const{url:t}=this.config;try{const i=await this._(t,{method:"GET",cache:"no-cache",credentials:"same-origin",headers:{Accept:"application/json"}});if(!i.ok)throw new Error(`\u7248\u672c\u6587\u4ef6\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a${i.status}`);const s=await i.json();if(!s||"object"!=typeof s)throw new Error("\u7248\u672c\u6587\u4ef6\u683c\u5f0f\u9519\u8bef\uff0c\u5fc5\u987b\u8fd4\u56de\u5bf9\u8c61");if(!s.version)throw new Error("\u7248\u672c\u6587\u4ef6\u683c\u5f0f\u9519\u8bef\uff0c\u7f3a\u5c11 version \u5b57\u6bb5");return this.k(s.version)}catch(t){return this.config.onError(t),!1}}k(t){const i=this.storageApi.get(this.versionKey);return i?t!=i&&(this.C(t),!0):(this.storageApi.set(this.versionKey,t),!1)}C(t){const{onUpdate:i,message:s}=this.config;if("function"==typeof i)return this.storageApi.set(this.versionKey,t),void i();this.stop(),setTimeout(()=>{window.confirm(s)?(this.storageApi.set(this.versionKey,t),this.reload()):this.start()},0)}reload(){let t=window.location.href;t=t.replace(/[?&]t=\d+/g,"");const i=t.includes("?")?"&":"?",s=`${t}${i}t=${Date.now()}`;window.location.replace(s)}async check(t=!1){this.o(`\u5f00\u59cb\u6267\u884c${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\uff0c\u6a21\u5f0f: ${this.checkMode}`);try{const i="etag"===this.checkMode?await this.T():await this.V();return this.o(`${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\u5b8c\u6210\uff0c\u68c0\u6d4b\u5230\u66f4\u65b0\u72b6\u6001: ${i}`),i}catch(i){throw this.o(`${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\u5931\u8d25: ${i.message}`),i}}start(){if(this.isRunning&&this.timer)return void this.o("VersionCheck\u5df2\u5728\u8fd0\u884c\u4e2d");this.isRunning=!0,this.v();const t=async()=>{try{await this.check(!0)}catch(t){this.config.onError(new Error(`\u8f6e\u8be2\u68c0\u6d4b\u5931\u8d25: ${t.message}`))}this.isRunning&&this.timer&&(this.timer=setTimeout(t,this.config.interval))};this.timer=setTimeout(t,this.config.interval),this.o("VersionCheck\u5df2\u542f\u52a8")}stop(t=!1){this.v(),t||(this.isRunning=!1),this.o("VersionCheck\u5df2\u505c\u6b62")}v(){this.timer&&(clearTimeout(this.timer),this.timer=null)}destroy(){this.stop(),this.l(),this.memoryStorage=null,this.timer=null,this.storageApi=null,this.checkMode=null,this.isRunning=!1,this.o("VersionCheck\u5b9e\u4f8b\u5df2\u9500\u6bc1"),this.config=null}}export{t as default};
1
+ class t{constructor(t={}){if("object"!=typeof t||null===t)throw new TypeError("options \u5fc5\u987b\u662f\u5bf9\u8c61\u7c7b\u578b");this.config={url:"/",interval:6e5,message:"\u68c0\u6d4b\u5230\u65b0\u7248\u672c\uff0c\u662f\u5426\u7acb\u5373\u5237\u65b0\uff1f",onUpdate:null,onError:t=>{},onLog:null,storage:null,t:"t",...t},this.timer=null,this.memoryStorage=null,this.storageApi=this.i(),this.versionKey="version_check_key",this.checkMode=this.h(),this.isRunning=!1,this.o()}l(t){"function"==typeof this.config.onLog&&this.config.onLog(t)}o(){this.u(),this.$=this.p.bind(this),document.addEventListener("visibilitychange",this.$)}u(){this.$&&(document.removeEventListener("visibilitychange",this.$),this.$=null)}p(){this.isRunning&&(document.hidden?this.m():this._())}m(){this.stop(!0),this.l("\u9875\u9762\u9690\u85cf\uff0c\u6682\u505cVersionCheck")}_(){this.start(),this.l("\u9875\u9762\u663e\u793a\uff0c\u6062\u590dVersionCheck")}i(){const t={get:t=>{if("string"!=typeof t)return this.config.onError(new Error("\u5b58\u50a8\u952e\u5fc5\u987b\u662f\u5b57\u7b26\u4e32")),null;try{if(window.localStorage)return localStorage.getItem(t)}catch(t){this.config.onError(new Error(`localStorage get \u5931\u8d25: ${t.message}`))}return this.memoryStorage},set:(t,i)=>{if("string"!=typeof t)return this.config.onError(new Error("\u5b58\u50a8\u952e\u5fc5\u987b\u662f\u5b57\u7b26\u4e32")),!1;try{if(window.localStorage)return localStorage.setItem(t,String(i)),!0}catch(t){this.config.onError(new Error(`localStorage set \u5931\u8d25: ${t.message}`))}return this.memoryStorage=String(i),!0}};return this.config.storage||t}h(){const{url:t}=this.config;return"/"!==t&&/\.\w+$/.test(t)?"file":"etag"}async T(t,i){try{const s=await fetch(t,i);if(s.status>=400&&s.status<500)return this.config.onError(new Error(`\u5ba2\u6237\u7aef\u9519\u8bef HTTP ${s.status}: ${s.statusText}`)),s;if(!s.ok)throw new Error(`HTTP ${s.status}: ${s.statusText}`);return s}catch(t){throw this.config.onError(new Error(`\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25: ${t.message}`)),t}}async k(){const{url:t}=this.config;try{const i=await this.T(t,{method:"HEAD",cache:"no-cache",credentials:"same-origin"});if(!i.ok)throw new Error(`ETag \u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a${i.status}`);const s=i.headers.get("ETag");if(!s)throw new Error("\u670d\u52a1\u5668\u672a\u8fd4\u56de ETag\uff0c\u68c0\u6d4b\u5931\u8d25");return this.V(s)}catch(t){return this.config.onError(t),!1}}async C(){const{url:t}=this.config;try{const i=await this.T(t,{method:"GET",cache:"no-cache",credentials:"same-origin",headers:{Accept:"application/json"}});if(!i.ok)throw new Error(`\u7248\u672c\u6587\u4ef6\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a${i.status}`);const s=await i.json();if(!s||"object"!=typeof s)throw new Error("\u7248\u672c\u6587\u4ef6\u683c\u5f0f\u9519\u8bef\uff0c\u5fc5\u987b\u8fd4\u56de\u5bf9\u8c61");if(!s.version)throw new Error("\u7248\u672c\u6587\u4ef6\u683c\u5f0f\u9519\u8bef\uff0c\u7f3a\u5c11 version \u5b57\u6bb5");return this.V(s.version)}catch(t){return this.config.onError(t),!1}}V(t){const i=this.storageApi.get(this.versionKey);return i?t!=i&&(this.v(t),!0):(this.storageApi.set(this.versionKey,t),!1)}v(t){const{onUpdate:i,message:s}=this.config;if("function"==typeof i)return this.storageApi.set(this.versionKey,t),void i();this.stop(),setTimeout(()=>{window.confirm(s)?(this.storageApi.set(this.versionKey,t),this.reload()):this.start()},0)}reload(){let t=window.location.href;const i=this.config.t;t=t.replace(new RegExp(`[?&]${i}=\\d+`,"g"),"");const s=t.includes("?")?"&":"?",e=`${t}${s}${i}=${Date.now()}`;window.location.replace(e)}async check(t=!1){this.l(`\u5f00\u59cb\u6267\u884c${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\uff0c\u6a21\u5f0f: ${this.checkMode}`);try{const i="etag"===this.checkMode?await this.k():await this.C();return this.l(`${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\u5b8c\u6210\uff0c\u68c0\u6d4b\u5230\u66f4\u65b0\u72b6\u6001: ${i}`),i}catch(i){throw this.l(`${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\u5931\u8d25: ${i.message}`),i}}start(){if(this.isRunning&&this.timer)return void this.l("VersionCheck\u5df2\u5728\u8fd0\u884c\u4e2d");this.isRunning=!0,this.S();const t=async()=>{try{await this.check(!0)}catch(t){this.config.onError(new Error(`\u8f6e\u8be2\u68c0\u6d4b\u5931\u8d25: ${t.message}`))}this.isRunning&&this.timer&&(this.timer=setTimeout(t,this.config.interval))};this.timer=setTimeout(t,this.config.interval),this.l("VersionCheck\u5df2\u542f\u52a8")}stop(t=!1){this.S(),t||(this.isRunning=!1),this.l("VersionCheck\u5df2\u505c\u6b62")}S(){this.timer&&(clearTimeout(this.timer),this.timer=null)}destroy(){this.stop(),this.u(),this.memoryStorage=null,this.timer=null,this.storageApi=null,this.checkMode=null,this.isRunning=!1,this.l("VersionCheck\u5b9e\u4f8b\u5df2\u9500\u6bc1"),this.config=null}}export{t as default};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var global,factory;global=this,factory=function(){return class{constructor(t={}){if("object"!=typeof t||null===t)throw new TypeError("options \u5fc5\u987b\u662f\u5bf9\u8c61\u7c7b\u578b");this.config={url:"/",interval:6e5,message:"\u68c0\u6d4b\u5230\u65b0\u7248\u672c\uff0c\u662f\u5426\u7acb\u5373\u5237\u65b0\uff1f",onUpdate:null,onError:t=>{},onLog:null,storage:null,...t},this.timer=null,this.memoryStorage=null,this.storageApi=this.t(),this.versionKey="version_check_key",this.checkMode=this.i(),this.isRunning=!1,this.h()}o(t){"function"==typeof this.config.onLog&&this.config.onLog(t)}h(){this.l(),this.u=this.p.bind(this),document.addEventListener("visibilitychange",this.u)}l(){this.u&&(document.removeEventListener("visibilitychange",this.u),this.u=null)}p(){this.isRunning&&(document.hidden?this.$():this.m())}$(){this.stop(!0),this.o("\u9875\u9762\u9690\u85cf\uff0c\u6682\u505cVersionCheck")}m(){this.start(),this.o("\u9875\u9762\u663e\u793a\uff0c\u6062\u590dVersionCheck")}t(){const t={get:t=>{if("string"!=typeof t)return this.config.onError(new Error("\u5b58\u50a8\u952e\u5fc5\u987b\u662f\u5b57\u7b26\u4e32")),null;try{if(window.localStorage)return localStorage.getItem(t)}catch(t){this.config.onError(new Error(`localStorage get \u5931\u8d25: ${t.message}`))}return this.memoryStorage},set:(t,i)=>{if("string"!=typeof t)return this.config.onError(new Error("\u5b58\u50a8\u952e\u5fc5\u987b\u662f\u5b57\u7b26\u4e32")),!1;try{if(window.localStorage)return localStorage.setItem(t,String(i)),!0}catch(t){this.config.onError(new Error(`localStorage set \u5931\u8d25: ${t.message}`))}return this.memoryStorage=String(i),!0}};return this.config.storage||t}i(){const{url:t}=this.config;return"/"!==t&&/\.\w+$/.test(t)?"file":"etag"}async T(t,i){try{const s=await fetch(t,i);if(s.status>=400&&s.status<500)return this.config.onError(new Error(`\u5ba2\u6237\u7aef\u9519\u8bef HTTP ${s.status}: ${s.statusText}`)),s;if(!s.ok)throw new Error(`HTTP ${s.status}: ${s.statusText}`);return s}catch(t){throw this.config.onError(new Error(`\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25: ${t.message}`)),t}}async _(){const{url:t}=this.config;try{const i=await this.T(t,{method:"HEAD",cache:"no-cache",credentials:"same-origin"});if(!i.ok)throw new Error(`ETag \u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a${i.status}`);const s=i.headers.get("ETag");if(!s)throw new Error("\u670d\u52a1\u5668\u672a\u8fd4\u56de ETag\uff0c\u68c0\u6d4b\u5931\u8d25");return this.k(s)}catch(t){return this.config.onError(t),!1}}async V(){const{url:t}=this.config;try{const i=await this.T(t,{method:"GET",cache:"no-cache",credentials:"same-origin",headers:{Accept:"application/json"}});if(!i.ok)throw new Error(`\u7248\u672c\u6587\u4ef6\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a${i.status}`);const s=await i.json();if(!s||"object"!=typeof s)throw new Error("\u7248\u672c\u6587\u4ef6\u683c\u5f0f\u9519\u8bef\uff0c\u5fc5\u987b\u8fd4\u56de\u5bf9\u8c61");if(!s.version)throw new Error("\u7248\u672c\u6587\u4ef6\u683c\u5f0f\u9519\u8bef\uff0c\u7f3a\u5c11 version \u5b57\u6bb5");return this.k(s.version)}catch(t){return this.config.onError(t),!1}}k(t){const i=this.storageApi.get(this.versionKey);return i?t!=i&&(this.v(t),!0):(this.storageApi.set(this.versionKey,t),!1)}v(t){const{onUpdate:i,message:s}=this.config;if("function"==typeof i)return this.storageApi.set(this.versionKey,t),void i();this.stop(),setTimeout(()=>{window.confirm(s)?(this.storageApi.set(this.versionKey,t),this.reload()):this.start()},0)}reload(){let t=window.location.href;t=t.replace(/[?&]t=\d+/g,"");const i=t.includes("?")?"&":"?",s=`${t}${i}t=${Date.now()}`;window.location.replace(s)}async check(t=!1){this.o(`\u5f00\u59cb\u6267\u884c${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\uff0c\u6a21\u5f0f: ${this.checkMode}`);try{const i="etag"===this.checkMode?await this._():await this.V();return this.o(`${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\u5b8c\u6210\uff0c\u68c0\u6d4b\u5230\u66f4\u65b0\u72b6\u6001: ${i}`),i}catch(i){throw this.o(`${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\u5931\u8d25: ${i.message}`),i}}start(){if(this.isRunning&&this.timer)return void this.o("VersionCheck\u5df2\u5728\u8fd0\u884c\u4e2d");this.isRunning=!0,this.C();const t=async()=>{try{await this.check(!0)}catch(t){this.config.onError(new Error(`\u8f6e\u8be2\u68c0\u6d4b\u5931\u8d25: ${t.message}`))}this.isRunning&&this.timer&&(this.timer=setTimeout(t,this.config.interval))};this.timer=setTimeout(t,this.config.interval),this.o("VersionCheck\u5df2\u542f\u52a8")}stop(t=!1){this.C(),t||(this.isRunning=!1),this.o("VersionCheck\u5df2\u505c\u6b62")}C(){this.timer&&(clearTimeout(this.timer),this.timer=null)}destroy(){this.stop(),this.l(),this.memoryStorage=null,this.timer=null,this.storageApi=null,this.checkMode=null,this.isRunning=!1,this.o("VersionCheck\u5b9e\u4f8b\u5df2\u9500\u6bc1"),this.config=null}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):(global="undefined"!=typeof globalThis?globalThis:global||self).VersionCheck=factory();
1
+ var global,factory;global=this,factory=function(){return class{constructor(t={}){if("object"!=typeof t||null===t)throw new TypeError("options \u5fc5\u987b\u662f\u5bf9\u8c61\u7c7b\u578b");this.config={url:"/",interval:6e5,message:"\u68c0\u6d4b\u5230\u65b0\u7248\u672c\uff0c\u662f\u5426\u7acb\u5373\u5237\u65b0\uff1f",onUpdate:null,onError:t=>{},onLog:null,storage:null,t:"t",...t},this.timer=null,this.memoryStorage=null,this.storageApi=this.i(),this.versionKey="version_check_key",this.checkMode=this.h(),this.isRunning=!1,this.o()}l(t){"function"==typeof this.config.onLog&&this.config.onLog(t)}o(){this.u(),this.p=this.$.bind(this),document.addEventListener("visibilitychange",this.p)}u(){this.p&&(document.removeEventListener("visibilitychange",this.p),this.p=null)}$(){this.isRunning&&(document.hidden?this.m():this.T())}m(){this.stop(!0),this.l("\u9875\u9762\u9690\u85cf\uff0c\u6682\u505cVersionCheck")}T(){this.start(),this.l("\u9875\u9762\u663e\u793a\uff0c\u6062\u590dVersionCheck")}i(){const t={get:t=>{if("string"!=typeof t)return this.config.onError(new Error("\u5b58\u50a8\u952e\u5fc5\u987b\u662f\u5b57\u7b26\u4e32")),null;try{if(window.localStorage)return localStorage.getItem(t)}catch(t){this.config.onError(new Error(`localStorage get \u5931\u8d25: ${t.message}`))}return this.memoryStorage},set:(t,i)=>{if("string"!=typeof t)return this.config.onError(new Error("\u5b58\u50a8\u952e\u5fc5\u987b\u662f\u5b57\u7b26\u4e32")),!1;try{if(window.localStorage)return localStorage.setItem(t,String(i)),!0}catch(t){this.config.onError(new Error(`localStorage set \u5931\u8d25: ${t.message}`))}return this.memoryStorage=String(i),!0}};return this.config.storage||t}h(){const{url:t}=this.config;return"/"!==t&&/\.\w+$/.test(t)?"file":"etag"}async _(t,i){try{const s=await fetch(t,i);if(s.status>=400&&s.status<500)return this.config.onError(new Error(`\u5ba2\u6237\u7aef\u9519\u8bef HTTP ${s.status}: ${s.statusText}`)),s;if(!s.ok)throw new Error(`HTTP ${s.status}: ${s.statusText}`);return s}catch(t){throw this.config.onError(new Error(`\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25: ${t.message}`)),t}}async k(){const{url:t}=this.config;try{const i=await this._(t,{method:"HEAD",cache:"no-cache",credentials:"same-origin"});if(!i.ok)throw new Error(`ETag \u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a${i.status}`);const s=i.headers.get("ETag");if(!s)throw new Error("\u670d\u52a1\u5668\u672a\u8fd4\u56de ETag\uff0c\u68c0\u6d4b\u5931\u8d25");return this.V(s)}catch(t){return this.config.onError(t),!1}}async v(){const{url:t}=this.config;try{const i=await this._(t,{method:"GET",cache:"no-cache",credentials:"same-origin",headers:{Accept:"application/json"}});if(!i.ok)throw new Error(`\u7248\u672c\u6587\u4ef6\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a${i.status}`);const s=await i.json();if(!s||"object"!=typeof s)throw new Error("\u7248\u672c\u6587\u4ef6\u683c\u5f0f\u9519\u8bef\uff0c\u5fc5\u987b\u8fd4\u56de\u5bf9\u8c61");if(!s.version)throw new Error("\u7248\u672c\u6587\u4ef6\u683c\u5f0f\u9519\u8bef\uff0c\u7f3a\u5c11 version \u5b57\u6bb5");return this.V(s.version)}catch(t){return this.config.onError(t),!1}}V(t){const i=this.storageApi.get(this.versionKey);return i?t!=i&&(this.C(t),!0):(this.storageApi.set(this.versionKey,t),!1)}C(t){const{onUpdate:i,message:s}=this.config;if("function"==typeof i)return this.storageApi.set(this.versionKey,t),void i();this.stop(),setTimeout(()=>{window.confirm(s)?(this.storageApi.set(this.versionKey,t),this.reload()):this.start()},0)}reload(){let t=window.location.href;const i=this.config.t;t=t.replace(new RegExp(`[?&]${i}=\\d+`,"g"),"");const s=t.includes("?")?"&":"?",e=`${t}${s}${i}=${Date.now()}`;window.location.replace(e)}async check(t=!1){this.l(`\u5f00\u59cb\u6267\u884c${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\uff0c\u6a21\u5f0f: ${this.checkMode}`);try{const i="etag"===this.checkMode?await this.k():await this.v();return this.l(`${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\u5b8c\u6210\uff0c\u68c0\u6d4b\u5230\u66f4\u65b0\u72b6\u6001: ${i}`),i}catch(i){throw this.l(`${t?"\u68c0\u6d4b":"\u624b\u52a8\u68c0\u6d4b"}\u5931\u8d25: ${i.message}`),i}}start(){if(this.isRunning&&this.timer)return void this.l("VersionCheck\u5df2\u5728\u8fd0\u884c\u4e2d");this.isRunning=!0,this.S();const t=async()=>{try{await this.check(!0)}catch(t){this.config.onError(new Error(`\u8f6e\u8be2\u68c0\u6d4b\u5931\u8d25: ${t.message}`))}this.isRunning&&this.timer&&(this.timer=setTimeout(t,this.config.interval))};this.timer=setTimeout(t,this.config.interval),this.l("VersionCheck\u5df2\u542f\u52a8")}stop(t=!1){this.S(),t||(this.isRunning=!1),this.l("VersionCheck\u5df2\u505c\u6b62")}S(){this.timer&&(clearTimeout(this.timer),this.timer=null)}destroy(){this.stop(),this.u(),this.memoryStorage=null,this.timer=null,this.storageApi=null,this.checkMode=null,this.isRunning=!1,this.l("VersionCheck\u5b9e\u4f8b\u5df2\u9500\u6bc1"),this.config=null}}},"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):(global="undefined"!=typeof globalThis?globalThis:global||self).VersionCheck=factory();
package/index.d.ts CHANGED
@@ -46,6 +46,9 @@ export interface VersionCheckOptions {
46
46
 
47
47
  /** 自定义存储配置 */
48
48
  storage?: VersionCheckStorageAdapter;
49
+
50
+ /** 重新加载时的时间戳参数名,默认't' */
51
+ t?: string;
49
52
  }
50
53
 
51
54
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "version-check-js",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "version-check-js极简配置的前端版本检测工具,自动判断ETag/版本文件模式,默认自动轮询,保留手动检测 。原生javascript实现。不局限于框架使用",
5
5
  "main": "dist/index.js",
6
6
  "types": "index.d.ts",