@docusaurus/plugin-pwa 0.0.0-5040 → 0.0.0-5043
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/lib/registerSw.d.ts +1 -15
- package/lib/registerSw.js +2 -1
- package/lib/sw.d.ts +7 -0
- package/lib/sw.js +84 -120
- package/package.json +8 -8
- package/src/registerSw.ts +3 -11
- package/src/{sw.js → sw.ts} +31 -41
package/lib/registerSw.d.ts
CHANGED
|
@@ -4,29 +4,15 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
declare global {
|
|
8
|
-
namespace NodeJS {
|
|
9
|
-
interface ProcessEnv {
|
|
10
|
-
PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES: (keyof typeof OfflineModeActivationStrategiesImplementations)[];
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
7
|
declare global {
|
|
15
8
|
interface Navigator {
|
|
16
9
|
getInstalledRelatedApps: () => Promise<{
|
|
17
10
|
platform: string;
|
|
18
11
|
}[]>;
|
|
19
12
|
connection: {
|
|
13
|
+
effectiveType: string;
|
|
20
14
|
saveData: boolean;
|
|
21
15
|
};
|
|
22
16
|
}
|
|
23
17
|
}
|
|
24
|
-
declare const OfflineModeActivationStrategiesImplementations: {
|
|
25
|
-
always: () => boolean;
|
|
26
|
-
mobile: () => boolean;
|
|
27
|
-
saveData: () => boolean;
|
|
28
|
-
appInstalled: () => Promise<boolean>;
|
|
29
|
-
standalone: () => boolean;
|
|
30
|
-
queryString: () => boolean;
|
|
31
|
-
};
|
|
32
18
|
export {};
|
package/lib/registerSw.js
CHANGED
|
@@ -9,7 +9,8 @@ import { createStorageSlot } from '@docusaurus/theme-common';
|
|
|
9
9
|
/* eslint-disable prefer-destructuring */
|
|
10
10
|
const PWA_SERVICE_WORKER_URL = process.env.PWA_SERVICE_WORKER_URL;
|
|
11
11
|
const PWA_RELOAD_POPUP = process.env.PWA_RELOAD_POPUP;
|
|
12
|
-
const PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES = process.env
|
|
12
|
+
const PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES = process.env
|
|
13
|
+
.PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES;
|
|
13
14
|
const PWA_DEBUG = process.env.PWA_DEBUG;
|
|
14
15
|
/* eslint-enable prefer-destructuring */
|
|
15
16
|
const debug = PWA_DEBUG; // Shortcut
|
package/lib/sw.d.ts
ADDED
package/lib/sw.js
CHANGED
|
@@ -5,143 +5,107 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
/* eslint-disable no-restricted-globals */
|
|
8
|
-
|
|
9
|
-
import {PrecacheController} from 'workbox-precaching';
|
|
10
|
-
|
|
8
|
+
import { PrecacheController } from 'workbox-precaching';
|
|
11
9
|
function parseSwParams() {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
return params;
|
|
10
|
+
const params = JSON.parse(new URLSearchParams(self.location.search).get('params'));
|
|
11
|
+
if (params.debug) {
|
|
12
|
+
console.log('[Docusaurus-PWA][SW]: Service Worker params:', params);
|
|
13
|
+
}
|
|
14
|
+
return params;
|
|
19
15
|
}
|
|
20
|
-
|
|
21
16
|
// Doc advises against dynamic imports in SW
|
|
22
17
|
// https://developers.google.com/web/tools/workbox/guides/using-bundlers#code_splitting_and_dynamic_imports
|
|
23
18
|
// https://twitter.com/sebastienlorber/status/1280155204575518720
|
|
24
19
|
// but looks it's working fine as it's inlined by webpack, need to double check
|
|
25
20
|
async function runSWCustomCode(params) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
21
|
+
if (process.env.PWA_SW_CUSTOM) {
|
|
22
|
+
const customSW = await import(process.env.PWA_SW_CUSTOM);
|
|
23
|
+
if (typeof customSW.default === 'function') {
|
|
24
|
+
customSW.default(params);
|
|
25
|
+
}
|
|
26
|
+
else if (params.debug) {
|
|
27
|
+
console.warn('[Docusaurus-PWA][SW]: swCustom should have a default export function');
|
|
28
|
+
}
|
|
34
29
|
}
|
|
35
|
-
}
|
|
36
30
|
}
|
|
37
|
-
|
|
38
31
|
/**
|
|
39
32
|
* Gets different possible variations for a request URL. Similar to
|
|
40
33
|
* https://git.io/JvixK
|
|
41
|
-
*
|
|
42
|
-
* @param {string} url
|
|
43
34
|
*/
|
|
44
35
|
function getPossibleURLs(url) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
// /blog/ => /blog/index.html
|
|
60
|
-
if (urlObject.pathname.endsWith('/')) {
|
|
61
|
-
possibleURLs.push(`${urlObject.href}index.html`);
|
|
62
|
-
} else {
|
|
63
|
-
// /blog => /blog/index.html
|
|
64
|
-
possibleURLs.push(`${urlObject.href}/index.html`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return possibleURLs;
|
|
36
|
+
const urlObject = new URL(url, self.location.href);
|
|
37
|
+
if (urlObject.origin !== self.location.origin) {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
// Ignore search params and hash
|
|
41
|
+
urlObject.search = '';
|
|
42
|
+
urlObject.hash = '';
|
|
43
|
+
return [
|
|
44
|
+
// /blog.html
|
|
45
|
+
urlObject.href,
|
|
46
|
+
// /blog/ => /blog/index.html
|
|
47
|
+
// /blog => /blog/index.html
|
|
48
|
+
`${urlObject.href}${urlObject.pathname.endsWith('/') ? '' : '/'}index.html`,
|
|
49
|
+
];
|
|
68
50
|
}
|
|
69
|
-
|
|
70
51
|
(async () => {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
if (params.offlineMode) {
|
|
81
|
-
controller.addToCacheList(precacheManifest);
|
|
82
|
-
if (params.debug) {
|
|
83
|
-
console.log('[Docusaurus-PWA][SW]: addToCacheList', {
|
|
84
|
-
precacheManifest,
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
await runSWCustomCode(params);
|
|
90
|
-
|
|
91
|
-
self.addEventListener('install', (event) => {
|
|
92
|
-
if (params.debug) {
|
|
93
|
-
console.log('[Docusaurus-PWA][SW]: install event', {
|
|
94
|
-
event,
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
event.waitUntil(controller.install(event));
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
self.addEventListener('activate', (event) => {
|
|
101
|
-
if (params.debug) {
|
|
102
|
-
console.log('[Docusaurus-PWA][SW]: activate event', {
|
|
103
|
-
event,
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
event.waitUntil(controller.activate(event));
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
self.addEventListener('fetch', async (event) => {
|
|
52
|
+
const params = parseSwParams();
|
|
53
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
54
|
+
const precacheManifest = self.__WB_MANIFEST;
|
|
55
|
+
const controller = new PrecacheController({
|
|
56
|
+
// Safer to turn this true?
|
|
57
|
+
fallbackToNetwork: true,
|
|
58
|
+
});
|
|
110
59
|
if (params.offlineMode) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const possibleURL = possibleURLs[i];
|
|
115
|
-
const cacheKey = controller.getCacheKeyForURL(possibleURL);
|
|
116
|
-
if (cacheKey) {
|
|
117
|
-
const cachedResponse = caches.match(cacheKey);
|
|
118
|
-
if (params.debug) {
|
|
119
|
-
console.log('[Docusaurus-PWA][SW]: serving cached asset', {
|
|
120
|
-
requestURL,
|
|
121
|
-
possibleURL,
|
|
122
|
-
possibleURLs,
|
|
123
|
-
cacheKey,
|
|
124
|
-
cachedResponse,
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
event.respondWith(cachedResponse);
|
|
128
|
-
break;
|
|
60
|
+
controller.addToCacheList(precacheManifest);
|
|
61
|
+
if (params.debug) {
|
|
62
|
+
console.log('[Docusaurus-PWA][SW]: addToCacheList', { precacheManifest });
|
|
129
63
|
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
self.addEventListener('message', async (event) => {
|
|
135
|
-
if (params.debug) {
|
|
136
|
-
console.log('[Docusaurus-PWA][SW]: message event', {
|
|
137
|
-
event,
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const type = event.data?.type;
|
|
142
|
-
|
|
143
|
-
if (type === 'SKIP_WAITING') {
|
|
144
|
-
self.skipWaiting();
|
|
145
64
|
}
|
|
146
|
-
|
|
65
|
+
await runSWCustomCode(params);
|
|
66
|
+
self.addEventListener('install', (event) => {
|
|
67
|
+
if (params.debug) {
|
|
68
|
+
console.log('[Docusaurus-PWA][SW]: install event', { event });
|
|
69
|
+
}
|
|
70
|
+
event.waitUntil(controller.install(event));
|
|
71
|
+
});
|
|
72
|
+
self.addEventListener('activate', (event) => {
|
|
73
|
+
if (params.debug) {
|
|
74
|
+
console.log('[Docusaurus-PWA][SW]: activate event', { event });
|
|
75
|
+
}
|
|
76
|
+
event.waitUntil(controller.activate(event));
|
|
77
|
+
});
|
|
78
|
+
self.addEventListener('fetch', async (event) => {
|
|
79
|
+
if (params.offlineMode) {
|
|
80
|
+
const requestURL = event.request.url;
|
|
81
|
+
const possibleURLs = getPossibleURLs(requestURL);
|
|
82
|
+
for (const possibleURL of possibleURLs) {
|
|
83
|
+
const cacheKey = controller.getCacheKeyForURL(possibleURL);
|
|
84
|
+
if (cacheKey) {
|
|
85
|
+
const cachedResponse = caches.match(cacheKey);
|
|
86
|
+
if (params.debug) {
|
|
87
|
+
console.log('[Docusaurus-PWA][SW]: serving cached asset', {
|
|
88
|
+
requestURL,
|
|
89
|
+
possibleURL,
|
|
90
|
+
possibleURLs,
|
|
91
|
+
cacheKey,
|
|
92
|
+
cachedResponse,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
event.respondWith(cachedResponse);
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
self.addEventListener('message', async (event) => {
|
|
102
|
+
if (params.debug) {
|
|
103
|
+
console.log('[Docusaurus-PWA][SW]: message event', { event });
|
|
104
|
+
}
|
|
105
|
+
const type = event.data?.type;
|
|
106
|
+
if (type === 'SKIP_WAITING') {
|
|
107
|
+
// lib def bug, see https://github.com/microsoft/TypeScript/issues/14877
|
|
108
|
+
self.skipWaiting();
|
|
109
|
+
}
|
|
110
|
+
});
|
|
147
111
|
})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/plugin-pwa",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-5043",
|
|
4
4
|
"description": "Docusaurus Plugin to add PWA support.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "src/plugin-pwa.d.ts",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@babel/core": "^7.18.0",
|
|
24
24
|
"@babel/preset-env": "^7.18.0",
|
|
25
|
-
"@docusaurus/core": "0.0.0-
|
|
26
|
-
"@docusaurus/theme-common": "0.0.0-
|
|
27
|
-
"@docusaurus/theme-translations": "0.0.0-
|
|
28
|
-
"@docusaurus/utils": "0.0.0-
|
|
29
|
-
"@docusaurus/utils-validation": "0.0.0-
|
|
25
|
+
"@docusaurus/core": "0.0.0-5043",
|
|
26
|
+
"@docusaurus/theme-common": "0.0.0-5043",
|
|
27
|
+
"@docusaurus/theme-translations": "0.0.0-5043",
|
|
28
|
+
"@docusaurus/utils": "0.0.0-5043",
|
|
29
|
+
"@docusaurus/utils-validation": "0.0.0-5043",
|
|
30
30
|
"babel-loader": "^8.2.5",
|
|
31
31
|
"clsx": "^1.1.1",
|
|
32
32
|
"core-js": "^3.22.5",
|
|
@@ -39,12 +39,12 @@
|
|
|
39
39
|
"workbox-window": "^6.5.3"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@docusaurus/module-type-aliases": "0.0.0-
|
|
42
|
+
"@docusaurus/module-type-aliases": "0.0.0-5043",
|
|
43
43
|
"fs-extra": "^10.1.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"react": "^16.8.4 || ^17.0.0",
|
|
47
47
|
"react-dom": "^16.8.4 || ^17.0.0"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "28ad11cdb6c55b7a8355e327913f617d4fe359bd"
|
|
50
50
|
}
|
package/src/registerSw.ts
CHANGED
|
@@ -7,20 +7,12 @@
|
|
|
7
7
|
|
|
8
8
|
import {createStorageSlot} from '@docusaurus/theme-common';
|
|
9
9
|
|
|
10
|
-
declare global {
|
|
11
|
-
namespace NodeJS {
|
|
12
|
-
interface ProcessEnv {
|
|
13
|
-
PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES: (keyof typeof OfflineModeActivationStrategiesImplementations)[];
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
10
|
// First: read the env variables (provided by Webpack)
|
|
19
11
|
/* eslint-disable prefer-destructuring */
|
|
20
12
|
const PWA_SERVICE_WORKER_URL = process.env.PWA_SERVICE_WORKER_URL;
|
|
21
13
|
const PWA_RELOAD_POPUP = process.env.PWA_RELOAD_POPUP;
|
|
22
|
-
const PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES =
|
|
23
|
-
|
|
14
|
+
const PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES = process.env
|
|
15
|
+
.PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES as unknown as (keyof typeof OfflineModeActivationStrategiesImplementations)[];
|
|
24
16
|
const PWA_DEBUG = process.env.PWA_DEBUG;
|
|
25
17
|
/* eslint-enable prefer-destructuring */
|
|
26
18
|
|
|
@@ -77,7 +69,7 @@ async function isAppInstalledEventFired() {
|
|
|
77
69
|
declare global {
|
|
78
70
|
interface Navigator {
|
|
79
71
|
getInstalledRelatedApps: () => Promise<{platform: string}[]>;
|
|
80
|
-
connection: {saveData: boolean};
|
|
72
|
+
connection: {effectiveType: string; saveData: boolean};
|
|
81
73
|
}
|
|
82
74
|
}
|
|
83
75
|
|
package/src/{sw.js → sw.ts}
RENAMED
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
*/
|
|
7
7
|
/* eslint-disable no-restricted-globals */
|
|
8
8
|
|
|
9
|
-
import {PrecacheController} from 'workbox-precaching';
|
|
9
|
+
import {PrecacheController, type PrecacheEntry} from 'workbox-precaching';
|
|
10
10
|
|
|
11
11
|
function parseSwParams() {
|
|
12
12
|
const params = JSON.parse(
|
|
13
|
-
new URLSearchParams(self.location.search).get('params')
|
|
13
|
+
new URLSearchParams(self.location.search).get('params')!,
|
|
14
14
|
);
|
|
15
15
|
if (params.debug) {
|
|
16
16
|
console.log('[Docusaurus-PWA][SW]: Service Worker params:', params);
|
|
@@ -22,7 +22,7 @@ function parseSwParams() {
|
|
|
22
22
|
// https://developers.google.com/web/tools/workbox/guides/using-bundlers#code_splitting_and_dynamic_imports
|
|
23
23
|
// https://twitter.com/sebastienlorber/status/1280155204575518720
|
|
24
24
|
// but looks it's working fine as it's inlined by webpack, need to double check
|
|
25
|
-
async function runSWCustomCode(params) {
|
|
25
|
+
async function runSWCustomCode(params: {offlineMode: boolean; debug: boolean}) {
|
|
26
26
|
if (process.env.PWA_SW_CUSTOM) {
|
|
27
27
|
const customSW = await import(process.env.PWA_SW_CUSTOM);
|
|
28
28
|
if (typeof customSW.default === 'function') {
|
|
@@ -38,40 +38,34 @@ async function runSWCustomCode(params) {
|
|
|
38
38
|
/**
|
|
39
39
|
* Gets different possible variations for a request URL. Similar to
|
|
40
40
|
* https://git.io/JvixK
|
|
41
|
-
*
|
|
42
|
-
* @param {string} url
|
|
43
41
|
*/
|
|
44
|
-
function getPossibleURLs(url) {
|
|
45
|
-
const possibleURLs = [];
|
|
42
|
+
function getPossibleURLs(url: string) {
|
|
46
43
|
const urlObject = new URL(url, self.location.href);
|
|
47
44
|
|
|
48
45
|
if (urlObject.origin !== self.location.origin) {
|
|
49
|
-
return
|
|
46
|
+
return [];
|
|
50
47
|
}
|
|
51
48
|
|
|
52
49
|
// Ignore search params and hash
|
|
53
50
|
urlObject.search = '';
|
|
54
51
|
urlObject.hash = '';
|
|
55
52
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (urlObject.pathname.endsWith('/')) {
|
|
61
|
-
possibleURLs.push(`${urlObject.href}index.html`);
|
|
62
|
-
} else {
|
|
53
|
+
return [
|
|
54
|
+
// /blog.html
|
|
55
|
+
urlObject.href,
|
|
56
|
+
// /blog/ => /blog/index.html
|
|
63
57
|
// /blog => /blog/index.html
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return possibleURLs;
|
|
58
|
+
`${urlObject.href}${urlObject.pathname.endsWith('/') ? '' : '/'}index.html`,
|
|
59
|
+
];
|
|
68
60
|
}
|
|
69
61
|
|
|
70
62
|
(async () => {
|
|
71
63
|
const params = parseSwParams();
|
|
72
64
|
|
|
73
65
|
// eslint-disable-next-line no-underscore-dangle
|
|
74
|
-
const precacheManifest =
|
|
66
|
+
const precacheManifest = (
|
|
67
|
+
self as typeof globalThis & {__WB_MANIFEST: (string | PrecacheEntry)[]}
|
|
68
|
+
).__WB_MANIFEST;
|
|
75
69
|
const controller = new PrecacheController({
|
|
76
70
|
// Safer to turn this true?
|
|
77
71
|
fallbackToNetwork: true,
|
|
@@ -80,9 +74,7 @@ function getPossibleURLs(url) {
|
|
|
80
74
|
if (params.offlineMode) {
|
|
81
75
|
controller.addToCacheList(precacheManifest);
|
|
82
76
|
if (params.debug) {
|
|
83
|
-
console.log('[Docusaurus-PWA][SW]: addToCacheList', {
|
|
84
|
-
precacheManifest,
|
|
85
|
-
});
|
|
77
|
+
console.log('[Docusaurus-PWA][SW]: addToCacheList', {precacheManifest});
|
|
86
78
|
}
|
|
87
79
|
}
|
|
88
80
|
|
|
@@ -90,31 +82,30 @@ function getPossibleURLs(url) {
|
|
|
90
82
|
|
|
91
83
|
self.addEventListener('install', (event) => {
|
|
92
84
|
if (params.debug) {
|
|
93
|
-
console.log('[Docusaurus-PWA][SW]: install event', {
|
|
94
|
-
event,
|
|
95
|
-
});
|
|
85
|
+
console.log('[Docusaurus-PWA][SW]: install event', {event});
|
|
96
86
|
}
|
|
97
|
-
event.waitUntil(
|
|
87
|
+
(event as ExtendableEvent).waitUntil(
|
|
88
|
+
controller.install(event as ExtendableEvent),
|
|
89
|
+
);
|
|
98
90
|
});
|
|
99
91
|
|
|
100
92
|
self.addEventListener('activate', (event) => {
|
|
101
93
|
if (params.debug) {
|
|
102
|
-
console.log('[Docusaurus-PWA][SW]: activate event', {
|
|
103
|
-
event,
|
|
104
|
-
});
|
|
94
|
+
console.log('[Docusaurus-PWA][SW]: activate event', {event});
|
|
105
95
|
}
|
|
106
|
-
event.waitUntil(
|
|
96
|
+
(event as ExtendableEvent).waitUntil(
|
|
97
|
+
controller.activate(event as ExtendableEvent),
|
|
98
|
+
);
|
|
107
99
|
});
|
|
108
100
|
|
|
109
101
|
self.addEventListener('fetch', async (event) => {
|
|
110
102
|
if (params.offlineMode) {
|
|
111
|
-
const requestURL = event.request.url;
|
|
103
|
+
const requestURL = (event as FetchEvent).request.url;
|
|
112
104
|
const possibleURLs = getPossibleURLs(requestURL);
|
|
113
|
-
for (
|
|
114
|
-
const possibleURL = possibleURLs[i];
|
|
105
|
+
for (const possibleURL of possibleURLs) {
|
|
115
106
|
const cacheKey = controller.getCacheKeyForURL(possibleURL);
|
|
116
107
|
if (cacheKey) {
|
|
117
|
-
const cachedResponse = caches.match(cacheKey)
|
|
108
|
+
const cachedResponse = caches.match(cacheKey) as Promise<Response>;
|
|
118
109
|
if (params.debug) {
|
|
119
110
|
console.log('[Docusaurus-PWA][SW]: serving cached asset', {
|
|
120
111
|
requestURL,
|
|
@@ -124,7 +115,7 @@ function getPossibleURLs(url) {
|
|
|
124
115
|
cachedResponse,
|
|
125
116
|
});
|
|
126
117
|
}
|
|
127
|
-
event.respondWith(cachedResponse);
|
|
118
|
+
(event as FetchEvent).respondWith(cachedResponse);
|
|
128
119
|
break;
|
|
129
120
|
}
|
|
130
121
|
}
|
|
@@ -133,15 +124,14 @@ function getPossibleURLs(url) {
|
|
|
133
124
|
|
|
134
125
|
self.addEventListener('message', async (event) => {
|
|
135
126
|
if (params.debug) {
|
|
136
|
-
console.log('[Docusaurus-PWA][SW]: message event', {
|
|
137
|
-
event,
|
|
138
|
-
});
|
|
127
|
+
console.log('[Docusaurus-PWA][SW]: message event', {event});
|
|
139
128
|
}
|
|
140
129
|
|
|
141
|
-
const type = event.data?.type;
|
|
130
|
+
const type = (event as MessageEvent).data?.type;
|
|
142
131
|
|
|
143
132
|
if (type === 'SKIP_WAITING') {
|
|
144
|
-
|
|
133
|
+
// lib def bug, see https://github.com/microsoft/TypeScript/issues/14877
|
|
134
|
+
(self as typeof globalThis & ServiceWorkerGlobalScope).skipWaiting();
|
|
145
135
|
}
|
|
146
136
|
});
|
|
147
137
|
})();
|