Skip to content

Commit

Permalink
preactjsGH-591: fix service worker being loaded from cdn issue
Browse files Browse the repository at this point in the history
- introduced new cli args to define swPath, default would be '/'.
- decoupled from using webpack public path, better to be implicit
  • Loading branch information
Kencana Kesuma committed Jul 16, 2020
1 parent 75f5fb6 commit c643897
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/cli/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ prog
.option('--src', 'Specify source directory', 'src')
.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('--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)
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/lib/lib/entry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global __webpack_public_path__ */
/* global (process.env.SW_PATH || __webpack_public_path__) */

import * as Preact from 'preact';
const { h, render, hydrate } = Preact;
Expand All @@ -14,17 +14,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 || __webpack_public_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 || __webpack_public_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 || __webpack_public_path__) + (process.env.ES_BUILD ? 'sw-esm.js' : 'sw.js')
);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/lib/lib/webpack/webpack-client-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ function isProd(config) {
'process.env.ADD_SW': config.sw,
'process.env.ES_BUILD': false,
'process.env.ESM': config.esm,
'process.env.PRERENDER': config.prerender,
'process.env.PRERENDER': config.prerender,
'process.env.SW_PATH': JSON.stringify(config.swPath)
}),
],

Expand Down

0 comments on commit c643897

Please sign in to comment.