Skip to content

Commit 2d7cfd6

Browse files
committed
Adds workbox option to the factory of the standard Webpack config for apps
1 parent 70b3c8a commit 2d7cfd6

File tree

5 files changed

+45
-12
lines changed

5 files changed

+45
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Topcoder React Utils Changelog
22

3+
### v0.8.1
4+
- Adds `workbox` option to the Wepback configuration factory for app
5+
([**`config/webpack/app-base`**](docs/webpack-config.md#configuration-details)).
6+
37
### v0.7.2
48
Fix of `<Link>` and `<NavLink>` logic.
59

config/webpack/app-base.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ const WorkboxPlugin = require('workbox-webpack-plugin');
4141
* passed in, it is assigned to the "main" entry point, and the "polyfills"
4242
* entry point will be added to it.
4343
*
44-
* @param {Boolean|Object} ops.generateServiceWorker When the value is truly,
45-
* webpack-workbox-plugin is added to the config to generate a service worker.
46-
* If an object is passed into this param, it is used as options for the worker
47-
* generation.
44+
* @param {Boolean|Object} ops.workbox Adds InjectManifest plugin from Workbox,
45+
* with given options, if the argument is Object, or default ones, if it is any
46+
* other truly value.
4847
*
4948
* @param {Boolean} ops.keepBuildInfo Optional. If `true` and a build info file
5049
* from a previous build is found, the factory will use that rather than
@@ -112,13 +111,14 @@ module.exports = function configFactory(ops) {
112111
}),
113112
];
114113

115-
let swOps = o.generateServiceWorker;
116-
if (swOps) {
117-
if (!_.isObject(swOps)) swOps = {};
118-
plugins.push(new WorkboxPlugin.GenerateSW({
119-
clientsClaim: true,
120-
skipWaiting: true,
121-
...swOps,
114+
/* Adds InjectManifest plugin from WorkBox, if opted to. */
115+
if (o.workbox) {
116+
if (!_.isObject(o.workbox)) o.workbox = {};
117+
plugins.push(new WorkboxPlugin.InjectManifest({
118+
importWorkboxFrom: 'local',
119+
swSrc: path.resolve(__dirname, '../workbox/default.js'),
120+
...o.workbox,
121+
swDest: '__service-worker.js',
122122
}));
123123
}
124124

config/workbox/default.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Default Workbox config.
3+
*/
4+
5+
/* global self, workbox */
6+
7+
workbox.slipWaiting();
8+
workbox.clientsClaim();
9+
10+
/* eslint-disable no-restricted-globals, no-underscore-dangle */
11+
workbox.precaching.precacheAndRoute(self.__precachingManifest);
12+
/* eslint-enable no-restricted-globals, no-underscore-dangle */

docs/webpack-config.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ mutation of the config object.
5959
- **`mode`** &ndash; *String* &ndash; [Webpack mode](https://webpack.js.org/concepts/mode/).
6060
- **`publicPath`** &mdash; *String* &mdash; Base URL for the output of
6161
the build assets;
62+
- **`workbox`** &ndash; *Boolean*|*Object* &ndash; Optional. If evaluates to
63+
a truly value, [Workbox's InjectManifest plugin](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#injectmanifest_plugin)
64+
is added to the array of Webpack plugins, to generate service worker for
65+
browser. If the value is an object, it is merged into the options passed
66+
into the plugin, otherwise default options are used:
67+
```json
68+
{
69+
"importWorkboxFrom": "local",
70+
"swSrc": "topcoder-react-utils/config/workbox/default.js",
71+
"swDest": "__service-worker.js"
72+
}
73+
```
74+
If service worker is generated by this option, it will be automatically
75+
initiated at the client side by the standard
76+
[client-side initialization script](docs/client.md)
77+
provided by **topcoder-react-utils**. Note that `swDest`'s value cannot be
78+
overriden by config options provided via `workbox` object.
6279

6380
- The generated config will opt to:
6481
- Bundle the font assets (EOF, OTF, SVG, TTF, WOFF, WOFF2 files from

src/client/init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if (useServiceWorker) {
2222
window.addEventListener('load', async () => {
2323
try {
2424
const reg = await navigator
25-
.serviceWorker.register('/service-worker.js');
25+
.serviceWorker.register('/__service-worker.js');
2626
console.log('SW registered:', reg);
2727
} catch (err) {
2828
console.log('SW registration failed:', err);

0 commit comments

Comments
 (0)