@serwist/window 8.0.0 → 8.0.1
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/dist/Serwist.d.ts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/messageSW.d.ts +0 -1
- package/dist/utils/SerwistEvent.d.ts +0 -1
- package/dist/utils/SerwistEventTarget.d.ts +0 -1
- package/dist/utils/urlsMatch.d.ts +0 -1
- package/package.json +7 -3
- package/.turbo/turbo-build.log +0 -10
- package/.turbo/turbo-lint.log +0 -4
- package/.turbo/turbo-typecheck.log +0 -4
- package/CHANGELOG.md +0 -33
- package/dist/Serwist.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/messageSW.d.ts.map +0 -1
- package/dist/utils/SerwistEvent.d.ts.map +0 -1
- package/dist/utils/SerwistEventTarget.d.ts.map +0 -1
- package/dist/utils/urlsMatch.d.ts.map +0 -1
- package/rollup.config.js +0 -27
- package/src/Serwist.ts +0 -686
- package/src/index.ts +0 -15
- package/src/messageSW.ts +0 -34
- package/src/utils/SerwistEvent.ts +0 -57
- package/src/utils/SerwistEventTarget.ts +0 -68
- package/src/utils/urlsMatch.ts +0 -21
- package/tsconfig.json +0 -8
package/dist/Serwist.d.ts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/messageSW.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/window",
|
|
3
|
-
"version": "8.0.
|
|
3
|
+
"version": "8.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Simplifies communications with Serwist packages running in the service worker",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"!dist/**/dts"
|
|
9
|
+
],
|
|
6
10
|
"keywords": [
|
|
7
11
|
"serwist",
|
|
8
12
|
"serwistjs",
|
|
@@ -30,11 +34,11 @@
|
|
|
30
34
|
},
|
|
31
35
|
"dependencies": {
|
|
32
36
|
"@types/trusted-types": "2.0.4",
|
|
33
|
-
"@serwist/core": "8.0.
|
|
37
|
+
"@serwist/core": "8.0.1"
|
|
34
38
|
},
|
|
35
39
|
"devDependencies": {
|
|
36
40
|
"rollup": "3.28.1",
|
|
37
|
-
"@serwist/constants": "8.0.
|
|
41
|
+
"@serwist/constants": "8.0.1"
|
|
38
42
|
},
|
|
39
43
|
"scripts": {
|
|
40
44
|
"build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
|
package/.turbo/turbo-build.log
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @serwist/window@8.0.0 build /home/runner/work/serwist/serwist/packages/window
|
|
3
|
-
> rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js
|
|
4
|
-
|
|
5
|
-
(node:2592) ExperimentalWarning: Import assertions are not a stable feature of the JavaScript language. Avoid relying on their current behavior and syntax as those might change in a future version of Node.js.
|
|
6
|
-
(Use `node --trace-warnings ...` to show where the warning was created)
|
|
7
|
-
(node:2592) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
|
|
8
|
-
[36m
|
|
9
|
-
[1msrc/index.ts[22m → [1mdist/index.old.cjs, dist/index.js[22m...[39m
|
|
10
|
-
[32mcreated [1mdist/index.old.cjs, dist/index.js[22m in [1m7.8s[22m[39m
|
package/.turbo/turbo-lint.log
DELETED
package/CHANGELOG.md
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
# @serwist/window
|
|
2
|
-
|
|
3
|
-
## 8.0.0
|
|
4
|
-
|
|
5
|
-
### Major Changes
|
|
6
|
-
|
|
7
|
-
- [`e0313f0`](https://github.com/serwist/serwist/commit/e0313f02f661a07ccbe9edc64e44e1af6136c73e) Thanks [@DuCanhGH](https://github.com/DuCanhGH)! - chore: initial release
|
|
8
|
-
|
|
9
|
-
- Reimagined `@serwist/next`.
|
|
10
|
-
- Removed various options.
|
|
11
|
-
- Removed `aggressiveFrontEndNavCaching` to reassess its usefulness.
|
|
12
|
-
- Removed `browserslist`. Use `swcEnvTargets` instead (TODO: add this option).
|
|
13
|
-
- Custom workers have been removed. You should use `swSrc`.
|
|
14
|
-
- Removed `extendDefaultRuntimeCaching`. Use the spread syntax instead (use `import { defaultCache } from "@serwist/next/browser"` to import the default runtimeCaching array).
|
|
15
|
-
- Temporarily removed `fallbacks` to investigate module-friendly alternatives.
|
|
16
|
-
- Removed `swcMinify`.
|
|
17
|
-
- Removed `watchWorkersInDev`.
|
|
18
|
-
- Removed `cacheStartUrl`, `dynamicStartUrl`, and `dynamicStartUrl`. These shall be re-added only when their use cases are made more clear to me.
|
|
19
|
-
- Merged `workboxOptions` with the plugin's options.
|
|
20
|
-
- Removed `swc-loader`, `terser-minify`, `webpack-builders`,... (we now leverage `ChildCompilationPlugin` to compile workers - this change will be backported to `@ducanh2912/next-pwa@10`)
|
|
21
|
-
- Removed the ability to use GenerateSW. `@serwist/sw.installSerwist` is provided as a replacement.
|
|
22
|
-
- `swSrc` is now a required property.
|
|
23
|
-
- Moved minimum support Next.js version from `11.0.0` to `14.0.0`.
|
|
24
|
-
- Removed GenerateSW (replaced by `@serwist/sw.installSerwist`).
|
|
25
|
-
- See `examples/next-basic/app/sw.ts` to see how `installSerwist` should be used.
|
|
26
|
-
- Repurposed `@serwist/sw`.
|
|
27
|
-
- The old package might be reintroduced if there's demand.
|
|
28
|
-
- **Note:** This is just the initial release, and there is still a lot of ground to cover (a lot of legacy code to be removed, a lot of features to be reintroduced,...). Here's to a bright future for the project :\_)
|
|
29
|
-
|
|
30
|
-
### Patch Changes
|
|
31
|
-
|
|
32
|
-
- Updated dependencies [[`e0313f0`](https://github.com/serwist/serwist/commit/e0313f02f661a07ccbe9edc64e44e1af6136c73e)]:
|
|
33
|
-
- @serwist/core@8.0.0
|
package/dist/Serwist.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Serwist.d.ts","sourceRoot":"","sources":["../src/Serwist.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAK1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AAgBnE;;;;;;;;;;GAUG;AACH,qBAAa,OAAQ,SAAQ,kBAAkB;IAC7C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA4B;IACvD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA2B;IAC5D,OAAO,CAAC,iBAAiB,CAAK;IAG9B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA2C;IACvE,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA2C;IAC3E,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAA2C;IAEhF,OAAO,CAAC,iBAAiB,CAA0B;IACnD,OAAO,CAAC,SAAS,CAAC,CAAU;IAC5B,OAAO,CAAC,wBAAwB,CAAC,CAAgB;IACjD,OAAO,CAAC,aAAa,CAAC,CAA4B;IAClD,OAAO,CAAC,GAAG,CAAC,CAAgB;IAC5B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiC;IACzD,OAAO,CAAC,WAAW,CAAC,CAAgB;IACpC,OAAO,CAAC,eAAe,CAAC,CAAS;IAEjC;;;;;;;;;;OAUG;gBAES,SAAS,EAAE,MAAM,GAAG,gBAAgB,EAAE,eAAe,GAAE,EAAO;IAY1E;;;;;;OAMG;IACG,QAAQ,CAAC,EACb,SAAiB,GAClB,GAAE;QACD;;;WAGG;QACH,SAAS,CAAC,EAAE,OAAO,CAAC;KAChB,GAAG,OAAO,CAAC,yBAAyB,GAAG,SAAS,CAAC;IAkGvD;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAY7B;;;;;;;;OAQG;IACH,IAAI,MAAM,IAAI,OAAO,CAAC,aAAa,CAAC,CAEnC;IAED;;;;;;;;;;;OAWG;IACH,IAAI,WAAW,IAAI,OAAO,CAAC,aAAa,CAAC,CAExC;IAED;;;;;;;;;;;;;;OAcG;IACH,KAAK,IAAI,OAAO,CAAC,aAAa,CAAC;IAM/B;;;;;;;;;;;;OAYG;IAGG,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;IAK3C;;;;;;OAMG;IACH,kBAAkB,IAAI,IAAI;IAM1B;;;;;;OAMG;IACH,OAAO,CAAC,6BAA6B;IASrC;;;;;OAKG;YACW,eAAe;IAsB7B;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,cAAc,CAgE7B;IAEF;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,cAAc,CAqF7B;IAEF;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAuBlC;IAEF;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,UAAU,CA2BzB;CACH;AAKD;;;;;;;;;;;GAWG;AAEH;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH;;;;;;;;;;;;;;;;;GAiBG;AAEH;;;;;;;;;;;;;;;GAeG"}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC;AAG9B,cAAc,yBAAyB,CAAC"}
|
package/dist/messageSW.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"messageSW.d.ts","sourceRoot":"","sources":["../src/messageSW.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;;;GAYG;AAGH,iBAAS,SAAS,CAAC,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,CAQ5D;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SerwistEvent.d.ts","sourceRoot":"","sources":["../../src/utils/SerwistEvent.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAElE;;;;;GAKG;AACH,qBAAa,YAAY,CAAC,CAAC,SAAS,MAAM,eAAe;IAO9C,IAAI,EAAE,CAAC;IANhB,MAAM,CAAC,EAAE,kBAAkB,CAAC;IAC5B,EAAE,CAAC,EAAE,aAAa,CAAC;IACnB,aAAa,CAAC,EAAE,KAAK,CAAC;IACtB,UAAU,CAAC,EAAE,OAAO,CAAC;gBAGZ,IAAI,EAAE,CAAC,EACd,KAAK,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC;CAIrD;AAED,MAAM,WAAW,mBAAoB,SAAQ,YAAY,CAAC,SAAS,CAAC;IAClE,IAAI,EAAE,GAAG,CAAC;IACV,aAAa,EAAE,KAAK,CAAC;IACrB,KAAK,EAAE,SAAS,WAAW,EAAE,CAAC;CAC/B;AAED,MAAM,WAAW,qBAAsB,SAAQ,YAAY,CAAC,MAAM,wBAAwB,CAAC;IACzF,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,4BAA6B,SAAQ,qBAAqB;IACzE,wBAAwB,CAAC,EAAE,OAAO,CAAC;CACpC;AAED,MAAM,WAAW,wBAAwB;IACvC,UAAU,EAAE,qBAAqB,CAAC;IAClC,SAAS,EAAE,qBAAqB,CAAC;IACjC,OAAO,EAAE,4BAA4B,CAAC;IACtC,UAAU,EAAE,qBAAqB,CAAC;IAClC,SAAS,EAAE,qBAAqB,CAAC;IACjC,WAAW,EAAE,qBAAqB,CAAC;IACnC,SAAS,EAAE,qBAAqB,CAAC;CAClC;AAED,MAAM,WAAW,eAAgB,SAAQ,wBAAwB;IAC/D,OAAO,EAAE,mBAAmB,CAAC;CAC9B"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SerwistEventTarget.d.ts","sourceRoot":"","sources":["../../src/utils/SerwistEventTarget.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEvE,MAAM,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,YAAY,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC;AAEjE;;;;;GAKG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAAgE;IAEvG;;;;OAIG;IACH,gBAAgB,CAAC,CAAC,SAAS,MAAM,eAAe,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,KAAK,GAAG,GAAG,IAAI;IAK9G;;;;OAIG;IACH,mBAAmB,CAAC,CAAC,SAAS,MAAM,eAAe,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,KAAK,GAAG,GAAG,IAAI;IAIjH;;;OAGG;IACH,aAAa,CAAC,KAAK,EAAE,YAAY,CAAC,GAAG,CAAC,GAAG,IAAI;IAS7C;;;;;;;OAOG;IACH,OAAO,CAAC,wBAAwB;CAMjC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"urlsMatch.d.ts","sourceRoot":"","sources":["../../src/utils/urlsMatch.ts"],"names":[],"mappings":"AAQA;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAG7D"}
|
package/rollup.config.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
import { getRollupOptions } from "@serwist/constants/rollup";
|
|
3
|
-
|
|
4
|
-
import packageJson from "./package.json" assert { type: "json" };
|
|
5
|
-
|
|
6
|
-
const isDev = process.env.NODE_ENV === "development";
|
|
7
|
-
|
|
8
|
-
export default getRollupOptions({
|
|
9
|
-
packageJson,
|
|
10
|
-
jsFiles: [
|
|
11
|
-
{
|
|
12
|
-
input: "src/index.ts",
|
|
13
|
-
output: [
|
|
14
|
-
{
|
|
15
|
-
file: "dist/index.old.cjs",
|
|
16
|
-
format: "cjs",
|
|
17
|
-
exports: "named",
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
file: "dist/index.js",
|
|
21
|
-
format: "esm",
|
|
22
|
-
},
|
|
23
|
-
],
|
|
24
|
-
},
|
|
25
|
-
],
|
|
26
|
-
shouldEmitDeclaration: !isDev,
|
|
27
|
-
});
|
package/src/Serwist.ts
DELETED
|
@@ -1,686 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { Deferred, dontWaitFor, logger } from "@serwist/core/internal";
|
|
10
|
-
import type { TrustedScriptURL } from "trusted-types/lib";
|
|
11
|
-
|
|
12
|
-
import { messageSW } from "./messageSW.js";
|
|
13
|
-
import type { SerwistLifecycleEventMap } from "./utils/SerwistEvent.js";
|
|
14
|
-
import { SerwistEvent } from "./utils/SerwistEvent.js";
|
|
15
|
-
import { SerwistEventTarget } from "./utils/SerwistEventTarget.js";
|
|
16
|
-
import { urlsMatch } from "./utils/urlsMatch.js";
|
|
17
|
-
|
|
18
|
-
// The time a SW must be in the waiting phase before we can conclude
|
|
19
|
-
// `skipWaiting()` wasn't called. This 200 amount wasn't scientifically
|
|
20
|
-
// chosen, but it seems to avoid false positives in my testing.
|
|
21
|
-
const WAITING_TIMEOUT_DURATION = 200;
|
|
22
|
-
|
|
23
|
-
// The amount of time after a registration that we can reasonably conclude
|
|
24
|
-
// that the registration didn't trigger an update.
|
|
25
|
-
const REGISTRATION_TIMEOUT_DURATION = 60000;
|
|
26
|
-
|
|
27
|
-
// The de facto standard message that a service worker should be listening for
|
|
28
|
-
// to trigger a call to skipWaiting().
|
|
29
|
-
const SKIP_WAITING_MESSAGE = { type: "SKIP_WAITING" };
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* A class to aid in handling service worker registration, updates, and
|
|
33
|
-
* reacting to service worker lifecycle events.
|
|
34
|
-
*
|
|
35
|
-
* @fires `@serwist/window.Serwist.message`
|
|
36
|
-
* @fires `@serwist/window.Serwist.installed`
|
|
37
|
-
* @fires `@serwist/window.Serwist.waiting`
|
|
38
|
-
* @fires `@serwist/window.Serwist.controlling`
|
|
39
|
-
* @fires `@serwist/window.Serwist.activated`
|
|
40
|
-
* @fires `@serwist/window.Serwist.redundant`
|
|
41
|
-
*/
|
|
42
|
-
export class Serwist extends SerwistEventTarget {
|
|
43
|
-
private readonly _scriptURL: string | TrustedScriptURL;
|
|
44
|
-
private readonly _registerOptions: RegistrationOptions = {};
|
|
45
|
-
private _updateFoundCount = 0;
|
|
46
|
-
|
|
47
|
-
// Deferreds we can resolve later.
|
|
48
|
-
private readonly _swDeferred: Deferred<ServiceWorker> = new Deferred();
|
|
49
|
-
private readonly _activeDeferred: Deferred<ServiceWorker> = new Deferred();
|
|
50
|
-
private readonly _controllingDeferred: Deferred<ServiceWorker> = new Deferred();
|
|
51
|
-
|
|
52
|
-
private _registrationTime: DOMHighResTimeStamp = 0;
|
|
53
|
-
private _isUpdate?: boolean;
|
|
54
|
-
private _compatibleControllingSW?: ServiceWorker;
|
|
55
|
-
private _registration?: ServiceWorkerRegistration;
|
|
56
|
-
private _sw?: ServiceWorker;
|
|
57
|
-
private readonly _ownSWs: Set<ServiceWorker> = new Set();
|
|
58
|
-
private _externalSW?: ServiceWorker;
|
|
59
|
-
private _waitingTimeout?: number;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Creates a new Serwist instance with a script URL and service worker
|
|
63
|
-
* options. The script URL and options are the same as those used when
|
|
64
|
-
* calling [navigator.serviceWorker.register(scriptURL, options)](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register).
|
|
65
|
-
*
|
|
66
|
-
* @param scriptURL The service worker script
|
|
67
|
-
* associated with this instance. Using a
|
|
68
|
-
* [`TrustedScriptURL`](https://web.dev/trusted-types/) is supported.
|
|
69
|
-
* @param registerOptions The service worker options associated
|
|
70
|
-
* with this instance.
|
|
71
|
-
*/
|
|
72
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
73
|
-
constructor(scriptURL: string | TrustedScriptURL, registerOptions: {} = {}) {
|
|
74
|
-
super();
|
|
75
|
-
|
|
76
|
-
this._scriptURL = scriptURL;
|
|
77
|
-
this._registerOptions = registerOptions;
|
|
78
|
-
|
|
79
|
-
// Add a message listener immediately since messages received during
|
|
80
|
-
// page load are buffered only until the DOMContentLoaded event:
|
|
81
|
-
// https://github.com/GoogleChrome/workbox/issues/2202
|
|
82
|
-
navigator.serviceWorker.addEventListener("message", this._onMessage);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Registers a service worker for this instances script URL and service
|
|
87
|
-
* worker options. By default this method delays registration until after
|
|
88
|
-
* the window has loaded.
|
|
89
|
-
*
|
|
90
|
-
* @param options
|
|
91
|
-
*/
|
|
92
|
-
async register({
|
|
93
|
-
immediate = false,
|
|
94
|
-
}: {
|
|
95
|
-
/**
|
|
96
|
-
* Setting this to true will register the service worker immediately,
|
|
97
|
-
* even if the window has not loaded (not recommended).
|
|
98
|
-
*/
|
|
99
|
-
immediate?: boolean;
|
|
100
|
-
} = {}): Promise<ServiceWorkerRegistration | undefined> {
|
|
101
|
-
if (process.env.NODE_ENV !== "production") {
|
|
102
|
-
if (this._registrationTime) {
|
|
103
|
-
logger.error("Cannot re-register a Serwist instance after it has " + "been registered. Create a new instance instead.");
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (!immediate && document.readyState !== "complete") {
|
|
109
|
-
await new Promise((res) => window.addEventListener("load", res));
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Set this flag to true if any service worker was controlling the page
|
|
113
|
-
// at registration time.
|
|
114
|
-
this._isUpdate = Boolean(navigator.serviceWorker.controller);
|
|
115
|
-
|
|
116
|
-
// Before registering, attempt to determine if a SW is already controlling
|
|
117
|
-
// the page, and if that SW script (and version, if specified) matches this
|
|
118
|
-
// instance's script.
|
|
119
|
-
this._compatibleControllingSW = this._getControllingSWIfCompatible();
|
|
120
|
-
|
|
121
|
-
this._registration = await this._registerScript();
|
|
122
|
-
|
|
123
|
-
// If we have a compatible controller, store the controller as the "own"
|
|
124
|
-
// SW, resolve active/controlling deferreds and add necessary listeners.
|
|
125
|
-
if (this._compatibleControllingSW) {
|
|
126
|
-
this._sw = this._compatibleControllingSW;
|
|
127
|
-
this._activeDeferred.resolve(this._compatibleControllingSW);
|
|
128
|
-
this._controllingDeferred.resolve(this._compatibleControllingSW);
|
|
129
|
-
|
|
130
|
-
this._compatibleControllingSW.addEventListener("statechange", this._onStateChange, { once: true });
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// If there's a waiting service worker with a matching URL before the
|
|
134
|
-
// `updatefound` event fires, it likely means that this site is open
|
|
135
|
-
// in another tab, or the user refreshed the page (and thus the previous
|
|
136
|
-
// page wasn't fully unloaded before this page started loading).
|
|
137
|
-
// https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#waiting
|
|
138
|
-
const waitingSW = this._registration.waiting;
|
|
139
|
-
if (waitingSW && urlsMatch(waitingSW.scriptURL, this._scriptURL.toString())) {
|
|
140
|
-
// Store the waiting SW as the "own" Sw, even if it means overwriting
|
|
141
|
-
// a compatible controller.
|
|
142
|
-
this._sw = waitingSW;
|
|
143
|
-
|
|
144
|
-
// Run this in the next microtask, so any code that adds an event
|
|
145
|
-
// listener after awaiting `register()` will get this event.
|
|
146
|
-
dontWaitFor(
|
|
147
|
-
Promise.resolve().then(() => {
|
|
148
|
-
this.dispatchEvent(
|
|
149
|
-
new SerwistEvent("waiting", {
|
|
150
|
-
sw: waitingSW,
|
|
151
|
-
wasWaitingBeforeRegister: true,
|
|
152
|
-
})
|
|
153
|
-
);
|
|
154
|
-
if (process.env.NODE_ENV !== "production") {
|
|
155
|
-
logger.warn("A service worker was already waiting to activate " + "before this script was registered...");
|
|
156
|
-
}
|
|
157
|
-
})
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// If an "own" SW is already set, resolve the deferred.
|
|
162
|
-
if (this._sw) {
|
|
163
|
-
this._swDeferred.resolve(this._sw);
|
|
164
|
-
this._ownSWs.add(this._sw);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (process.env.NODE_ENV !== "production") {
|
|
168
|
-
logger.log("Successfully registered service worker.", this._scriptURL.toString());
|
|
169
|
-
|
|
170
|
-
if (navigator.serviceWorker.controller) {
|
|
171
|
-
if (this._compatibleControllingSW) {
|
|
172
|
-
logger.debug("A service worker with the same script URL " + "is already controlling this page.");
|
|
173
|
-
} else {
|
|
174
|
-
logger.debug(
|
|
175
|
-
"A service worker with a different script URL is " +
|
|
176
|
-
"currently controlling the page. The browser is now fetching " +
|
|
177
|
-
"the new script now..."
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const currentPageIsOutOfScope = () => {
|
|
183
|
-
const scopeURL = new URL(this._registerOptions.scope || this._scriptURL.toString(), document.baseURI);
|
|
184
|
-
const scopeURLBasePath = new URL("./", scopeURL.href).pathname;
|
|
185
|
-
return !location.pathname.startsWith(scopeURLBasePath);
|
|
186
|
-
};
|
|
187
|
-
if (currentPageIsOutOfScope()) {
|
|
188
|
-
logger.warn("The current page is not in scope for the registered " + "service worker. Was this a mistake?");
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
this._registration.addEventListener("updatefound", this._onUpdateFound);
|
|
193
|
-
navigator.serviceWorker.addEventListener("controllerchange", this._onControllerChange);
|
|
194
|
-
|
|
195
|
-
return this._registration;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Checks for updates of the registered service worker.
|
|
200
|
-
*/
|
|
201
|
-
async update(): Promise<void> {
|
|
202
|
-
if (!this._registration) {
|
|
203
|
-
if (process.env.NODE_ENV !== "production") {
|
|
204
|
-
logger.error("Cannot update a Serwist instance without " + "being registered. Register the Serwist instance first.");
|
|
205
|
-
}
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// Try to update registration
|
|
210
|
-
await this._registration.update();
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Resolves to the service worker registered by this instance as soon as it
|
|
215
|
-
* is active. If a service worker was already controlling at registration
|
|
216
|
-
* time then it will resolve to that if the script URLs (and optionally
|
|
217
|
-
* script versions) match, otherwise it will wait until an update is found
|
|
218
|
-
* and activates.
|
|
219
|
-
*
|
|
220
|
-
* @returns
|
|
221
|
-
*/
|
|
222
|
-
get active(): Promise<ServiceWorker> {
|
|
223
|
-
return this._activeDeferred.promise;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Resolves to the service worker registered by this instance as soon as it
|
|
228
|
-
* is controlling the page. If a service worker was already controlling at
|
|
229
|
-
* registration time then it will resolve to that if the script URLs (and
|
|
230
|
-
* optionally script versions) match, otherwise it will wait until an update
|
|
231
|
-
* is found and starts controlling the page.
|
|
232
|
-
* Note: the first time a service worker is installed it will active but
|
|
233
|
-
* not start controlling the page unless `clients.claim()` is called in the
|
|
234
|
-
* service worker.
|
|
235
|
-
*
|
|
236
|
-
* @returns
|
|
237
|
-
*/
|
|
238
|
-
get controlling(): Promise<ServiceWorker> {
|
|
239
|
-
return this._controllingDeferred.promise;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Resolves with a reference to a service worker that matches the script URL
|
|
244
|
-
* of this instance, as soon as it's available.
|
|
245
|
-
*
|
|
246
|
-
* If, at registration time, there's already an active or waiting service
|
|
247
|
-
* worker with a matching script URL, it will be used (with the waiting
|
|
248
|
-
* service worker taking precedence over the active service worker if both
|
|
249
|
-
* match, since the waiting service worker would have been registered more
|
|
250
|
-
* recently).
|
|
251
|
-
* If there's no matching active or waiting service worker at registration
|
|
252
|
-
* time then the promise will not resolve until an update is found and starts
|
|
253
|
-
* installing, at which point the installing service worker is used.
|
|
254
|
-
*
|
|
255
|
-
* @returns
|
|
256
|
-
*/
|
|
257
|
-
getSW(): Promise<ServiceWorker> {
|
|
258
|
-
// If `this._sw` is set, resolve with that as we want `getSW()` to
|
|
259
|
-
// return the correct (new) service worker if an update is found.
|
|
260
|
-
return this._sw !== undefined ? Promise.resolve(this._sw) : this._swDeferred.promise;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Sends the passed data object to the service worker registered by this
|
|
265
|
-
* instance (via `@serwist/window.Serwist.getSW`) and resolves
|
|
266
|
-
* with a response (if any).
|
|
267
|
-
*
|
|
268
|
-
* A response can be set in a message handler in the service worker by
|
|
269
|
-
* calling `event.ports[0].postMessage(...)`, which will resolve the promise
|
|
270
|
-
* returned by `messageSW()`. If no response is set, the promise will never
|
|
271
|
-
* resolve.
|
|
272
|
-
*
|
|
273
|
-
* @param data An object to send to the service worker
|
|
274
|
-
* @returns
|
|
275
|
-
*/
|
|
276
|
-
// We might be able to change the 'data' type to Record<string, unknown> in the future.
|
|
277
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
278
|
-
async messageSW(data: object): Promise<any> {
|
|
279
|
-
const sw = await this.getSW();
|
|
280
|
-
return messageSW(sw, data);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Sends a `{type: 'SKIP_WAITING'}` message to the service worker that's
|
|
285
|
-
* currently in the `waiting` state associated with the current registration.
|
|
286
|
-
*
|
|
287
|
-
* If there is no current registration or no service worker is `waiting`,
|
|
288
|
-
* calling this will have no effect.
|
|
289
|
-
*/
|
|
290
|
-
messageSkipWaiting(): void {
|
|
291
|
-
if (this._registration && this._registration.waiting) {
|
|
292
|
-
void messageSW(this._registration.waiting, SKIP_WAITING_MESSAGE);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Checks for a service worker already controlling the page and returns
|
|
298
|
-
* it if its script URL matches.
|
|
299
|
-
*
|
|
300
|
-
* @private
|
|
301
|
-
* @returns
|
|
302
|
-
*/
|
|
303
|
-
private _getControllingSWIfCompatible() {
|
|
304
|
-
const controller = navigator.serviceWorker.controller;
|
|
305
|
-
if (controller && urlsMatch(controller.scriptURL, this._scriptURL.toString())) {
|
|
306
|
-
return controller;
|
|
307
|
-
} else {
|
|
308
|
-
return undefined;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Registers a service worker for this instances script URL and register
|
|
314
|
-
* options and tracks the time registration was complete.
|
|
315
|
-
*
|
|
316
|
-
* @private
|
|
317
|
-
*/
|
|
318
|
-
private async _registerScript() {
|
|
319
|
-
try {
|
|
320
|
-
// this._scriptURL may be a TrustedScriptURL, but there's no support for
|
|
321
|
-
// passing that to register() in lib.dom right now.
|
|
322
|
-
// https://github.com/GoogleChrome/workbox/issues/2855
|
|
323
|
-
const reg = await navigator.serviceWorker.register(this._scriptURL as string, this._registerOptions);
|
|
324
|
-
|
|
325
|
-
// Keep track of when registration happened, so it can be used in the
|
|
326
|
-
// `this._onUpdateFound` heuristic. Also use the presence of this
|
|
327
|
-
// property as a way to see if `.register()` has been called.
|
|
328
|
-
this._registrationTime = performance.now();
|
|
329
|
-
|
|
330
|
-
return reg;
|
|
331
|
-
} catch (error) {
|
|
332
|
-
if (process.env.NODE_ENV !== "production") {
|
|
333
|
-
logger.error(error);
|
|
334
|
-
}
|
|
335
|
-
// Re-throw the error.
|
|
336
|
-
throw error;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* @private
|
|
342
|
-
*/
|
|
343
|
-
private readonly _onUpdateFound = () => {
|
|
344
|
-
// `this._registration` will never be `undefined` after an update is found.
|
|
345
|
-
const registration = this._registration!;
|
|
346
|
-
const installingSW = registration.installing as ServiceWorker;
|
|
347
|
-
|
|
348
|
-
// If the script URL passed to `navigator.serviceWorker.register()` is
|
|
349
|
-
// different from the current controlling SW's script URL, we know any
|
|
350
|
-
// successful registration calls will trigger an `updatefound` event.
|
|
351
|
-
// But if the registered script URL is the same as the current controlling
|
|
352
|
-
// SW's script URL, we'll only get an `updatefound` event if the file
|
|
353
|
-
// changed since it was last registered. This can be a problem if the user
|
|
354
|
-
// opens up the same page in a different tab, and that page registers
|
|
355
|
-
// a SW that triggers an update. It's a problem because this page has no
|
|
356
|
-
// good way of knowing whether the `updatefound` event came from the SW
|
|
357
|
-
// script it registered or from a registration attempt made by a newer
|
|
358
|
-
// version of the page running in another tab.
|
|
359
|
-
// To minimize the possibility of a false positive, we use the logic here:
|
|
360
|
-
const updateLikelyTriggeredExternally =
|
|
361
|
-
// Since we enforce only calling `register()` once, and since we don't
|
|
362
|
-
// add the `updatefound` event listener until the `register()` call, if
|
|
363
|
-
// `_updateFoundCount` is > 0 then it means this method has already
|
|
364
|
-
// been called, thus this SW must be external
|
|
365
|
-
this._updateFoundCount > 0 ||
|
|
366
|
-
// If the script URL of the installing SW is different from this
|
|
367
|
-
// instance's script URL, we know it's definitely not from our
|
|
368
|
-
// registration.
|
|
369
|
-
!urlsMatch(installingSW.scriptURL, this._scriptURL.toString()) ||
|
|
370
|
-
// If all of the above are false, then we use a time-based heuristic:
|
|
371
|
-
// Any `updatefound` event that occurs long after our registration is
|
|
372
|
-
// assumed to be external.
|
|
373
|
-
performance.now() > this._registrationTime + REGISTRATION_TIMEOUT_DURATION
|
|
374
|
-
? // If any of the above are not true, we assume the update was
|
|
375
|
-
// triggered by this instance.
|
|
376
|
-
true
|
|
377
|
-
: false;
|
|
378
|
-
|
|
379
|
-
if (updateLikelyTriggeredExternally) {
|
|
380
|
-
this._externalSW = installingSW;
|
|
381
|
-
registration.removeEventListener("updatefound", this._onUpdateFound);
|
|
382
|
-
} else {
|
|
383
|
-
// If the update was not triggered externally we know the installing
|
|
384
|
-
// SW is the one we registered, so we set it.
|
|
385
|
-
this._sw = installingSW;
|
|
386
|
-
this._ownSWs.add(installingSW);
|
|
387
|
-
this._swDeferred.resolve(installingSW);
|
|
388
|
-
|
|
389
|
-
// The `installing` state isn't something we have a dedicated
|
|
390
|
-
// callback for, but we do log messages for it in development.
|
|
391
|
-
if (process.env.NODE_ENV !== "production") {
|
|
392
|
-
if (navigator.serviceWorker.controller) {
|
|
393
|
-
logger.log("Updated service worker found. Installing now...");
|
|
394
|
-
} else {
|
|
395
|
-
logger.log("Service worker is installing...");
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// Increment the `updatefound` count, so future invocations of this
|
|
401
|
-
// method can be sure they were triggered externally.
|
|
402
|
-
++this._updateFoundCount;
|
|
403
|
-
|
|
404
|
-
// Add a `statechange` listener regardless of whether this update was
|
|
405
|
-
// triggered externally, since we have callbacks for both.
|
|
406
|
-
installingSW.addEventListener("statechange", this._onStateChange);
|
|
407
|
-
};
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
* @private
|
|
411
|
-
* @param originalEvent
|
|
412
|
-
*/
|
|
413
|
-
private readonly _onStateChange = (originalEvent: Event) => {
|
|
414
|
-
// `this._registration` will never be `undefined` after an update is found.
|
|
415
|
-
const registration = this._registration!;
|
|
416
|
-
const sw = originalEvent.target as ServiceWorker;
|
|
417
|
-
const { state } = sw;
|
|
418
|
-
const isExternal = sw === this._externalSW;
|
|
419
|
-
|
|
420
|
-
const eventProps: {
|
|
421
|
-
sw: ServiceWorker;
|
|
422
|
-
originalEvent: Event;
|
|
423
|
-
isUpdate?: boolean;
|
|
424
|
-
isExternal: boolean;
|
|
425
|
-
} = {
|
|
426
|
-
sw,
|
|
427
|
-
isExternal,
|
|
428
|
-
originalEvent,
|
|
429
|
-
};
|
|
430
|
-
if (!isExternal && this._isUpdate) {
|
|
431
|
-
eventProps.isUpdate = true;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
this.dispatchEvent(new SerwistEvent(state as keyof SerwistLifecycleEventMap, eventProps));
|
|
435
|
-
|
|
436
|
-
if (state === "installed") {
|
|
437
|
-
// This timeout is used to ignore cases where the service worker calls
|
|
438
|
-
// `skipWaiting()` in the install event, thus moving it directly in the
|
|
439
|
-
// activating state. (Since all service workers *must* go through the
|
|
440
|
-
// waiting phase, the only way to detect `skipWaiting()` called in the
|
|
441
|
-
// install event is to observe that the time spent in the waiting phase
|
|
442
|
-
// is very short.)
|
|
443
|
-
// NOTE: we don't need separate timeouts for the own and external SWs
|
|
444
|
-
// since they can't go through these phases at the same time.
|
|
445
|
-
this._waitingTimeout = self.setTimeout(() => {
|
|
446
|
-
// Ensure the SW is still waiting (it may now be redundant).
|
|
447
|
-
if (state === "installed" && registration.waiting === sw) {
|
|
448
|
-
this.dispatchEvent(new SerwistEvent("waiting", eventProps));
|
|
449
|
-
|
|
450
|
-
if (process.env.NODE_ENV !== "production") {
|
|
451
|
-
if (isExternal) {
|
|
452
|
-
logger.warn("An external service worker has installed but is " + "waiting for this client to close before activating...");
|
|
453
|
-
} else {
|
|
454
|
-
logger.warn("The service worker has installed but is waiting " + "for existing clients to close before activating...");
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
}, WAITING_TIMEOUT_DURATION);
|
|
459
|
-
} else if (state === "activating") {
|
|
460
|
-
clearTimeout(this._waitingTimeout);
|
|
461
|
-
if (!isExternal) {
|
|
462
|
-
this._activeDeferred.resolve(sw);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
if (process.env.NODE_ENV !== "production") {
|
|
467
|
-
switch (state) {
|
|
468
|
-
case "installed":
|
|
469
|
-
if (isExternal) {
|
|
470
|
-
logger.warn("An external service worker has installed. " + "You may want to suggest users reload this page.");
|
|
471
|
-
} else {
|
|
472
|
-
logger.log("Registered service worker installed.");
|
|
473
|
-
}
|
|
474
|
-
break;
|
|
475
|
-
case "activated":
|
|
476
|
-
if (isExternal) {
|
|
477
|
-
logger.warn("An external service worker has activated.");
|
|
478
|
-
} else {
|
|
479
|
-
logger.log("Registered service worker activated.");
|
|
480
|
-
if (sw !== navigator.serviceWorker.controller) {
|
|
481
|
-
logger.warn(
|
|
482
|
-
"The registered service worker is active but " +
|
|
483
|
-
"not yet controlling the page. Reload or run " +
|
|
484
|
-
"`clients.claim()` in the service worker."
|
|
485
|
-
);
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
break;
|
|
489
|
-
case "redundant":
|
|
490
|
-
if (sw === this._compatibleControllingSW) {
|
|
491
|
-
logger.log("Previously controlling service worker now redundant!");
|
|
492
|
-
} else if (!isExternal) {
|
|
493
|
-
logger.log("Registered service worker now redundant!");
|
|
494
|
-
}
|
|
495
|
-
break;
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
};
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* @private
|
|
502
|
-
* @param originalEvent
|
|
503
|
-
*/
|
|
504
|
-
private readonly _onControllerChange = (originalEvent: Event) => {
|
|
505
|
-
const sw = this._sw;
|
|
506
|
-
const isExternal = sw !== navigator.serviceWorker.controller;
|
|
507
|
-
|
|
508
|
-
// Unconditionally dispatch the controlling event, with isExternal set
|
|
509
|
-
// to distinguish between controller changes due to the initial registration
|
|
510
|
-
// vs. an update-check or other tab's registration.
|
|
511
|
-
// See https://github.com/GoogleChrome/workbox/issues/2786
|
|
512
|
-
this.dispatchEvent(
|
|
513
|
-
new SerwistEvent("controlling", {
|
|
514
|
-
isExternal,
|
|
515
|
-
originalEvent,
|
|
516
|
-
sw,
|
|
517
|
-
isUpdate: this._isUpdate,
|
|
518
|
-
})
|
|
519
|
-
);
|
|
520
|
-
|
|
521
|
-
if (!isExternal) {
|
|
522
|
-
if (process.env.NODE_ENV !== "production") {
|
|
523
|
-
logger.log("Registered service worker now controlling this page.");
|
|
524
|
-
}
|
|
525
|
-
this._controllingDeferred.resolve(sw);
|
|
526
|
-
}
|
|
527
|
-
};
|
|
528
|
-
|
|
529
|
-
/**
|
|
530
|
-
* @private
|
|
531
|
-
* @param originalEvent
|
|
532
|
-
*/
|
|
533
|
-
private readonly _onMessage = async (originalEvent: MessageEvent) => {
|
|
534
|
-
// Can't change type 'any' of data.
|
|
535
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
536
|
-
const { data, ports, source } = originalEvent;
|
|
537
|
-
|
|
538
|
-
// Wait until there's an "own" service worker. This is used to buffer
|
|
539
|
-
// `message` events that may be received prior to calling `register()`.
|
|
540
|
-
await this.getSW();
|
|
541
|
-
|
|
542
|
-
// If the service worker that sent the message is in the list of own
|
|
543
|
-
// service workers for this instance, dispatch a `message` event.
|
|
544
|
-
// NOTE: we check for all previously owned service workers rather than
|
|
545
|
-
// just the current one because some messages (e.g. cache updates) use
|
|
546
|
-
// a timeout when sent and may be delayed long enough for a service worker
|
|
547
|
-
// update to be found.
|
|
548
|
-
if (this._ownSWs.has(source as ServiceWorker)) {
|
|
549
|
-
this.dispatchEvent(
|
|
550
|
-
new SerwistEvent("message", {
|
|
551
|
-
// Can't change type 'any' of data.
|
|
552
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
553
|
-
data,
|
|
554
|
-
originalEvent,
|
|
555
|
-
ports,
|
|
556
|
-
sw: source as ServiceWorker,
|
|
557
|
-
})
|
|
558
|
-
);
|
|
559
|
-
}
|
|
560
|
-
};
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
// The jsdoc comments below outline the events this instance may dispatch:
|
|
564
|
-
// -----------------------------------------------------------------------
|
|
565
|
-
|
|
566
|
-
/**
|
|
567
|
-
* The `message` event is dispatched any time a `postMessage` is received.
|
|
568
|
-
*
|
|
569
|
-
* @event workbox-window.Workbox#message
|
|
570
|
-
* @type {SerwistEvent}
|
|
571
|
-
* @property {*} data The `data` property from the original `message` event.
|
|
572
|
-
* @property {Event} originalEvent The original [`message`]{@link https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent}
|
|
573
|
-
* event.
|
|
574
|
-
* @property {string} type `message`.
|
|
575
|
-
* @property {MessagePort[]} ports The `ports` value from `originalEvent`.
|
|
576
|
-
* @property {Workbox} target The `Workbox` instance.
|
|
577
|
-
*/
|
|
578
|
-
|
|
579
|
-
/**
|
|
580
|
-
* The `installed` event is dispatched if the state of a
|
|
581
|
-
* {@link workbox-window.Workbox} instance's
|
|
582
|
-
* {@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw|registered service worker}
|
|
583
|
-
* changes to `installed`.
|
|
584
|
-
*
|
|
585
|
-
* Then can happen either the very first time a service worker is installed,
|
|
586
|
-
* or after an update to the current service worker is found. In the case
|
|
587
|
-
* of an update being found, the event's `isUpdate` property will be `true`.
|
|
588
|
-
*
|
|
589
|
-
* @event workbox-window.Workbox#installed
|
|
590
|
-
* @type {SerwistEvent}
|
|
591
|
-
* @property {ServiceWorker} sw The service worker instance.
|
|
592
|
-
* @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|
593
|
-
* event.
|
|
594
|
-
* @property {boolean|undefined} isUpdate True if a service worker was already
|
|
595
|
-
* controlling when this `Workbox` instance called `register()`.
|
|
596
|
-
* @property {boolean|undefined} isExternal True if this event is associated
|
|
597
|
-
* with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
|
|
598
|
-
* @property {string} type `installed`.
|
|
599
|
-
* @property {Workbox} target The `Workbox` instance.
|
|
600
|
-
*/
|
|
601
|
-
|
|
602
|
-
/**
|
|
603
|
-
* The `waiting` event is dispatched if the state of a
|
|
604
|
-
* {@link workbox-window.Workbox} instance's
|
|
605
|
-
* [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
|
|
606
|
-
* changes to `installed` and then doesn't immediately change to `activating`.
|
|
607
|
-
* It may also be dispatched if a service worker with the same
|
|
608
|
-
* [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
|
|
609
|
-
* was already waiting when the {@link workbox-window.Workbox#register}
|
|
610
|
-
* method was called.
|
|
611
|
-
*
|
|
612
|
-
* @event workbox-window.Workbox#waiting
|
|
613
|
-
* @type {SerwistEvent}
|
|
614
|
-
* @property {ServiceWorker} sw The service worker instance.
|
|
615
|
-
* @property {Event|undefined} originalEvent The original
|
|
616
|
-
* [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|
617
|
-
* event, or `undefined` in the case where the service worker was waiting
|
|
618
|
-
* to before `.register()` was called.
|
|
619
|
-
* @property {boolean|undefined} isUpdate True if a service worker was already
|
|
620
|
-
* controlling when this `Workbox` instance called `register()`.
|
|
621
|
-
* @property {boolean|undefined} isExternal True if this event is associated
|
|
622
|
-
* with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
|
|
623
|
-
* @property {boolean|undefined} wasWaitingBeforeRegister True if a service worker with
|
|
624
|
-
* a matching `scriptURL` was already waiting when this `Workbox`
|
|
625
|
-
* instance called `register()`.
|
|
626
|
-
* @property {string} type `waiting`.
|
|
627
|
-
* @property {Workbox} target The `Workbox` instance.
|
|
628
|
-
*/
|
|
629
|
-
|
|
630
|
-
/**
|
|
631
|
-
* The `controlling` event is dispatched if a
|
|
632
|
-
* [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
|
|
633
|
-
* fires on the service worker [container]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer}
|
|
634
|
-
* and the [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
|
|
635
|
-
* of the new [controller]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/controller}
|
|
636
|
-
* matches the `scriptURL` of the `Workbox` instance's
|
|
637
|
-
* [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}.
|
|
638
|
-
*
|
|
639
|
-
* @event workbox-window.Workbox#controlling
|
|
640
|
-
* @type {SerwistEvent}
|
|
641
|
-
* @property {ServiceWorker} sw The service worker instance.
|
|
642
|
-
* @property {Event} originalEvent The original [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
|
|
643
|
-
* event.
|
|
644
|
-
* @property {boolean|undefined} isUpdate True if a service worker was already
|
|
645
|
-
* controlling when this service worker was registered.
|
|
646
|
-
* @property {boolean|undefined} isExternal True if this event is associated
|
|
647
|
-
* with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
|
|
648
|
-
* @property {string} type `controlling`.
|
|
649
|
-
* @property {Workbox} target The `Workbox` instance.
|
|
650
|
-
*/
|
|
651
|
-
|
|
652
|
-
/**
|
|
653
|
-
* The `activated` event is dispatched if the state of a
|
|
654
|
-
* {@link workbox-window.Workbox} instance's
|
|
655
|
-
* {@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw|registered service worker}
|
|
656
|
-
* changes to `activated`.
|
|
657
|
-
*
|
|
658
|
-
* @event workbox-window.Workbox#activated
|
|
659
|
-
* @type {SerwistEvent}
|
|
660
|
-
* @property {ServiceWorker} sw The service worker instance.
|
|
661
|
-
* @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|
662
|
-
* event.
|
|
663
|
-
* @property {boolean|undefined} isUpdate True if a service worker was already
|
|
664
|
-
* controlling when this `Workbox` instance called `register()`.
|
|
665
|
-
* @property {boolean|undefined} isExternal True if this event is associated
|
|
666
|
-
* with an [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}.
|
|
667
|
-
* @property {string} type `activated`.
|
|
668
|
-
* @property {Workbox} target The `Workbox` instance.
|
|
669
|
-
*/
|
|
670
|
-
|
|
671
|
-
/**
|
|
672
|
-
* The `redundant` event is dispatched if the state of a
|
|
673
|
-
* {@link workbox-window.Workbox} instance's
|
|
674
|
-
* [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
|
|
675
|
-
* changes to `redundant`.
|
|
676
|
-
*
|
|
677
|
-
* @event workbox-window.Workbox#redundant
|
|
678
|
-
* @type {SerwistEvent}
|
|
679
|
-
* @property {ServiceWorker} sw The service worker instance.
|
|
680
|
-
* @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
|
|
681
|
-
* event.
|
|
682
|
-
* @property {boolean|undefined} isUpdate True if a service worker was already
|
|
683
|
-
* controlling when this `Workbox` instance called `register()`.
|
|
684
|
-
* @property {string} type `redundant`.
|
|
685
|
-
* @property {Workbox} target The `Workbox` instance.
|
|
686
|
-
*/
|
package/src/index.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { messageSW } from "./messageSW.js";
|
|
10
|
-
import { Serwist } from "./Serwist.js";
|
|
11
|
-
|
|
12
|
-
export { messageSW, Serwist };
|
|
13
|
-
|
|
14
|
-
// See https://github.com/GoogleChrome/workbox/issues/2770
|
|
15
|
-
export * from "./utils/SerwistEvent.js";
|
package/src/messageSW.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Sends a data object to a service worker via `postMessage` and resolves with
|
|
11
|
-
* a response (if any).
|
|
12
|
-
*
|
|
13
|
-
* A response can be set in a message handler in the service worker by
|
|
14
|
-
* calling `event.ports[0].postMessage(...)`, which will resolve the promise
|
|
15
|
-
* returned by `messageSW()`. If no response is set, the promise will not
|
|
16
|
-
* resolve.
|
|
17
|
-
*
|
|
18
|
-
* @param sw The service worker to send the message to.
|
|
19
|
-
* @param data An object to send to the service worker.
|
|
20
|
-
* @returns
|
|
21
|
-
*/
|
|
22
|
-
// Better not change type of data.
|
|
23
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
24
|
-
function messageSW(sw: ServiceWorker, data: {}): Promise<any> {
|
|
25
|
-
return new Promise((resolve) => {
|
|
26
|
-
const messageChannel = new MessageChannel();
|
|
27
|
-
messageChannel.port1.onmessage = (event: MessageEvent) => {
|
|
28
|
-
resolve(event.data);
|
|
29
|
-
};
|
|
30
|
-
sw.postMessage(data, [messageChannel.port2]);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export { messageSW };
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { SerwistEventTarget } from "./SerwistEventTarget.js";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* A minimal `Event` subclass shim.
|
|
13
|
-
* This doesn't *actually* subclass `Event` because not all browsers support
|
|
14
|
-
* constructable `EventTarget`, and using a real `Event` will error.
|
|
15
|
-
* @private
|
|
16
|
-
*/
|
|
17
|
-
export class SerwistEvent<K extends keyof SerwistEventMap> {
|
|
18
|
-
target?: SerwistEventTarget;
|
|
19
|
-
sw?: ServiceWorker;
|
|
20
|
-
originalEvent?: Event;
|
|
21
|
-
isExternal?: boolean;
|
|
22
|
-
|
|
23
|
-
constructor(
|
|
24
|
-
public type: K,
|
|
25
|
-
props: Omit<SerwistEventMap[K], "target" | "type">
|
|
26
|
-
) {
|
|
27
|
-
Object.assign(this, props);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface SerwistMessageEvent extends SerwistEvent<"message"> {
|
|
32
|
-
data: any;
|
|
33
|
-
originalEvent: Event;
|
|
34
|
-
ports: readonly MessagePort[];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface SerwistLifecycleEvent extends SerwistEvent<keyof SerwistLifecycleEventMap> {
|
|
38
|
-
isUpdate?: boolean;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface SerwistLifecycleWaitingEvent extends SerwistLifecycleEvent {
|
|
42
|
-
wasWaitingBeforeRegister?: boolean;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface SerwistLifecycleEventMap {
|
|
46
|
-
installing: SerwistLifecycleEvent;
|
|
47
|
-
installed: SerwistLifecycleEvent;
|
|
48
|
-
waiting: SerwistLifecycleWaitingEvent;
|
|
49
|
-
activating: SerwistLifecycleEvent;
|
|
50
|
-
activated: SerwistLifecycleEvent;
|
|
51
|
-
controlling: SerwistLifecycleEvent;
|
|
52
|
-
redundant: SerwistLifecycleEvent;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface SerwistEventMap extends SerwistLifecycleEventMap {
|
|
56
|
-
message: SerwistMessageEvent;
|
|
57
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { SerwistEvent, SerwistEventMap } from "./SerwistEvent.js";
|
|
10
|
-
|
|
11
|
-
export type ListenerCallback = (event: SerwistEvent<any>) => any;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* A minimal `EventTarget` shim.
|
|
15
|
-
* This is necessary because not all browsers support constructable
|
|
16
|
-
* `EventTarget`, so using a real `EventTarget` will error.
|
|
17
|
-
* @private
|
|
18
|
-
*/
|
|
19
|
-
export class SerwistEventTarget {
|
|
20
|
-
private readonly _eventListenerRegistry: Map<keyof SerwistEventMap, Set<ListenerCallback>> = new Map();
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @param type
|
|
24
|
-
* @param listener
|
|
25
|
-
* @private
|
|
26
|
-
*/
|
|
27
|
-
addEventListener<K extends keyof SerwistEventMap>(type: K, listener: (event: SerwistEventMap[K]) => any): void {
|
|
28
|
-
const foo = this._getEventListenersByType(type);
|
|
29
|
-
foo.add(listener as ListenerCallback);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @param type
|
|
34
|
-
* @param listener
|
|
35
|
-
* @private
|
|
36
|
-
*/
|
|
37
|
-
removeEventListener<K extends keyof SerwistEventMap>(type: K, listener: (event: SerwistEventMap[K]) => any): void {
|
|
38
|
-
this._getEventListenersByType(type).delete(listener as ListenerCallback);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @param event
|
|
43
|
-
* @private
|
|
44
|
-
*/
|
|
45
|
-
dispatchEvent(event: SerwistEvent<any>): void {
|
|
46
|
-
event.target = this;
|
|
47
|
-
|
|
48
|
-
const listeners = this._getEventListenersByType(event.type);
|
|
49
|
-
for (const listener of listeners) {
|
|
50
|
-
listener(event);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Returns a Set of listeners associated with the passed event type.
|
|
56
|
-
* If no handlers have been registered, an empty Set is returned.
|
|
57
|
-
*
|
|
58
|
-
* @param type The event type.
|
|
59
|
-
* @returns An array of handler functions.
|
|
60
|
-
* @private
|
|
61
|
-
*/
|
|
62
|
-
private _getEventListenersByType(type: keyof SerwistEventMap) {
|
|
63
|
-
if (!this._eventListenerRegistry.has(type)) {
|
|
64
|
-
this._eventListenerRegistry.set(type, new Set());
|
|
65
|
-
}
|
|
66
|
-
return this._eventListenerRegistry.get(type)!;
|
|
67
|
-
}
|
|
68
|
-
}
|
package/src/utils/urlsMatch.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Returns true if two URLs have the same `.href` property. The URLS can be
|
|
11
|
-
* relative, and if they are the current location href is used to resolve URLs.
|
|
12
|
-
*
|
|
13
|
-
* @private
|
|
14
|
-
* @param url1
|
|
15
|
-
* @param url2
|
|
16
|
-
* @returns
|
|
17
|
-
*/
|
|
18
|
-
export function urlsMatch(url1: string, url2: string): boolean {
|
|
19
|
-
const { href } = location;
|
|
20
|
-
return new URL(url1, href).href === new URL(url2, href).href;
|
|
21
|
-
}
|