From cbdff0db20d8b0dc22cab9d419d42517531ee766 Mon Sep 17 00:00:00 2001 From: Kencana Kesuma Date: Thu, 16 Jul 2020 15:13:13 +0800 Subject: [PATCH 1/4] GH-591 fix(sw-src-load): fix service worker being loaded from cdn issue - introduced new cli args to define swPath, default would be '/' - decoupled from using webpack public path, better to be implicit --- packages/cli/lib/index.js | 1 + packages/cli/lib/lib/entry.js | 8 +++----- packages/cli/lib/lib/webpack/webpack-client-config.js | 2 ++ 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/cli/lib/index.js b/packages/cli/lib/index.js index b8282cb9f..0fbfd4dfb 100755 --- a/packages/cli/lib/index.js +++ b/packages/cli/lib/index.js @@ -37,6 +37,7 @@ prog .option('--dest', 'Specify output directory', 'build') .option('--cwd', 'A directory to use instead of $PWD', '.') .option('--sw', 'Generate and attach a Service Worker', true) + .option('--swPath', 'Specify service worker file location, default is root', '/') .option('--json', 'Generate build stats for bundle analysis') .option('--template', 'Path to custom HTML template') .option('--preload', 'Adds preload tags to the document its assets', false) diff --git a/packages/cli/lib/lib/entry.js b/packages/cli/lib/lib/entry.js index 6cfde8078..f017a94b1 100644 --- a/packages/cli/lib/lib/entry.js +++ b/packages/cli/lib/lib/entry.js @@ -1,5 +1,3 @@ -/* global __webpack_public_path__ */ - import * as Preact from 'preact'; const { h, render, hydrate } = Preact; @@ -14,17 +12,17 @@ if (process.env.NODE_ENV === 'development') { // only add a debug sw if webpack service worker is not requested. if (process.env.ADD_SW === undefined && 'serviceWorker' in navigator) { // eslint-disable-next-line no-undef - navigator.serviceWorker.register(__webpack_public_path__ + 'sw-debug.js'); + navigator.serviceWorker.register(process.env.SW_PATH + 'sw-debug.js'); } else if (process.env.ADD_SW && 'serviceWorker' in navigator) { // eslint-disable-next-line no-undef navigator.serviceWorker.register( - __webpack_public_path__ + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') + process.env.SW_PATH + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') ); } } else if (process.env.ADD_SW && 'serviceWorker' in navigator) { // eslint-disable-next-line no-undef navigator.serviceWorker.register( - __webpack_public_path__ + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') + process.env.SW_PATH + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') ); } diff --git a/packages/cli/lib/lib/webpack/webpack-client-config.js b/packages/cli/lib/lib/webpack/webpack-client-config.js index f138e0574..0acbe1a5e 100644 --- a/packages/cli/lib/lib/webpack/webpack-client-config.js +++ b/packages/cli/lib/lib/webpack/webpack-client-config.js @@ -204,6 +204,7 @@ function isProd(config) { 'process.env.ES_BUILD': false, 'process.env.ESM': config.esm, 'process.env.PRERENDER': config.prerender, + 'process.env.SW_PATH': JSON.stringify(config.swPath), }), ], @@ -314,6 +315,7 @@ function isDev(config) { new webpack.DefinePlugin({ 'process.env.ADD_SW': config.sw, 'process.env.PRERENDER': config.prerender, + 'process.env.SW_PATH': JSON.stringify(config.swPath), }), ], From 3571171f271412fc40c71a5655d2d5832b55f647 Mon Sep 17 00:00:00 2001 From: Kencana Kesuma Date: Thu, 16 Jul 2020 15:15:17 +0800 Subject: [PATCH 2/4] GH-591 fix(sw-src-load): add swPath option into watch command --- packages/cli/lib/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/cli/lib/index.js b/packages/cli/lib/index.js index 0fbfd4dfb..53f0bf804 100755 --- a/packages/cli/lib/index.js +++ b/packages/cli/lib/index.js @@ -86,6 +86,7 @@ prog .option('--esm', 'Builds ES-2015 bundles for your code.', false) .option('--clear', 'Clear the console', true) .option('--sw', 'Generate and attach a Service Worker', undefined) + .option('--swPath', 'Specify service worker file location, default is root', '/') .option('--babelConfig', 'Specify the babel config file', '.babelrc') .option('--rhl', '(Deprecated) use --refresh instead', false) .option('--json', 'Generate build stats for bundle analysis') From 79364d5caa47723641f81b5e74b156ef08f2020f Mon Sep 17 00:00:00 2001 From: Kencana Kesuma Date: Thu, 16 Jul 2020 15:16:34 +0800 Subject: [PATCH 3/4] GH-591 fix(sw-src-load): update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e9113fdaa..9960774e4 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,7 @@ $ preact build --dest Specify output directory (default build) --cwd A directory to use instead of $PWD (default .) --sw Generate and attach a Service Worker (default true) + --swPath Specify service worker file location (default is '/) --json Generate build stats for bundle analysis --template Path to custom HTML template --preload Adds preload tags to the document its assets (default false) @@ -143,6 +144,7 @@ $ preact watch --cwd A directory to use instead of $PWD (default .) --esm Builds ES-2015 bundles for your code. (default true) --sw Generate and attach a Service Worker (default false) + --swPath Specify service worker file location (default is '/) --json Generate build stats for bundle analysis --https Run server with HTTPS protocol --key Path to PEM key for custom SSL certificate From eb931b3b0ef351d8f9cbb298bd79dda8b2679773 Mon Sep 17 00:00:00 2001 From: Kencana Kesuma Date: Thu, 30 Jul 2020 11:29:10 +0800 Subject: [PATCH 4/4] GH-591 refactor(sw-src-load): remove swPath flag, add a check of __webpack_public_path__ if it begins with http protocol, set it blank --- README.md | 2 -- packages/cli/lib/index.js | 2 -- packages/cli/lib/lib/entry.js | 10 +++++++--- packages/cli/lib/lib/webpack/webpack-client-config.js | 2 -- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9960774e4..e9113fdaa 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,6 @@ $ preact build --dest Specify output directory (default build) --cwd A directory to use instead of $PWD (default .) --sw Generate and attach a Service Worker (default true) - --swPath Specify service worker file location (default is '/) --json Generate build stats for bundle analysis --template Path to custom HTML template --preload Adds preload tags to the document its assets (default false) @@ -144,7 +143,6 @@ $ preact watch --cwd A directory to use instead of $PWD (default .) --esm Builds ES-2015 bundles for your code. (default true) --sw Generate and attach a Service Worker (default false) - --swPath Specify service worker file location (default is '/) --json Generate build stats for bundle analysis --https Run server with HTTPS protocol --key Path to PEM key for custom SSL certificate diff --git a/packages/cli/lib/index.js b/packages/cli/lib/index.js index 53f0bf804..b8282cb9f 100755 --- a/packages/cli/lib/index.js +++ b/packages/cli/lib/index.js @@ -37,7 +37,6 @@ prog .option('--dest', 'Specify output directory', 'build') .option('--cwd', 'A directory to use instead of $PWD', '.') .option('--sw', 'Generate and attach a Service Worker', true) - .option('--swPath', 'Specify service worker file location, default is root', '/') .option('--json', 'Generate build stats for bundle analysis') .option('--template', 'Path to custom HTML template') .option('--preload', 'Adds preload tags to the document its assets', false) @@ -86,7 +85,6 @@ prog .option('--esm', 'Builds ES-2015 bundles for your code.', false) .option('--clear', 'Clear the console', true) .option('--sw', 'Generate and attach a Service Worker', undefined) - .option('--swPath', 'Specify service worker file location, default is root', '/') .option('--babelConfig', 'Specify the babel config file', '.babelrc') .option('--rhl', '(Deprecated) use --refresh instead', false) .option('--json', 'Generate build stats for bundle analysis') diff --git a/packages/cli/lib/lib/entry.js b/packages/cli/lib/lib/entry.js index f017a94b1..4bf1cc680 100644 --- a/packages/cli/lib/lib/entry.js +++ b/packages/cli/lib/lib/entry.js @@ -5,6 +5,10 @@ const interopDefault = (m) => (m && m.default ? m.default : m); const normalizeURL = (url) => (url[url.length - 1] === '/' ? url : url + '/'); +// Service workers are not allowed to be loaded from external domain +const PROTOCOL_RE = /^(http(s)?:\/\/)/; +const SW_PATH = PROTOCOL_RE.test(__webpack_public_path__) ? '' : __webpack_public_path__; + if (process.env.NODE_ENV === 'development') { // enable preact devtools require('preact/debug'); @@ -12,17 +16,17 @@ if (process.env.NODE_ENV === 'development') { // only add a debug sw if webpack service worker is not requested. if (process.env.ADD_SW === undefined && 'serviceWorker' in navigator) { // eslint-disable-next-line no-undef - navigator.serviceWorker.register(process.env.SW_PATH + 'sw-debug.js'); + navigator.serviceWorker.register(SW_PATH + 'sw-debug.js'); } else if (process.env.ADD_SW && 'serviceWorker' in navigator) { // eslint-disable-next-line no-undef navigator.serviceWorker.register( - process.env.SW_PATH + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') + SW_PATH + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') ); } } else if (process.env.ADD_SW && 'serviceWorker' in navigator) { // eslint-disable-next-line no-undef navigator.serviceWorker.register( - process.env.SW_PATH + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') + SW_PATH + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js') ); } diff --git a/packages/cli/lib/lib/webpack/webpack-client-config.js b/packages/cli/lib/lib/webpack/webpack-client-config.js index 0acbe1a5e..f138e0574 100644 --- a/packages/cli/lib/lib/webpack/webpack-client-config.js +++ b/packages/cli/lib/lib/webpack/webpack-client-config.js @@ -204,7 +204,6 @@ function isProd(config) { 'process.env.ES_BUILD': false, 'process.env.ESM': config.esm, 'process.env.PRERENDER': config.prerender, - 'process.env.SW_PATH': JSON.stringify(config.swPath), }), ], @@ -315,7 +314,6 @@ function isDev(config) { new webpack.DefinePlugin({ 'process.env.ADD_SW': config.sw, 'process.env.PRERENDER': config.prerender, - 'process.env.SW_PATH': JSON.stringify(config.swPath), }), ],