@docusaurus/plugin-pwa 2.0.0-beta.1ec2c95e3 → 2.0.0-beta.21

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.
@@ -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);
@@ -18,11 +18,11 @@ function parseSwParams() {
18
18
  return params;
19
19
  }
20
20
 
21
- // doc advise against dynamic imports in SW
21
+ // Doc advises against dynamic imports in SW
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
- // but I think it's working fine as it's inlined by webpack, need to double check?
25
- async function runSWCustomCode(params) {
24
+ // but looks it's working fine as it's inlined by webpack, need to double check
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,49 +38,43 @@ 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
- const precacheManifest = self.__WB_MANIFEST;
65
+ // eslint-disable-next-line no-underscore-dangle
66
+ const precacheManifest = (
67
+ self as typeof globalThis & {__WB_MANIFEST: (string | PrecacheEntry)[]}
68
+ ).__WB_MANIFEST;
74
69
  const controller = new PrecacheController({
75
- fallbackToNetwork: true, // safer to turn this true?
70
+ // Safer to turn this true?
71
+ fallbackToNetwork: true,
76
72
  });
77
73
 
78
74
  if (params.offlineMode) {
79
75
  controller.addToCacheList(precacheManifest);
80
76
  if (params.debug) {
81
- console.log('[Docusaurus-PWA][SW]: addToCacheList', {
82
- precacheManifest,
83
- });
77
+ console.log('[Docusaurus-PWA][SW]: addToCacheList', {precacheManifest});
84
78
  }
85
79
  }
86
80
 
@@ -88,31 +82,30 @@ function getPossibleURLs(url) {
88
82
 
89
83
  self.addEventListener('install', (event) => {
90
84
  if (params.debug) {
91
- console.log('[Docusaurus-PWA][SW]: install event', {
92
- event,
93
- });
85
+ console.log('[Docusaurus-PWA][SW]: install event', {event});
94
86
  }
95
- event.waitUntil(controller.install(event));
87
+ (event as ExtendableEvent).waitUntil(
88
+ controller.install(event as ExtendableEvent),
89
+ );
96
90
  });
97
91
 
98
92
  self.addEventListener('activate', (event) => {
99
93
  if (params.debug) {
100
- console.log('[Docusaurus-PWA][SW]: activate event', {
101
- event,
102
- });
94
+ console.log('[Docusaurus-PWA][SW]: activate event', {event});
103
95
  }
104
- event.waitUntil(controller.activate(event));
96
+ (event as ExtendableEvent).waitUntil(
97
+ controller.activate(event as ExtendableEvent),
98
+ );
105
99
  });
106
100
 
107
101
  self.addEventListener('fetch', async (event) => {
108
102
  if (params.offlineMode) {
109
- const requestURL = event.request.url;
103
+ const requestURL = (event as FetchEvent).request.url;
110
104
  const possibleURLs = getPossibleURLs(requestURL);
111
- for (let i = 0; i < possibleURLs.length; i += 1) {
112
- const possibleURL = possibleURLs[i];
105
+ for (const possibleURL of possibleURLs) {
113
106
  const cacheKey = controller.getCacheKeyForURL(possibleURL);
114
107
  if (cacheKey) {
115
- const cachedResponse = caches.match(cacheKey);
108
+ const cachedResponse = caches.match(cacheKey) as Promise<Response>;
116
109
  if (params.debug) {
117
110
  console.log('[Docusaurus-PWA][SW]: serving cached asset', {
118
111
  requestURL,
@@ -122,7 +115,7 @@ function getPossibleURLs(url) {
122
115
  cachedResponse,
123
116
  });
124
117
  }
125
- event.respondWith(cachedResponse);
118
+ (event as FetchEvent).respondWith(cachedResponse);
126
119
  break;
127
120
  }
128
121
  }
@@ -131,15 +124,14 @@ function getPossibleURLs(url) {
131
124
 
132
125
  self.addEventListener('message', async (event) => {
133
126
  if (params.debug) {
134
- console.log('[Docusaurus-PWA][SW]: message event', {
135
- event,
136
- });
127
+ console.log('[Docusaurus-PWA][SW]: message event', {event});
137
128
  }
138
129
 
139
- const type = event.data && event.data.type;
130
+ const type = (event as MessageEvent).data?.type;
140
131
 
141
132
  if (type === 'SKIP_WAITING') {
142
- self.skipWaiting();
133
+ // lib def bug, see https://github.com/microsoft/TypeScript/issues/14877
134
+ (self as typeof globalThis & ServiceWorkerGlobalScope).skipWaiting();
143
135
  }
144
136
  });
145
137
  })();
@@ -0,0 +1,59 @@
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
+ */
7
+
8
+ import React, {useState} from 'react';
9
+ import clsx from 'clsx';
10
+ import Translate, {translate} from '@docusaurus/Translate';
11
+ import type {Props} from '@theme/PwaReloadPopup';
12
+
13
+ import styles from './styles.module.css';
14
+
15
+ export default function PwaReloadPopup({onReload}: Props): JSX.Element | false {
16
+ const [isVisible, setIsVisible] = useState(true);
17
+
18
+ return (
19
+ isVisible && (
20
+ <div className={clsx('alert', 'alert--secondary', styles.popup)}>
21
+ <p>
22
+ <Translate
23
+ id="theme.PwaReloadPopup.info"
24
+ description="The text for PWA reload popup">
25
+ New version available
26
+ </Translate>
27
+ </p>
28
+ <div className={styles.buttonContainer}>
29
+ <button
30
+ className="button button--link"
31
+ type="button"
32
+ onClick={() => {
33
+ setIsVisible(false);
34
+ onReload();
35
+ }}>
36
+ <Translate
37
+ id="theme.PwaReloadPopup.refreshButtonText"
38
+ description="The text for PWA reload button">
39
+ Refresh
40
+ </Translate>
41
+ </button>
42
+
43
+ <button
44
+ aria-label={translate({
45
+ id: 'theme.PwaReloadPopup.closeButtonAriaLabel',
46
+ message: 'Close',
47
+ description:
48
+ 'The ARIA label for close button of PWA reload popup',
49
+ })}
50
+ className="close"
51
+ type="button"
52
+ onClick={() => setIsVisible(false)}>
53
+ <span aria-hidden="true">×</span>
54
+ </button>
55
+ </div>
56
+ </div>
57
+ )
58
+ );
59
+ }
package/src/types.d.ts ADDED
@@ -0,0 +1,8 @@
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
+ */
7
+
8
+ /// <reference types="@docusaurus/module-type-aliases" />