@docusaurus/plugin-pwa 0.0.0-5041 → 0.0.0-5044

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/sw.d.ts CHANGED
@@ -1 +1,7 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
1
7
  export {};
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
- const params = JSON.parse(
13
- new URLSearchParams(self.location.search).get('params'),
14
- );
15
- if (params.debug) {
16
- console.log('[Docusaurus-PWA][SW]: Service Worker params:', params);
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
- if (process.env.PWA_SW_CUSTOM) {
27
- const customSW = await import(process.env.PWA_SW_CUSTOM);
28
- if (typeof customSW.default === 'function') {
29
- customSW.default(params);
30
- } else if (params.debug) {
31
- console.warn(
32
- '[Docusaurus-PWA][SW]: swCustom should have a default export function',
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
- const possibleURLs = [];
46
- const urlObject = new URL(url, self.location.href);
47
-
48
- if (urlObject.origin !== self.location.origin) {
49
- return possibleURLs;
50
- }
51
-
52
- // Ignore search params and hash
53
- urlObject.search = '';
54
- urlObject.hash = '';
55
-
56
- // /blog.html
57
- possibleURLs.push(urlObject.href);
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
- const params = parseSwParams();
72
-
73
- // eslint-disable-next-line no-underscore-dangle
74
- const precacheManifest = self.__WB_MANIFEST;
75
- const controller = new PrecacheController({
76
- // Safer to turn this true?
77
- fallbackToNetwork: true,
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
- const requestURL = event.request.url;
112
- const possibleURLs = getPossibleURLs(requestURL);
113
- for (let i = 0; i < possibleURLs.length; i += 1) {
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-5041",
3
+ "version": "0.0.0-5044",
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-5041",
26
- "@docusaurus/theme-common": "0.0.0-5041",
27
- "@docusaurus/theme-translations": "0.0.0-5041",
28
- "@docusaurus/utils": "0.0.0-5041",
29
- "@docusaurus/utils-validation": "0.0.0-5041",
25
+ "@docusaurus/core": "0.0.0-5044",
26
+ "@docusaurus/theme-common": "0.0.0-5044",
27
+ "@docusaurus/theme-translations": "0.0.0-5044",
28
+ "@docusaurus/utils": "0.0.0-5044",
29
+ "@docusaurus/utils-validation": "0.0.0-5044",
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-5041",
42
+ "@docusaurus/module-type-aliases": "0.0.0-5044",
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": "5a3b9a5a3a9c680f7f6d29b622416fc30cd8275d"
49
+ "gitHead": "9ffd6ca265dc2af7ed5b96daf5ebf32708e899cf"
50
50
  }
@@ -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 possibleURLs;
46
+ return [];
50
47
  }
51
48
 
52
49
  // Ignore search params and hash
53
50
  urlObject.search = '';
54
51
  urlObject.hash = '';
55
52
 
56
- // /blog.html
57
- possibleURLs.push(urlObject.href);
58
-
59
- // /blog/ => /blog/index.html
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
- possibleURLs.push(`${urlObject.href}/index.html`);
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 = self.__WB_MANIFEST;
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(controller.install(event));
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(controller.activate(event));
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 (let i = 0; i < possibleURLs.length; i += 1) {
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
- self.skipWaiting();
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
  })();