@interface-technologies/check-for-js-bundle-update-saga 2.0.0-alpha.7 → 4.0.0-alpha.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/LICENSE CHANGED
@@ -1,7 +1,7 @@
1
- Copyright 2018 Interface Technologies, Inc.
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
1
+ Copyright 2018 Interface Technologies, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
7
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1 +1 @@
1
- See https://srmagura.github.io/iti-react/.
1
+ See https://srmagura.github.io/iti-react/.
@@ -0,0 +1,65 @@
1
+ import moment from 'moment-timezone';
2
+ import { SagaIterator } from 'redux-saga';
3
+ /** @internal */
4
+ export declare function getIndexHtml(): Promise<string | undefined>;
5
+ /** @internal */
6
+ export declare function reload(): void;
7
+ export interface CheckForJsBundleUpdateSagaOptions {
8
+ delayDuration?: moment.Duration;
9
+ onError(e: unknown): void;
10
+ }
11
+ /**
12
+ * Peridoically fetches `/` (`index.html`) to check if a new JavaScript bundle has been
13
+ * released.
14
+ *
15
+ * If the bundle has been updated, the user is prompted to refresh the page.
16
+ * After 3 alerts are shown, the page is forcibly refreshed.
17
+ *
18
+ * Don't enable this in development!
19
+ *
20
+ * Your `index.html` must contain a hidden element with the ID `jsBundleHash` that
21
+ * contains a hash of the JavaScript bundle's entry point. Here's how to do this
22
+ * in ASP.NET Core:
23
+ *
24
+ * ```razor
25
+ * var path = $"dist/{bundleName}.js";
26
+ * string jsBundleHash;
27
+ *
28
+ * using (var sha256 = SHA256.Create())
29
+ * {
30
+ * var fullPath = System.IO.Path.Combine("wwwroot", path);
31
+ * using (var readStream = File.OpenRead(fullPath))
32
+ * {
33
+ * var hashBytes = sha256.ComputeHash(readStream);
34
+ * jsBundleHash = WebEncoders.Base64UrlEncode(hashBytes);
35
+ * }
36
+ * }
37
+ *
38
+ * <script src=@path asp-append-version="true"></script>
39
+ * <span id="jsBundleHash" style="display: none">
40
+ * @jsBundleHash
41
+ * </span>
42
+ * ```
43
+ *
44
+ * And example of using `checkForJsBundleUpdateSaga` from your TypeScript code:
45
+ *
46
+ * ```
47
+ * export function* myCheckForJsBundleUpdateSaga(): SagaIterator<void> {
48
+ * if (process.env.NODE_ENV === 'development') return
49
+ *
50
+ * function onError(e: unknown): void {
51
+ * console.error(e)
52
+ *
53
+ * const ierror = processError(e)
54
+ *
55
+ * // Never show the user an error because of this
56
+ * if (shouldLogError(ierror)) {
57
+ * Bugsnag.notify(ierror)
58
+ * }
59
+ * }
60
+ *
61
+ * yield call(checkForJsBundleUpdateSaga, { onError })
62
+ * }
63
+ * ```
64
+ */
65
+ export declare function checkForJsBundleUpdateSaga({ delayDuration, onError, }: CheckForJsBundleUpdateSagaOptions): SagaIterator<void>;
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.checkForJsBundleUpdateSaga = exports.reload = exports.getIndexHtml = void 0;
7
+ const react_1 = __importDefault(require("react"));
8
+ const moment_timezone_1 = __importDefault(require("moment-timezone"));
9
+ const effects_1 = require("redux-saga/effects");
10
+ const iti_react_1 = require("@interface-technologies/iti-react");
11
+ const hashElementId = 'jsBundleHash';
12
+ const defaultDelayDuration = moment_timezone_1.default.duration(4, 'minutes');
13
+ const forceRefreshAfterAlertCount = 3;
14
+ /** @internal */
15
+ function getIndexHtml() {
16
+ return (fetch('/')
17
+ .then((response) => {
18
+ if (!response.ok)
19
+ return undefined;
20
+ return response.text();
21
+ })
22
+ // If fetch throws a TypeError for some weird reason, also return undefined
23
+ .catch(() => undefined));
24
+ }
25
+ exports.getIndexHtml = getIndexHtml;
26
+ /** @internal */
27
+ function reload() {
28
+ window.location.reload();
29
+ }
30
+ exports.reload = reload;
31
+ /**
32
+ * Peridoically fetches `/` (`index.html`) to check if a new JavaScript bundle has been
33
+ * released.
34
+ *
35
+ * If the bundle has been updated, the user is prompted to refresh the page.
36
+ * After 3 alerts are shown, the page is forcibly refreshed.
37
+ *
38
+ * Don't enable this in development!
39
+ *
40
+ * Your `index.html` must contain a hidden element with the ID `jsBundleHash` that
41
+ * contains a hash of the JavaScript bundle's entry point. Here's how to do this
42
+ * in ASP.NET Core:
43
+ *
44
+ * ```razor
45
+ * var path = $"dist/{bundleName}.js";
46
+ * string jsBundleHash;
47
+ *
48
+ * using (var sha256 = SHA256.Create())
49
+ * {
50
+ * var fullPath = System.IO.Path.Combine("wwwroot", path);
51
+ * using (var readStream = File.OpenRead(fullPath))
52
+ * {
53
+ * var hashBytes = sha256.ComputeHash(readStream);
54
+ * jsBundleHash = WebEncoders.Base64UrlEncode(hashBytes);
55
+ * }
56
+ * }
57
+ *
58
+ * <script src=@path asp-append-version="true"></script>
59
+ * <span id="jsBundleHash" style="display: none">
60
+ * @jsBundleHash
61
+ * </span>
62
+ * ```
63
+ *
64
+ * And example of using `checkForJsBundleUpdateSaga` from your TypeScript code:
65
+ *
66
+ * ```
67
+ * export function* myCheckForJsBundleUpdateSaga(): SagaIterator<void> {
68
+ * if (process.env.NODE_ENV === 'development') return
69
+ *
70
+ * function onError(e: unknown): void {
71
+ * console.error(e)
72
+ *
73
+ * const ierror = processError(e)
74
+ *
75
+ * // Never show the user an error because of this
76
+ * if (shouldLogError(ierror)) {
77
+ * Bugsnag.notify(ierror)
78
+ * }
79
+ * }
80
+ *
81
+ * yield call(checkForJsBundleUpdateSaga, { onError })
82
+ * }
83
+ * ```
84
+ */
85
+ function* checkForJsBundleUpdateSaga({ delayDuration = defaultDelayDuration, onError, }) {
86
+ var _a, _b, _c;
87
+ const jsBundleHash = (_b = (_a = document.getElementById(hashElementId)) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim();
88
+ if (!jsBundleHash) {
89
+ onError(new Error('Could not get jsBundleHash.'));
90
+ return;
91
+ }
92
+ yield (0, effects_1.delay)(delayDuration.asMilliseconds());
93
+ let alertShownCount = 0;
94
+ for (;;) {
95
+ try {
96
+ const indexHtml = (yield (0, effects_1.call)(getIndexHtml));
97
+ if (indexHtml) {
98
+ const document = new DOMParser().parseFromString(indexHtml, 'text/html');
99
+ const hashEl = document.getElementById(hashElementId);
100
+ if (hashEl) {
101
+ // innerText doesn't work here for some reason
102
+ const newJsBundleHash = (_c = hashEl.innerHTML) === null || _c === void 0 ? void 0 : _c.trim();
103
+ if (jsBundleHash !== newJsBundleHash) {
104
+ const content = (react_1.default.createElement("div", null,
105
+ react_1.default.createElement("p", null, "Please save your work and refresh the page."),
106
+ react_1.default.createElement("p", { className: "mb-0" }, "You may encounter errors if you do not refresh the page.")));
107
+ if (alertShownCount >= forceRefreshAfterAlertCount) {
108
+ window.onbeforeunload = null;
109
+ yield (0, effects_1.call)(reload);
110
+ return;
111
+ }
112
+ yield (0, effects_1.call)(iti_react_1.alert, content, { title: 'Website Update Available!' });
113
+ alertShownCount += 1;
114
+ }
115
+ }
116
+ else {
117
+ onError(new Error('Could not get jsBundleHash in fetched document.'));
118
+ }
119
+ }
120
+ }
121
+ catch (e) {
122
+ onError(e);
123
+ }
124
+ yield (0, effects_1.delay)(delayDuration.asMilliseconds());
125
+ }
126
+ }
127
+ exports.checkForJsBundleUpdateSaga = checkForJsBundleUpdateSaga;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkForJsBundleUpdateSaga.js","sourceRoot":"","sources":["../src/checkForJsBundleUpdateSaga.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,MAAM,MAAM,iBAAiB,CAAA;AAEpC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAA;AAEzD,MAAM,aAAa,GAAG,cAAc,CAAA;AACpC,MAAM,oBAAoB,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,SAAS,CAAC,CAAA;AAC1D,MAAM,2BAA2B,GAAG,CAAC,CAAA;AAErC,gBAAgB;AAChB,MAAM,UAAU,YAAY;IACxB,OAAO,CACH,KAAK,CAAC,GAAG,CAAC;SACL,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;QACf,IAAI,CAAC,QAAQ,CAAC,EAAE;YAAE,OAAO,SAAS,CAAA;QAElC,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAA;IAC1B,CAAC,CAAC;QACF,2EAA2E;SAC1E,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAC9B,CAAA;AACL,CAAC;AAED,gBAAgB;AAChB,MAAM,UAAU,MAAM;IAClB,MAAM,CAAC,QAAQ,CAAC,MAAM,EAA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,EAAE,KAAK,EAAE,2BAA2B,EAAE,CAAC,CAAA;wBAClE,eAAe,IAAI,CAAC,CAAA;qBACvB;iBACJ;qBAAM;oBACH,OAAO,CAAC,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC,CAAA;iBACxE;aACJ;SACJ;QAAC,OAAO,CAAC,EAAE;YACR,OAAO,CAAC,CAAC,CAAC,CAAA;SACb;QAED,MAAM,KAAK,CAAC,aAAa,CAAC,cAAc,EAAE,CAAC,CAAA;KAC9C;AACL,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const moment_timezone_1 = __importDefault(require("moment-timezone"));
7
+ const redux_saga_test_plan_1 = require("redux-saga-test-plan");
8
+ const effects_1 = require("redux-saga/effects");
9
+ const iti_react_1 = require("@interface-technologies/iti-react");
10
+ const checkForJsBundleUpdateSaga_1 = require("./checkForJsBundleUpdateSaga");
11
+ jest.mock('@interface-technologies/iti-react');
12
+ // Mock window.location.reload since it not implemented in jsdom
13
+ // eslint-disable-next-line
14
+ delete window.location;
15
+ window.location = { reload: jest.fn() };
16
+ const jsBundleHash = 'hash0';
17
+ const newJsBundleHash = 'hash1';
18
+ beforeEach(() => {
19
+ jest.useRealTimers();
20
+ const jsBundleHashElement = document.createElement('span');
21
+ jsBundleHashElement.innerText = jsBundleHash;
22
+ jsBundleHashElement.id = 'jsBundleHash';
23
+ document.body.appendChild(jsBundleHashElement);
24
+ });
25
+ function getHtml(hash) {
26
+ return `
27
+ <html>
28
+ <head></head>
29
+ <body>
30
+ <span id="jsBundleHash">${hash}</span>
31
+ </body>
32
+ </html>
33
+ `;
34
+ }
35
+ const delayDuration = moment_timezone_1.default.duration(0, 'seconds');
36
+ it('does not show alert if hash matches', async () => {
37
+ const onError = jest.fn();
38
+ await (0, redux_saga_test_plan_1.expectSaga)(checkForJsBundleUpdateSaga_1.checkForJsBundleUpdateSaga, { delayDuration, onError })
39
+ .provide([[(0, effects_1.call)(checkForJsBundleUpdateSaga_1.getIndexHtml), getHtml(jsBundleHash)]])
40
+ .not.call.fn(iti_react_1.alert)
41
+ .not.call.fn(checkForJsBundleUpdateSaga_1.reload)
42
+ .silentRun();
43
+ expect(onError).not.toHaveBeenCalled();
44
+ });
45
+ it('does not call onError if getIndexHtml returns undefined', async () => {
46
+ const onError = jest.fn();
47
+ await (0, redux_saga_test_plan_1.expectSaga)(checkForJsBundleUpdateSaga_1.checkForJsBundleUpdateSaga, { delayDuration, onError })
48
+ .provide([[(0, effects_1.call)(checkForJsBundleUpdateSaga_1.getIndexHtml), undefined]])
49
+ .not.call.fn(iti_react_1.alert)
50
+ .not.call.fn(checkForJsBundleUpdateSaga_1.reload)
51
+ .silentRun();
52
+ expect(onError).not.toHaveBeenCalled();
53
+ });
54
+ it('shows alert several times and then refreshes page', async () => {
55
+ const onError = jest.fn((e) => console.error(e));
56
+ await (0, redux_saga_test_plan_1.expectSaga)(checkForJsBundleUpdateSaga_1.checkForJsBundleUpdateSaga, { delayDuration, onError })
57
+ .provide([[(0, effects_1.call)(checkForJsBundleUpdateSaga_1.getIndexHtml), getHtml(newJsBundleHash)]])
58
+ .call.fn(iti_react_1.alert)
59
+ .call.fn(iti_react_1.alert)
60
+ .call.fn(iti_react_1.alert)
61
+ .call(checkForJsBundleUpdateSaga_1.reload)
62
+ .run();
63
+ expect(onError).not.toHaveBeenCalled();
64
+ expect(window.location.reload).toHaveBeenCalled();
65
+ });
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkForJsBundleUpdateSaga.test.js","sourceRoot":"","sources":["../src/checkForJsBundleUpdateSaga.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,iBAAiB,CAAA;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,I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,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAA;IACtC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,gBAAgB,EAAE,CAAA;AACrD,CAAC,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ export { checkForJsBundleUpdateSaga } from './checkForJsBundleUpdateSaga';
2
+ export type { CheckForJsBundleUpdateSagaOptions } from './checkForJsBundleUpdateSaga';
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkForJsBundleUpdateSaga = void 0;
4
+ var checkForJsBundleUpdateSaga_1 = require("./checkForJsBundleUpdateSaga");
5
+ Object.defineProperty(exports, "checkForJsBundleUpdateSaga", { enumerable: true, get: function () { return checkForJsBundleUpdateSaga_1.checkForJsBundleUpdateSaga; } });
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAA"}
package/package.json CHANGED
@@ -1,70 +1,39 @@
1
1
  {
2
2
  "name": "@interface-technologies/check-for-js-bundle-update-saga",
3
- "version": "2.0.0-alpha.7",
3
+ "version": "4.0.0-alpha.0",
4
4
  "description": "Redux saga for checking if a JavaScript app has been deployed.",
5
- "homepage": "https://bitbucket.org/itidev/iti.react",
5
+ "homepage": "https://github.com/srmagura/iti-react",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "git+https://bitbucket.org/itidev/iti.react.git"
8
+ "url": "git+https://github.com/srmagura/iti-react.git"
9
9
  },
10
10
  "license": "MIT",
11
11
  "author": "Interface Technologies, Inc.",
12
- "main": "src/index.ts",
13
- "scripts": {
14
- "lint": "eslint",
15
- "lint-staged": "lint-staged --no-stash",
16
- "test": "jest",
17
- "tsc": "tsc",
18
- "prettier": "prettier"
19
- },
12
+ "main": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "typedocMain": "src/index.ts",
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "prettier": "@interface-technologies/prettier-config",
20
19
  "dependencies": {
21
- "@types/react": "^17.0.22",
22
- "moment-timezone": "^0.5.33",
20
+ "@types/react": "^17.0.35",
21
+ "moment-timezone": "^0.5.34",
23
22
  "redux-saga": "^1.1.3"
24
23
  },
25
- "peerDependencies": {
26
- "@interface-technologies/iti-react": "^3.0.0-alpha.6",
27
- "@popperjs/core": "^2.10.1",
28
- "bootstrap": "^5.1.1",
29
- "react": "^17.0.2"
30
- },
31
24
  "devDependencies": {
32
- "@babel/core": "^7.15.5",
33
- "@babel/preset-env": "^7.15.6",
34
- "@babel/preset-react": "^7.14.5",
35
- "@babel/preset-typescript": "^7.15.0",
36
- "@interface-technologies/eslint-config": "0.3.0",
37
- "@interface-technologies/iti-react": "3.0.0-alpha.6",
38
- "@interface-technologies/prettier-config": "0.1.0",
39
- "@interface-technologies/tsconfig": "0.1.1",
25
+ "@interface-technologies/iti-react": "4.0.0-alpha.0",
26
+ "@interface-technologies/tsconfig": "4.0.0-alpha.0",
40
27
  "@redux-saga/is": "^1.1.2",
41
28
  "@redux-saga/symbols": "^1.1.2",
42
29
  "@testing-library/react-hooks": "^7.0.2",
43
- "@types/jest": "27.0.1",
44
- "@typescript-eslint/eslint-plugin": "^4.31.2",
45
- "@typescript-eslint/parser": "^4.31.2",
46
- "babel-jest": "^27.2.1",
47
- "eslint": "^7.32.0",
48
- "eslint-config-airbnb": "^18.2.1",
49
- "eslint-config-airbnb-typescript": "^14.0.0",
50
- "eslint-config-prettier": "^8.3.0",
51
- "eslint-import-resolver-typescript": "^2.5.0",
52
- "eslint-plugin-import": "^2.24.2",
53
- "eslint-plugin-jsx-a11y": "^6.4.1",
54
- "eslint-plugin-promise": "^5.1.0",
55
- "eslint-plugin-react": "^7.25.3",
56
- "eslint-plugin-react-hooks": "^4.2.0",
57
- "eslint-plugin-redux-saga": "^1.2.1",
58
- "jest": "^27.2.1",
59
- "jest-haste-map": "^27.2.0",
60
- "jest-resolve": "^27.2.0",
61
- "lint-staged": "^11.1.2",
62
- "prettier": "^2.4.1",
63
30
  "react": "^17.0.2",
64
- "react-test-renderer": "^17.0.2",
65
- "redux-saga-test-plan": "^4.0.3",
66
- "ts-jest": "^27.0.5",
67
- "typescript": "~4.4.3"
31
+ "redux-saga-test-plan": "^4.0.4"
68
32
  },
69
- "prettier": "@interface-technologies/prettier-config"
33
+ "peerDependencies": {
34
+ "@interface-technologies/iti-react": "^4.0.0-alpha.0",
35
+ "@popperjs/core": "^2.10.2",
36
+ "bootstrap": "^5.1.2",
37
+ "react": "^17.0.2"
38
+ }
70
39
  }
