From 738c5ceaf65c27722fab5855d651ee32691e4bb7 Mon Sep 17 00:00:00 2001 From: Prateek Bhatnagar Date: Tue, 19 Sep 2017 15:52:41 +0530 Subject: [PATCH] adding flag to disable service worker --- README.md | 9 +++++---- src/commands/build.js | 4 ++++ src/lib/entry.js | 2 +- src/lib/webpack/webpack-client-config.js | 7 ++++++- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e6d439fb4..635f08a7f 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,8 @@ $ preact build --src Entry file (index.js). [default: "src"] --dest Directory root for output. [default: "build"] --production, -p Create a minified production build. [default: true] - --no-prerender Disable pre-render of static app content. + --no-prerender Disable pre-render of static app content. + --service-worker Add a service worker to application. [default: true] --prerenderUrls Path to pre-render routes configuration. [default "prerender-urls.json"] --template Path to template file. --clean Clear output directory before building. [default: true] @@ -133,16 +134,16 @@ $ preact serve ### Templates -### Full: +### Full: - Preact Router Included - Dynamic routes Demo -### Simple: +### Simple: -- Github API +- Github API Demo ### Empty diff --git a/src/commands/build.js b/src/commands/build.js index 09bd3b4d4..36ee1a991 100644 --- a/src/commands/build.js +++ b/src/commands/build.js @@ -31,6 +31,10 @@ export default asyncCommand({ description: 'Path to pre-render routes configuration.', default: 'prerender-urls.json' }, + 'service-worker': { + description: 'Add a service worker to the application.', + default: true + }, clean: { description: 'Clear output directory before building.', default: true diff --git a/src/lib/entry.js b/src/lib/entry.js index 420231a38..c770705e2 100644 --- a/src/lib/entry.js +++ b/src/lib/entry.js @@ -4,7 +4,7 @@ if (process.env.NODE_ENV==='development') { // enable preact devtools require('preact/devtools'); } -else if ('serviceWorker' in navigator && location.protocol === 'https:') { +else if (process.env.ADD_SW && 'serviceWorker' in navigator && location.protocol === 'https:') { navigator.serviceWorker.register('/sw.js'); } diff --git a/src/lib/webpack/webpack-client-config.js b/src/lib/webpack/webpack-client-config.js index a344a0c37..d61b80f2d 100644 --- a/src/lib/webpack/webpack-client-config.js +++ b/src/lib/webpack/webpack-client-config.js @@ -199,7 +199,12 @@ const production = config => addPlugins([ /push-manifest\.json$/, /.DS_Store/ ] - }) + }), + new webpack.DefinePlugin({ + 'process.env': { + 'ADD_SW': config['service-worker'] + } + }), ]); const htmlPlugin = (config, src) => {