@docusaurus/plugin-pwa 2.0.0-beta.fbdeefcac → 2.0.0-rc.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/lib/index.d.ts +10 -0
- package/lib/index.js +164 -0
- package/lib/options.d.ts +9 -0
- package/lib/options.js +46 -0
- package/lib/registerSw.d.ts +18 -0
- package/lib/registerSw.js +186 -0
- package/lib/renderReloadPopup.d.ts +8 -0
- package/lib/renderReloadPopup.js +22 -0
- package/lib/sw.d.ts +7 -0
- package/lib/sw.js +111 -0
- package/lib/theme/PwaReloadPopup/index.d.ts +9 -0
- package/{src → lib}/theme/PwaReloadPopup/index.js +0 -4
- package/lib/theme/PwaReloadPopup/styles.module.css +39 -0
- package/package.json +35 -19
- package/src/deps.d.ts +15 -0
- package/src/{index.js → index.ts} +72 -45
- package/src/{pluginOptionSchema.js → options.ts} +17 -8
- package/src/plugin-pwa.d.ts +88 -0
- package/src/registerSw.ts +237 -0
- package/src/{renderReloadPopup.js → renderReloadPopup.tsx} +7 -5
- package/src/{sw.js → sw.ts} +36 -44
- package/src/theme/PwaReloadPopup/index.tsx +59 -0
- package/src/types.d.ts +8 -0
- package/src/registerSw.js +0 -303
package/package.json
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/plugin-pwa",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-rc.1",
|
|
4
4
|
"description": "Docusaurus Plugin to add PWA support.",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "src/plugin-pwa.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc --build && node ../../admin/scripts/copyUntypedFiles.js && prettier --config ../../.prettierrc --write \"lib/theme/**/*.js\"",
|
|
9
|
+
"watch": "run-p -c copy:watch build:watch",
|
|
10
|
+
"build:watch": "tsc --build --watch",
|
|
11
|
+
"copy:watch": "node ../../admin/scripts/copyUntypedFiles.js --watch"
|
|
12
|
+
},
|
|
6
13
|
"publishConfig": {
|
|
7
14
|
"access": "public"
|
|
8
15
|
},
|
|
@@ -13,26 +20,35 @@
|
|
|
13
20
|
},
|
|
14
21
|
"license": "MIT",
|
|
15
22
|
"dependencies": {
|
|
16
|
-
"@babel/
|
|
17
|
-
"@babel/
|
|
18
|
-
"@
|
|
19
|
-
"@docusaurus/
|
|
20
|
-
"@docusaurus/theme-
|
|
21
|
-
"@docusaurus/
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"webpack-
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
23
|
+
"@babel/core": "^7.18.6",
|
|
24
|
+
"@babel/preset-env": "^7.18.6",
|
|
25
|
+
"@docusaurus/core": "2.0.0-rc.1",
|
|
26
|
+
"@docusaurus/theme-common": "2.0.0-rc.1",
|
|
27
|
+
"@docusaurus/theme-translations": "2.0.0-rc.1",
|
|
28
|
+
"@docusaurus/types": "2.0.0-rc.1",
|
|
29
|
+
"@docusaurus/utils": "2.0.0-rc.1",
|
|
30
|
+
"@docusaurus/utils-validation": "2.0.0-rc.1",
|
|
31
|
+
"babel-loader": "^8.2.5",
|
|
32
|
+
"clsx": "^1.2.1",
|
|
33
|
+
"core-js": "^3.23.3",
|
|
34
|
+
"terser-webpack-plugin": "^5.3.3",
|
|
35
|
+
"tslib": "^2.4.0",
|
|
36
|
+
"webpack": "^5.73.0",
|
|
37
|
+
"webpack-merge": "^5.8.0",
|
|
38
|
+
"workbox-build": "^6.5.3",
|
|
39
|
+
"workbox-precaching": "^6.5.3",
|
|
40
|
+
"workbox-window": "^6.5.3"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@docusaurus/module-type-aliases": "2.0.0-rc.1",
|
|
44
|
+
"fs-extra": "^10.1.0"
|
|
31
45
|
},
|
|
32
46
|
"peerDependencies": {
|
|
33
|
-
"@babel/core": "^7.0.0",
|
|
34
47
|
"react": "^16.8.4 || ^17.0.0",
|
|
35
48
|
"react-dom": "^16.8.4 || ^17.0.0"
|
|
36
49
|
},
|
|
37
|
-
"
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=16.14"
|
|
52
|
+
},
|
|
53
|
+
"gitHead": "c8ddd02a8e68dfaf515c20465a049a83153bd205"
|
|
38
54
|
}
|
package/src/deps.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
// TODO incompatible declaration file: https://github.com/unjs/webpackbar/pull/108
|
|
9
|
+
declare module 'webpackbar' {
|
|
10
|
+
import webpack from 'webpack';
|
|
11
|
+
|
|
12
|
+
export default class WebpackBarPlugin extends webpack.ProgressPlugin {
|
|
13
|
+
constructor(options: {name: string; color?: string});
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -5,14 +5,16 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import webpack, {type Configuration} from 'webpack';
|
|
10
|
+
import Terser from 'terser-webpack-plugin';
|
|
11
|
+
import {injectManifest} from 'workbox-build';
|
|
12
|
+
import {normalizeUrl} from '@docusaurus/utils';
|
|
13
|
+
import {compile} from '@docusaurus/core/lib/webpack/utils';
|
|
14
|
+
import LogPlugin from '@docusaurus/core/lib/webpack/plugins/LogPlugin';
|
|
15
|
+
import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations';
|
|
16
|
+
import type {HtmlTags, LoadContext, Plugin} from '@docusaurus/types';
|
|
17
|
+
import type {PluginOptions} from '@docusaurus/plugin-pwa';
|
|
16
18
|
|
|
17
19
|
const isProd = process.env.NODE_ENV === 'production';
|
|
18
20
|
|
|
@@ -33,22 +35,23 @@ function getSWBabelLoader() {
|
|
|
33
35
|
},
|
|
34
36
|
],
|
|
35
37
|
],
|
|
36
|
-
plugins: [
|
|
37
|
-
require.resolve('@babel/plugin-proposal-object-rest-spread'),
|
|
38
|
-
require.resolve('@babel/plugin-proposal-optional-chaining'),
|
|
39
|
-
require.resolve('@babel/plugin-proposal-nullish-coalescing-operator'),
|
|
40
|
-
],
|
|
41
38
|
},
|
|
42
39
|
};
|
|
43
40
|
}
|
|
44
41
|
|
|
45
|
-
function
|
|
46
|
-
|
|
42
|
+
export default function pluginPWA(
|
|
43
|
+
context: LoadContext,
|
|
44
|
+
options: PluginOptions,
|
|
45
|
+
): Plugin<void> {
|
|
46
|
+
const {
|
|
47
|
+
outDir,
|
|
48
|
+
baseUrl,
|
|
49
|
+
i18n: {currentLocale},
|
|
50
|
+
} = context;
|
|
47
51
|
const {
|
|
48
52
|
debug,
|
|
49
53
|
offlineModeActivationStrategies,
|
|
50
54
|
injectManifestConfig,
|
|
51
|
-
reloadPopup,
|
|
52
55
|
pwaHead,
|
|
53
56
|
swCustom,
|
|
54
57
|
swRegister,
|
|
@@ -58,11 +61,21 @@ function plugin(context, options) {
|
|
|
58
61
|
name: 'docusaurus-plugin-pwa',
|
|
59
62
|
|
|
60
63
|
getThemePath() {
|
|
61
|
-
return
|
|
64
|
+
return '../lib/theme';
|
|
65
|
+
},
|
|
66
|
+
getTypeScriptThemePath() {
|
|
67
|
+
return '../src/theme';
|
|
62
68
|
},
|
|
63
69
|
|
|
64
70
|
getClientModules() {
|
|
65
|
-
return isProd ? [swRegister] : [];
|
|
71
|
+
return isProd && swRegister ? [swRegister] : [];
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
getDefaultCodeTranslationMessages() {
|
|
75
|
+
return readDefaultCodeTranslationMessages({
|
|
76
|
+
locale: currentLocale,
|
|
77
|
+
name: 'plugin-pwa',
|
|
78
|
+
});
|
|
66
79
|
},
|
|
67
80
|
|
|
68
81
|
configureWebpack(config) {
|
|
@@ -75,25 +88,40 @@ function plugin(context, options) {
|
|
|
75
88
|
new webpack.EnvironmentPlugin({
|
|
76
89
|
PWA_DEBUG: debug,
|
|
77
90
|
PWA_SERVICE_WORKER_URL: path.posix.resolve(
|
|
78
|
-
`${config.output
|
|
91
|
+
`${(config.output?.publicPath as string) || '/'}`,
|
|
79
92
|
'sw.js',
|
|
80
93
|
),
|
|
81
|
-
PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES:
|
|
82
|
-
|
|
94
|
+
PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES:
|
|
95
|
+
offlineModeActivationStrategies,
|
|
83
96
|
}),
|
|
84
97
|
],
|
|
85
98
|
};
|
|
86
99
|
},
|
|
87
100
|
|
|
88
101
|
injectHtmlTags() {
|
|
89
|
-
const headTags = [];
|
|
90
|
-
if (isProd
|
|
91
|
-
pwaHead.forEach(({tagName, ...attributes}) =>
|
|
92
|
-
|
|
102
|
+
const headTags: HtmlTags = [];
|
|
103
|
+
if (isProd) {
|
|
104
|
+
pwaHead.forEach(({tagName, ...attributes}) => {
|
|
105
|
+
(['href', 'content'] as const).forEach((attribute) => {
|
|
106
|
+
const attributeValue = attributes[attribute];
|
|
107
|
+
|
|
108
|
+
if (!attributeValue) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const attributePath =
|
|
113
|
+
!!path.extname(attributeValue) && attributeValue;
|
|
114
|
+
|
|
115
|
+
if (attributePath && !attributePath.startsWith(baseUrl)) {
|
|
116
|
+
attributes[attribute] = normalizeUrl([baseUrl, attributeValue]);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return headTags.push({
|
|
93
121
|
tagName,
|
|
94
122
|
attributes,
|
|
95
|
-
})
|
|
96
|
-
);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
97
125
|
}
|
|
98
126
|
return {headTags};
|
|
99
127
|
},
|
|
@@ -105,8 +133,8 @@ function plugin(context, options) {
|
|
|
105
133
|
|
|
106
134
|
const swSourceFileTest = /\.m?js$/;
|
|
107
135
|
|
|
108
|
-
const swWebpackConfig = {
|
|
109
|
-
entry:
|
|
136
|
+
const swWebpackConfig: Configuration = {
|
|
137
|
+
entry: require.resolve('./sw.js'),
|
|
110
138
|
output: {
|
|
111
139
|
path: outDir,
|
|
112
140
|
filename: 'sw.js',
|
|
@@ -118,17 +146,19 @@ function plugin(context, options) {
|
|
|
118
146
|
optimization: {
|
|
119
147
|
splitChunks: false,
|
|
120
148
|
minimize: !debug,
|
|
121
|
-
//
|
|
122
|
-
minimizer:
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
149
|
+
// See https://developers.google.com/web/tools/workbox/guides/using-bundlers#webpack
|
|
150
|
+
minimizer: debug
|
|
151
|
+
? []
|
|
152
|
+
: [
|
|
153
|
+
new Terser({
|
|
154
|
+
test: swSourceFileTest,
|
|
155
|
+
}),
|
|
156
|
+
],
|
|
128
157
|
},
|
|
129
158
|
plugins: [
|
|
130
159
|
new webpack.EnvironmentPlugin({
|
|
131
|
-
|
|
160
|
+
// Fallback value required with Webpack 5
|
|
161
|
+
PWA_SW_CUSTOM: swCustom ?? '',
|
|
132
162
|
}),
|
|
133
163
|
new LogPlugin({
|
|
134
164
|
name: 'Service Worker',
|
|
@@ -139,7 +169,7 @@ function plugin(context, options) {
|
|
|
139
169
|
rules: [
|
|
140
170
|
{
|
|
141
171
|
test: swSourceFileTest,
|
|
142
|
-
exclude: /
|
|
172
|
+
exclude: /node_modules/,
|
|
143
173
|
use: getSWBabelLoader(),
|
|
144
174
|
},
|
|
145
175
|
],
|
|
@@ -156,9 +186,10 @@ function plugin(context, options) {
|
|
|
156
186
|
'**/*.{js,json,css,html}',
|
|
157
187
|
'**/*.{png,jpg,jpeg,gif,svg,ico}',
|
|
158
188
|
'**/*.{woff,woff2,eot,ttf,otf}',
|
|
159
|
-
|
|
189
|
+
// @ts-expect-error: internal API?
|
|
190
|
+
...((injectManifest.globPatterns as string[] | undefined) ?? []),
|
|
160
191
|
],
|
|
161
|
-
//
|
|
192
|
+
// Those attributes are not overrideable
|
|
162
193
|
swDest,
|
|
163
194
|
swSrc: swDest,
|
|
164
195
|
globDirectory: props.outDir,
|
|
@@ -167,8 +198,4 @@ function plugin(context, options) {
|
|
|
167
198
|
};
|
|
168
199
|
}
|
|
169
200
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
plugin.validateOptions = function validateOptions({validate, options}) {
|
|
173
|
-
return validate(PluginOptionSchema, options);
|
|
174
|
-
};
|
|
201
|
+
export {validateOptions} from './options';
|
|
@@ -5,8 +5,9 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
import {Joi} from '@docusaurus/utils-validation';
|
|
9
|
+
import type {OptionValidationContext} from '@docusaurus/types';
|
|
10
|
+
import type {PluginOptions} from '@docusaurus/plugin-pwa';
|
|
10
11
|
|
|
11
12
|
const DEFAULT_OPTIONS = {
|
|
12
13
|
debug: false,
|
|
@@ -18,11 +19,10 @@ const DEFAULT_OPTIONS = {
|
|
|
18
19
|
injectManifestConfig: {},
|
|
19
20
|
pwaHead: [],
|
|
20
21
|
swCustom: undefined,
|
|
21
|
-
swRegister:
|
|
22
|
-
reloadPopup: '@theme/PwaReloadPopup',
|
|
22
|
+
swRegister: './registerSw.js',
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
const optionsSchema = Joi.object<PluginOptions>({
|
|
26
26
|
debug: Joi.bool().default(DEFAULT_OPTIONS.debug),
|
|
27
27
|
offlineModeActivationStrategies: Joi.array()
|
|
28
28
|
.items(
|
|
@@ -48,7 +48,16 @@ exports.PluginOptionSchema = Joi.object({
|
|
|
48
48
|
swRegister: Joi.alternatives()
|
|
49
49
|
.try(Joi.string(), Joi.bool().valid(false))
|
|
50
50
|
.default(DEFAULT_OPTIONS.swRegister),
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
.
|
|
51
|
+
// @ts-expect-error: forbidden
|
|
52
|
+
reloadPopup: Joi.any().forbidden().messages({
|
|
53
|
+
'any.unknown':
|
|
54
|
+
'The reloadPopup option is removed in favor of swizzling. See https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-pwa#customizing-reload-popup for how to customize the reload popup using swizzling.',
|
|
55
|
+
}),
|
|
54
56
|
});
|
|
57
|
+
|
|
58
|
+
export function validateOptions({
|
|
59
|
+
validate,
|
|
60
|
+
options,
|
|
61
|
+
}: OptionValidationContext<PluginOptions, PluginOptions>): PluginOptions {
|
|
62
|
+
return validate(optionsSchema, options);
|
|
63
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
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
|
+
declare module '@docusaurus/plugin-pwa' {
|
|
9
|
+
import type {InjectManifestOptions} from 'workbox-build';
|
|
10
|
+
|
|
11
|
+
export type PluginOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* Turn debug mode on:
|
|
14
|
+
*
|
|
15
|
+
* - Workbox logs
|
|
16
|
+
* - Additional Docusaurus logs
|
|
17
|
+
* - Unoptimized SW file output
|
|
18
|
+
* - Source maps
|
|
19
|
+
*/
|
|
20
|
+
debug?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Strategies used to turn the offline mode on:
|
|
23
|
+
*
|
|
24
|
+
* - `appInstalled`: activates for users having installed the site as an app
|
|
25
|
+
* (not 100% reliable)
|
|
26
|
+
* - `standalone`: activates for users running the app as standalone (often
|
|
27
|
+
* the case once a PWA is installed)
|
|
28
|
+
* - `queryString`: activates if queryString contains `offlineMode=true`
|
|
29
|
+
* (convenient for PWA debugging)
|
|
30
|
+
* - `mobile`: activates for mobile users (width <= 940px)
|
|
31
|
+
* - `saveData`: activates for users with `navigator.connection.saveData ===
|
|
32
|
+
* true`
|
|
33
|
+
* - `always`: activates for all users
|
|
34
|
+
*/
|
|
35
|
+
offlineModeActivationStrategies: (
|
|
36
|
+
| 'appInstalled'
|
|
37
|
+
| 'queryString'
|
|
38
|
+
| 'standalone'
|
|
39
|
+
| 'mobile'
|
|
40
|
+
| 'saveData'
|
|
41
|
+
| 'always'
|
|
42
|
+
)[];
|
|
43
|
+
/**
|
|
44
|
+
* Workbox options to pass to `workbox.injectManifest()`. This gives you
|
|
45
|
+
* control over which assets will be precached, and be available offline.
|
|
46
|
+
* @see https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#.injectManifest
|
|
47
|
+
*/
|
|
48
|
+
injectManifestConfig: InjectManifestOptions;
|
|
49
|
+
/**
|
|
50
|
+
* Array of objects containing `tagName` and key-value pairs for attributes
|
|
51
|
+
* to inject into the `<head>` tag. Technically you can inject any head tag
|
|
52
|
+
* through this, but it's ideally used for tags to make your site PWA-
|
|
53
|
+
* compliant.
|
|
54
|
+
*/
|
|
55
|
+
pwaHead: {
|
|
56
|
+
tagName: string;
|
|
57
|
+
href?: string;
|
|
58
|
+
content?: string;
|
|
59
|
+
[attributeName: string]: string | boolean | undefined;
|
|
60
|
+
}[];
|
|
61
|
+
/**
|
|
62
|
+
* Useful for additional Workbox rules. You can do whatever a service worker
|
|
63
|
+
* can do here, and use the full power of workbox libraries. The code is
|
|
64
|
+
* transpiled, so you can use modern ES6+ syntax here.
|
|
65
|
+
*/
|
|
66
|
+
swCustom?: string;
|
|
67
|
+
/**
|
|
68
|
+
* Adds an entry before the Docusaurus app so that registration can happen
|
|
69
|
+
* before the app runs. The default `registerSW.js` file is enough for
|
|
70
|
+
* simple registration. Passing `false` will disable registration entirely.
|
|
71
|
+
*/
|
|
72
|
+
swRegister: string | false;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export type Options = Partial<PluginOptions>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
declare module '@theme/PwaReloadPopup' {
|
|
79
|
+
export interface Props {
|
|
80
|
+
/**
|
|
81
|
+
* The popup should call this callback when the `reload` button is clicked.
|
|
82
|
+
* This will tell the service worker to install the waiting service worker
|
|
83
|
+
* and reload the page.
|
|
84
|
+
*/
|
|
85
|
+
readonly onReload: () => void;
|
|
86
|
+
}
|
|
87
|
+
export default function PwaReloadPopup(props: Props): JSX.Element;
|
|
88
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
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 ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
|
|
9
|
+
import {createStorageSlot} from '@docusaurus/theme-common';
|
|
10
|
+
|
|
11
|
+
// First: read the env variables (provided by Webpack)
|
|
12
|
+
/* eslint-disable prefer-destructuring */
|
|
13
|
+
const PWA_SERVICE_WORKER_URL = process.env.PWA_SERVICE_WORKER_URL!;
|
|
14
|
+
const PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES = process.env
|
|
15
|
+
.PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES as unknown as (keyof typeof OfflineModeActivationStrategiesImplementations)[];
|
|
16
|
+
const PWA_DEBUG = process.env.PWA_DEBUG;
|
|
17
|
+
/* eslint-enable prefer-destructuring */
|
|
18
|
+
|
|
19
|
+
const MAX_MOBILE_WIDTH = 996;
|
|
20
|
+
|
|
21
|
+
const AppInstalledEventFiredStorage = createStorageSlot(
|
|
22
|
+
'docusaurus.pwa.event.appInstalled.fired',
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
declare global {
|
|
26
|
+
interface Navigator {
|
|
27
|
+
getInstalledRelatedApps: () => Promise<{platform: string}[]>;
|
|
28
|
+
connection?: {effectiveType: string; saveData: boolean};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function debugLog(msg: string, obj?: unknown) {
|
|
33
|
+
if (PWA_DEBUG) {
|
|
34
|
+
if (typeof obj === 'undefined') {
|
|
35
|
+
console.log(`[Docusaurus-PWA][registerSw]: ${msg}`);
|
|
36
|
+
} else {
|
|
37
|
+
console.log(`[Docusaurus-PWA][registerSw]: ${msg}`, obj);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function clearRegistrations() {
|
|
43
|
+
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
44
|
+
debugLog('will unregister all service workers', {registrations});
|
|
45
|
+
await Promise.all(
|
|
46
|
+
registrations.map((registration) =>
|
|
47
|
+
registration
|
|
48
|
+
.unregister()
|
|
49
|
+
.then((result) =>
|
|
50
|
+
debugLog('unregister service worker', {registration, result}),
|
|
51
|
+
),
|
|
52
|
+
),
|
|
53
|
+
);
|
|
54
|
+
debugLog('unregistered all service workers', {registrations});
|
|
55
|
+
window.location.reload();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/*
|
|
59
|
+
As of 2021:
|
|
60
|
+
It is complicated and not very reliable to detect an app is actually installed.
|
|
61
|
+
https://stackoverflow.com/questions/51735869/check-if-user-has-already-installed-pwa-to-homescreen-on-chrome
|
|
62
|
+
|
|
63
|
+
- appinstalled event is not in the spec anymore and seems to not fire? https://firt.dev/pwa-2021#less-capabilities-%E2%98%B9%EF%B8%8F
|
|
64
|
+
- getInstalledRelatedApps() is only supported in recent Chrome and does not seem to reliable either https://github.com/WICG/get-installed-related-apps
|
|
65
|
+
- display-mode: standalone is not exactly the same concept, but looks like a decent fallback https://petelepage.com/blog/2019/07/is-my-pwa-installed/
|
|
66
|
+
*/
|
|
67
|
+
function getIsAppInstalledEventFired() {
|
|
68
|
+
return AppInstalledEventFiredStorage.get() === 'true';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function getIsAppInstalledRelatedApps() {
|
|
72
|
+
if (!('getInstalledRelatedApps' in window.navigator)) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const relatedApps = await navigator.getInstalledRelatedApps();
|
|
76
|
+
return relatedApps.some((app) => app.platform === 'webapp');
|
|
77
|
+
}
|
|
78
|
+
function isStandaloneDisplayMode() {
|
|
79
|
+
return window.matchMedia('(display-mode: standalone)').matches;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const OfflineModeActivationStrategiesImplementations = {
|
|
83
|
+
always: () => true,
|
|
84
|
+
mobile: () => window.innerWidth <= MAX_MOBILE_WIDTH,
|
|
85
|
+
saveData: () => !!navigator.connection?.saveData,
|
|
86
|
+
appInstalled: () =>
|
|
87
|
+
getIsAppInstalledEventFired() || getIsAppInstalledRelatedApps(),
|
|
88
|
+
standalone: () => isStandaloneDisplayMode(),
|
|
89
|
+
queryString: () =>
|
|
90
|
+
new URLSearchParams(window.location.search).get('offlineMode') === 'true',
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
async function getActiveStrategies() {
|
|
94
|
+
const activeStrategies = await Promise.all(
|
|
95
|
+
PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES.map((strategyName) =>
|
|
96
|
+
Promise.resolve(
|
|
97
|
+
OfflineModeActivationStrategiesImplementations[strategyName](),
|
|
98
|
+
).then((isActive) => (isActive ? strategyName : undefined)),
|
|
99
|
+
),
|
|
100
|
+
);
|
|
101
|
+
return activeStrategies.filter(Boolean);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function getIsOfflineModeEnabled() {
|
|
105
|
+
const activeStrategies = await getActiveStrategies();
|
|
106
|
+
const enabled = activeStrategies.length > 0;
|
|
107
|
+
debugLog(
|
|
108
|
+
enabled
|
|
109
|
+
? 'offline mode enabled, because of activation strategies'
|
|
110
|
+
: 'offline mode disabled, because none of the offlineModeActivationStrategies could be used',
|
|
111
|
+
{
|
|
112
|
+
activeStrategies,
|
|
113
|
+
availableStrategies: PWA_OFFLINE_MODE_ACTIVATION_STRATEGIES,
|
|
114
|
+
},
|
|
115
|
+
);
|
|
116
|
+
return enabled;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function createServiceWorkerUrl(params: object) {
|
|
120
|
+
const paramsQueryString = JSON.stringify(params);
|
|
121
|
+
const url = `${PWA_SERVICE_WORKER_URL}?params=${encodeURIComponent(
|
|
122
|
+
paramsQueryString,
|
|
123
|
+
)}`;
|
|
124
|
+
debugLog('service worker url', {url, params});
|
|
125
|
+
return url;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function registerSW() {
|
|
129
|
+
const [{Workbox}, offlineMode] = await Promise.all([
|
|
130
|
+
import('workbox-window'),
|
|
131
|
+
getIsOfflineModeEnabled(),
|
|
132
|
+
]);
|
|
133
|
+
const url = createServiceWorkerUrl({offlineMode, debug: PWA_DEBUG});
|
|
134
|
+
const wb = new Workbox(url);
|
|
135
|
+
const sendSkipWaiting = () => wb.messageSW({type: 'SKIP_WAITING'});
|
|
136
|
+
const handleServiceWorkerWaiting = () => {
|
|
137
|
+
debugLog('handleServiceWorkerWaiting');
|
|
138
|
+
// Immediately load new service worker when files aren't cached
|
|
139
|
+
if (!offlineMode) {
|
|
140
|
+
return sendSkipWaiting();
|
|
141
|
+
}
|
|
142
|
+
return import('./renderReloadPopup.js').then(({renderReloadPopup}) =>
|
|
143
|
+
renderReloadPopup({
|
|
144
|
+
onReload() {
|
|
145
|
+
wb.addEventListener('controlling', () => {
|
|
146
|
+
window.location.reload();
|
|
147
|
+
});
|
|
148
|
+
sendSkipWaiting();
|
|
149
|
+
},
|
|
150
|
+
}),
|
|
151
|
+
);
|
|
152
|
+
};
|
|
153
|
+
// Update the current service worker when the next one has finished
|
|
154
|
+
// installing and transitions to waiting state.
|
|
155
|
+
wb.addEventListener('waiting', (event) => {
|
|
156
|
+
debugLog('event waiting', {event});
|
|
157
|
+
void handleServiceWorkerWaiting();
|
|
158
|
+
});
|
|
159
|
+
// Update current service worker if the next one finishes installing and
|
|
160
|
+
// moves to waiting state in another tab.
|
|
161
|
+
// @ts-expect-error: not present in the API typings anymore
|
|
162
|
+
wb.addEventListener('externalwaiting', (event) => {
|
|
163
|
+
debugLog('event externalwaiting', {event});
|
|
164
|
+
void handleServiceWorkerWaiting();
|
|
165
|
+
});
|
|
166
|
+
const registration = await wb.register();
|
|
167
|
+
if (registration) {
|
|
168
|
+
if (registration.active) {
|
|
169
|
+
debugLog('registration.active', {registration});
|
|
170
|
+
}
|
|
171
|
+
if (registration.installing) {
|
|
172
|
+
debugLog('registration.installing', {registration});
|
|
173
|
+
}
|
|
174
|
+
if (registration.waiting) {
|
|
175
|
+
debugLog('registration.waiting', {registration});
|
|
176
|
+
// Update service worker if the next one is already in the waiting
|
|
177
|
+
// state. This happens when the user doesn't click on `reload` in
|
|
178
|
+
// the popup.
|
|
179
|
+
await handleServiceWorkerWaiting();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// TODO these events still works in chrome but have been removed from the spec
|
|
185
|
+
// in 2019! See https://github.com/w3c/manifest/pull/836
|
|
186
|
+
function addLegacyAppInstalledEventsListeners() {
|
|
187
|
+
debugLog('addLegacyAppInstalledEventsListeners');
|
|
188
|
+
|
|
189
|
+
window.addEventListener('appinstalled', (event) => {
|
|
190
|
+
debugLog('event appinstalled', {event});
|
|
191
|
+
AppInstalledEventFiredStorage.set('true');
|
|
192
|
+
debugLog("AppInstalledEventFiredStorage.set('true')");
|
|
193
|
+
// After the app is installed, we register a service worker with the path
|
|
194
|
+
// `/sw?enabled`. Since the previous service worker was `/sw`, it'll be
|
|
195
|
+
// treated as a new one. The previous registration will need to be
|
|
196
|
+
// cleared, otherwise the reload popup will show.
|
|
197
|
+
void clearRegistrations();
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// TODO this event still works in chrome but has been removed from the spec
|
|
201
|
+
// in 2019!!!
|
|
202
|
+
window.addEventListener('beforeinstallprompt', (event) => {
|
|
203
|
+
debugLog('event beforeinstallprompt', {event});
|
|
204
|
+
// TODO instead of default browser install UI, show custom docusaurus
|
|
205
|
+
// prompt?
|
|
206
|
+
// event.preventDefault();
|
|
207
|
+
const appInstalledEventFired = AppInstalledEventFiredStorage.get();
|
|
208
|
+
debugLog('AppInstalledEventFiredStorage.get()', {appInstalledEventFired});
|
|
209
|
+
if (appInstalledEventFired) {
|
|
210
|
+
AppInstalledEventFiredStorage.del();
|
|
211
|
+
debugLog('AppInstalledEventFiredStorage.del()');
|
|
212
|
+
// After uninstalling the app, if the user doesn't clear all data, then
|
|
213
|
+
// the previous service worker will continue serving cached files. We
|
|
214
|
+
// need to clear registrations and reload, otherwise the popup shows.
|
|
215
|
+
void clearRegistrations();
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
debugLog(
|
|
220
|
+
'legacy appinstalled and beforeinstallprompt event listeners installed',
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/*
|
|
225
|
+
Init code to run on the client!
|
|
226
|
+
*/
|
|
227
|
+
if (ExecutionEnvironment.canUseDOM) {
|
|
228
|
+
debugLog('debug mode enabled');
|
|
229
|
+
|
|
230
|
+
if ('serviceWorker' in navigator) {
|
|
231
|
+
// First: add the listeners asap/synchronously
|
|
232
|
+
addLegacyAppInstalledEventsListeners();
|
|
233
|
+
|
|
234
|
+
// Then try to register the SW using lazy/dynamic imports
|
|
235
|
+
registerSW().catch((e: unknown) => console.error('registerSW failed', e));
|
|
236
|
+
}
|
|
237
|
+
}
|
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import React from 'react';
|
|
9
|
-
import
|
|
9
|
+
import ReactDOM from 'react-dom';
|
|
10
|
+
import type {Props} from '@theme/PwaReloadPopup';
|
|
10
11
|
|
|
11
12
|
const POPUP_CONTAINER_ID = 'pwa-popup-container';
|
|
12
13
|
|
|
@@ -19,8 +20,9 @@ const createContainer = () => {
|
|
|
19
20
|
return container;
|
|
20
21
|
};
|
|
21
22
|
|
|
22
|
-
export
|
|
23
|
-
const container = getContainer()
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
export function renderReloadPopup(props: Props): Promise<void> {
|
|
24
|
+
const container = getContainer() ?? createContainer();
|
|
25
|
+
return import('@theme/PwaReloadPopup').then(({default: ReloadPopup}) => {
|
|
26
|
+
ReactDOM.render(<ReloadPopup {...props} />, container);
|
|
27
|
+
});
|
|
26
28
|
}
|