package/babel.config.js DELETED
@@ -1,10 +0,0 @@
1
- // For Jest tests only
2
- module.exports = {
3
- presets: [
4
- ['@babel/preset-env', { targets: { node: 'current' } }],
5
- '@babel/preset-typescript',
6
- ["@babel/preset-react", {
7
- "runtime": "automatic"
8
- }]
9
- ],
10
- }
@@ -1,17 +0,0 @@
1
- const micromatch = require('micromatch')
2
-
3
- module.exports = {
4
- '*.ts?(x)': files => {
5
- const tasks = []
6
-
7
- // Don't lint files in .eslintignore
8
- notIgnored = micromatch.not(files, '**/__tests__/**/*')
9
-
10
- if (notIgnored.length > 0) {
11
- tasks.push(`eslint --fix --max-warnings 0 ${notIgnored.join(' ')}`)
12
- }
13
-
14
- tasks.push(`prettier --write ${files.join(' ')}`)
15
- return tasks
16
- }
17
- }
@@ -1,145 +0,0 @@
1
- import moment from 'moment-timezone'
2
- import { SagaIterator } from 'redux-saga'
3
- import { delay, call } from 'redux-saga/effects'
4
- import { alert } from '@interface-technologies/iti-react'
5
-
6
- const hashElementId = 'jsBundleHash'
7
- const defaultDelayDuration = moment.duration(4, 'minutes')
8
- const forceRefreshAfterAlertCount = 3
9
-
10
- /** @internal */
11
- export function getIndexHtml(): Promise<string | undefined> {
12
- return (
13
- fetch('/')
14
- .then((response) => {
15
- if (!response.ok) return undefined
16
-
17
- return response.text()
18
- })
19
- // If fetch throws a TypeError for some weird reason, also return undefined
20
- .catch(() => undefined)
21
- )
22
- }
23
-
24
- /** @internal */
25
- export function reload(): void {
26
- window.location.reload()
27
- }
28
-
29
- export interface CheckForJsBundleUpdateSagaOptions {
30
- delayDuration?: moment.Duration
31
- onError(e: unknown): void
32
- }
33
-
34
- /**
35
- * Peridoically fetches `/` (`index.html`) to check if a new JavaScript bundle has been
36
- * released.
37
- *
38
- * If the bundle has been updated, the user is prompted to refresh the page.
39
- * After 3 alerts are shown, the page is forcibly refreshed.
40
- *
41
- * Don't enable this in development!
42
- *
43
- * Your `index.html` must contain a hidden element with the ID `jsBundleHash` that
44
- * contains a hash of the JavaScript bundle's entry point. Here's how to do this
45
- * in ASP.NET Core:
46
- *
47
- * # TODO:SAM change langauge to razor once shiki bug fix released (https://github.com/shikijs/shiki/issues/219)
48
- * ```text
49
- * var path = $"dist/{bundleName}.js";
50
- * string jsBundleHash;
51
- *
52
- * using (var sha256 = SHA256.Create())
53
- * {
54
- * var fullPath = System.IO.Path.Combine("wwwroot", path);
55
- * using (var readStream = File.OpenRead(fullPath))
56
- * {
57
- * var hashBytes = sha256.ComputeHash(readStream);
58
- * jsBundleHash = WebEncoders.Base64UrlEncode(hashBytes);
59
- * }
60
- * }
61
- *
62
- * <script src=@path asp-append-version="true"></script>
63
- * <span id="jsBundleHash" style="display: none">
64
- * @jsBundleHash
65
- * </span>
66
- * ```
67
- *
68
- * And example of using `checkForJsBundleUpdateSaga` from your TypeScript code:
69
- *
70
- * ```
71
- * export function* myCheckForJsBundleUpdateSaga(): SagaIterator<void> {
72
- * if ((window as unknown as WindowWithGlobals).isDebug) return
73
- *
74
- * function onError(e: unknown): void {
75
- * console.error(e)
76
- *
77
- * const ierror = processError(e)
78
- *
79
- * // Never show the user an error because of this
80
- * if (shouldLogError(ierror)) {
81
- * Bugsnag.notify(ierror)
82
- * }
83
- * }
84
- *
85
- * yield call(checkForJsBundleUpdateSaga, { onError })
86
- * }
87
- * ```
88
- */
89
- export function* checkForJsBundleUpdateSaga({
90
- delayDuration = defaultDelayDuration,
91
- onError,
92
- }: CheckForJsBundleUpdateSagaOptions): SagaIterator<void> {
93
- const jsBundleHash = document.getElementById(hashElementId)?.innerText?.trim()
94
-
95
- if (!jsBundleHash) {
96
- onError(new Error('Could not get jsBundleHash.'))
97
- return
98
- }
99
-
100
- yield delay(delayDuration.asMilliseconds())
101
-
102
- let alertShownCount = 0
103
-
104
- for (;;) {
105
- try {
106
- const indexHtml = (yield call(getIndexHtml)) as string | undefined
107
- if (indexHtml) {
108
- const document = new DOMParser().parseFromString(indexHtml, 'text/html')
109
- const hashEl = document.getElementById(hashElementId)
110
-
111
- if (hashEl) {
112
- // innerText doesn't work here for some reason
113
- const newJsBundleHash = hashEl.innerHTML?.trim()
114
-
115
- if (jsBundleHash !== newJsBundleHash) {
116
- const content = (
117
- <div>
118
- <p>Please save your work and refresh the page.</p>
119
- <p className="mb-0">
120
- You may encounter errors if you do not refresh the
121
- page.
122
- </p>
123
- </div>
124
- )
125
-
126
- if (alertShownCount >= forceRefreshAfterAlertCount) {
127
- window.onbeforeunload = null
128
- yield call(reload)
129
- return
130
- }
131
-
132
- yield call(alert, content, { title: 'Website Update Available!' })
133
- alertShownCount++
134
- }
135
- } else {
136
- onError(new Error('Could not get jsBundleHash in fetched document.'))
137
- }
138
- }
139
- } catch (e) {
140
- onError(e)
141
- }
142
-
143
- yield delay(delayDuration.asMilliseconds())
144
- }
145
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { checkForJsBundleUpdateSaga } from './CheckForJsBundleUpdateSaga'
2
- export type { CheckForJsBundleUpdateSagaOptions } from './CheckForJsBundleUpdateSaga'