@leancodepl/force-update 8.5.0
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/index.cjs.default.js +1 -0
- package/index.cjs.js +79 -0
- package/index.cjs.mjs +2 -0
- package/index.d.ts +1 -0
- package/index.esm.js +75 -0
- package/package.json +24 -0
- package/src/index.d.ts +3 -0
- package/src/lib/ForceUpdateNotification/index.d.ts +4 -0
- package/src/lib/listenOnForceUpdate.d.ts +14 -0
- package/src/lib/vitePluginForceUpdate.d.ts +13 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./index.cjs.js').default;
|
package/index.cjs.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var rxjs = require('rxjs');
|
|
5
|
+
var ajax = require('rxjs/ajax');
|
|
6
|
+
var operators = require('rxjs/operators');
|
|
7
|
+
|
|
8
|
+
const defaultVersionCheckIntervalPeriod = 3 * 60 * 1000;
|
|
9
|
+
/**
|
|
10
|
+
* Listens for force updates by periodically checking the `/version` endpoint.
|
|
11
|
+
* When a new version is detected, it calls the provided callback function.
|
|
12
|
+
*
|
|
13
|
+
* @param params - Configuration object for force update listening
|
|
14
|
+
* @param params.versionCheckIntervalPeriod - The interval period in milliseconds to check for version updates (default: 180000 - 3 minutes)
|
|
15
|
+
* @param params.onNewVersionAvailable - Callback function that is called when a new version is available
|
|
16
|
+
* @returns A cleanup function that unsubscribes from the version checking
|
|
17
|
+
*/ const listenOnForceUpdate = ({ versionCheckIntervalPeriod = defaultVersionCheckIntervalPeriod, onNewVersionAvailable })=>{
|
|
18
|
+
const versionUrl = ()=>ajax.ajax({
|
|
19
|
+
url: "/version",
|
|
20
|
+
responseType: "text",
|
|
21
|
+
headers: {
|
|
22
|
+
"Cache-Control": "no-store"
|
|
23
|
+
}
|
|
24
|
+
}).pipe(operators.map((response)=>{
|
|
25
|
+
const version = response.response;
|
|
26
|
+
return typeof version === "string" ? version.trim() : null;
|
|
27
|
+
}), operators.catchError(()=>rxjs.of(null)));
|
|
28
|
+
const subscription = rxjs.interval(versionCheckIntervalPeriod).pipe(operators.mergeMap(versionUrl), operators.scan((lastVersion, version)=>version != null ? version : lastVersion), operators.pairwise(), operators.first(([previousVersion, currentVersion])=>!!previousVersion && previousVersion !== currentVersion)).subscribe(()=>{
|
|
29
|
+
onNewVersionAvailable();
|
|
30
|
+
subscription.unsubscribe();
|
|
31
|
+
});
|
|
32
|
+
return ()=>subscription.unsubscribe();
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function ForceUpdateNotification({ message = "A new version of the app is available. Please reload the page to access latest features." }) {
|
|
36
|
+
react.useEffect(()=>{
|
|
37
|
+
const cleanup = listenOnForceUpdate({
|
|
38
|
+
onNewVersionAvailable: ()=>{
|
|
39
|
+
if (window.confirm(message)) {
|
|
40
|
+
window.location.reload();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return cleanup;
|
|
45
|
+
}, [
|
|
46
|
+
message
|
|
47
|
+
]);
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Vite plugin that emits a version asset file based on an environment variable, 'APP_VERSION' as default
|
|
53
|
+
* This is used for force update mechanism that needs to check the current app version.
|
|
54
|
+
*
|
|
55
|
+
* @param options - Configuration options for the plugin
|
|
56
|
+
* @param options.envVarName - The name of the environment variable to read the version from (default: 'APP_VERSION')
|
|
57
|
+
* @returns A Vite plugin that creates a /version endpoint serving the current app version
|
|
58
|
+
*/ function vitePluginForceUpdate(options = {}) {
|
|
59
|
+
const { envVarName = "APP_VERSION" } = options;
|
|
60
|
+
return {
|
|
61
|
+
name: "vite-plugin-force-update",
|
|
62
|
+
generateBundle () {
|
|
63
|
+
const version = process.env[envVarName];
|
|
64
|
+
if (!version) {
|
|
65
|
+
console.warn(`Environment variable ${envVarName} is not set.`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.emitFile({
|
|
69
|
+
type: "asset",
|
|
70
|
+
fileName: "version",
|
|
71
|
+
source: version.trim()
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
exports.ForceUpdateNotification = ForceUpdateNotification;
|
|
78
|
+
exports.listenOnForceUpdate = listenOnForceUpdate;
|
|
79
|
+
exports.vitePluginForceUpdate = vitePluginForceUpdate;
|
package/index.cjs.mjs
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { interval, of } from 'rxjs';
|
|
3
|
+
import { ajax } from 'rxjs/ajax';
|
|
4
|
+
import { mergeMap, scan, pairwise, first, map, catchError } from 'rxjs/operators';
|
|
5
|
+
|
|
6
|
+
const defaultVersionCheckIntervalPeriod = 3 * 60 * 1000;
|
|
7
|
+
/**
|
|
8
|
+
* Listens for force updates by periodically checking the `/version` endpoint.
|
|
9
|
+
* When a new version is detected, it calls the provided callback function.
|
|
10
|
+
*
|
|
11
|
+
* @param params - Configuration object for force update listening
|
|
12
|
+
* @param params.versionCheckIntervalPeriod - The interval period in milliseconds to check for version updates (default: 180000 - 3 minutes)
|
|
13
|
+
* @param params.onNewVersionAvailable - Callback function that is called when a new version is available
|
|
14
|
+
* @returns A cleanup function that unsubscribes from the version checking
|
|
15
|
+
*/ const listenOnForceUpdate = ({ versionCheckIntervalPeriod = defaultVersionCheckIntervalPeriod, onNewVersionAvailable })=>{
|
|
16
|
+
const versionUrl = ()=>ajax({
|
|
17
|
+
url: "/version",
|
|
18
|
+
responseType: "text",
|
|
19
|
+
headers: {
|
|
20
|
+
"Cache-Control": "no-store"
|
|
21
|
+
}
|
|
22
|
+
}).pipe(map((response)=>{
|
|
23
|
+
const version = response.response;
|
|
24
|
+
return typeof version === "string" ? version.trim() : null;
|
|
25
|
+
}), catchError(()=>of(null)));
|
|
26
|
+
const subscription = interval(versionCheckIntervalPeriod).pipe(mergeMap(versionUrl), scan((lastVersion, version)=>version != null ? version : lastVersion), pairwise(), first(([previousVersion, currentVersion])=>!!previousVersion && previousVersion !== currentVersion)).subscribe(()=>{
|
|
27
|
+
onNewVersionAvailable();
|
|
28
|
+
subscription.unsubscribe();
|
|
29
|
+
});
|
|
30
|
+
return ()=>subscription.unsubscribe();
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function ForceUpdateNotification({ message = "A new version of the app is available. Please reload the page to access latest features." }) {
|
|
34
|
+
useEffect(()=>{
|
|
35
|
+
const cleanup = listenOnForceUpdate({
|
|
36
|
+
onNewVersionAvailable: ()=>{
|
|
37
|
+
if (window.confirm(message)) {
|
|
38
|
+
window.location.reload();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return cleanup;
|
|
43
|
+
}, [
|
|
44
|
+
message
|
|
45
|
+
]);
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Vite plugin that emits a version asset file based on an environment variable, 'APP_VERSION' as default
|
|
51
|
+
* This is used for force update mechanism that needs to check the current app version.
|
|
52
|
+
*
|
|
53
|
+
* @param options - Configuration options for the plugin
|
|
54
|
+
* @param options.envVarName - The name of the environment variable to read the version from (default: 'APP_VERSION')
|
|
55
|
+
* @returns A Vite plugin that creates a /version endpoint serving the current app version
|
|
56
|
+
*/ function vitePluginForceUpdate(options = {}) {
|
|
57
|
+
const { envVarName = "APP_VERSION" } = options;
|
|
58
|
+
return {
|
|
59
|
+
name: "vite-plugin-force-update",
|
|
60
|
+
generateBundle () {
|
|
61
|
+
const version = process.env[envVarName];
|
|
62
|
+
if (!version) {
|
|
63
|
+
console.warn(`Environment variable ${envVarName} is not set.`);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
this.emitFile({
|
|
67
|
+
type: "asset",
|
|
68
|
+
fileName: "version",
|
|
69
|
+
source: version.trim()
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { ForceUpdateNotification, listenOnForceUpdate, vitePluginForceUpdate };
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leancodepl/force-update",
|
|
3
|
+
"version": "8.5.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"rxjs": ">=7.0.0"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"react": "*",
|
|
10
|
+
"vite": ">=4.0.0"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
"./package.json": "./package.json",
|
|
14
|
+
".": {
|
|
15
|
+
"module": "./index.esm.js",
|
|
16
|
+
"types": "./index.d.ts",
|
|
17
|
+
"import": "./index.cjs.mjs",
|
|
18
|
+
"default": "./index.cjs.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"module": "./index.esm.js",
|
|
22
|
+
"main": "./index.cjs.js",
|
|
23
|
+
"types": "./index.d.ts"
|
|
24
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type ForceUpdateParams = {
|
|
2
|
+
versionCheckIntervalPeriod?: number;
|
|
3
|
+
onNewVersionAvailable: () => void;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Listens for force updates by periodically checking the `/version` endpoint.
|
|
7
|
+
* When a new version is detected, it calls the provided callback function.
|
|
8
|
+
*
|
|
9
|
+
* @param params - Configuration object for force update listening
|
|
10
|
+
* @param params.versionCheckIntervalPeriod - The interval period in milliseconds to check for version updates (default: 180000 - 3 minutes)
|
|
11
|
+
* @param params.onNewVersionAvailable - Callback function that is called when a new version is available
|
|
12
|
+
* @returns A cleanup function that unsubscribes from the version checking
|
|
13
|
+
*/
|
|
14
|
+
export declare const listenOnForceUpdate: ({ versionCheckIntervalPeriod, onNewVersionAvailable, }: ForceUpdateParams) => () => void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
export interface ForceUpdatePluginOptions {
|
|
3
|
+
envVarName?: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Vite plugin that emits a version asset file based on an environment variable, 'APP_VERSION' as default
|
|
7
|
+
* This is used for force update mechanism that needs to check the current app version.
|
|
8
|
+
*
|
|
9
|
+
* @param options - Configuration options for the plugin
|
|
10
|
+
* @param options.envVarName - The name of the environment variable to read the version from (default: 'APP_VERSION')
|
|
11
|
+
* @returns A Vite plugin that creates a /version endpoint serving the current app version
|
|
12
|
+
*/
|
|
13
|
+
export declare function vitePluginForceUpdate(options?: ForceUpdatePluginOptions): Plugin;
|