Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an option to configure service worker setup messages #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -53,6 +53,7 @@ pwa:
strategy: cacheFirst
- pattern: !!js/regexp /\//
strategy: networkFirst
console: error
priority: 5
```

Expand All @@ -68,6 +69,7 @@ pwa:
- routes - request routes and strategies, based on [sw-toolbox](https://googlechromelabs.github.io/sw-toolbox/#main). **The routes order does matter**.
- pattern: url pattern, this config can be express-style or RegExp
- strategy: the strategy you want to choose. [All strategies](https://googlechromelabs.github.io/sw-toolbox/api.html#options): `cacheFirst`, `networkFirst`, `cacheOnly`, `networkOnly`, `fastest`. Caution: Log requests should use `networkOnly` strategy.
- console: console log level, set to `none` to hide all the setup messages or `error` to hide normal logs. The default option is `all`(show all logs)
- priority - [plugin priority](https://hexo.io/api/filter.html) (default value is 10)

## License
Expand Down
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -25,7 +25,10 @@ if (hexo.config.pwa.manifest) {
if (hexo.config.pwa.serviceWorker) {
// get sw register code and compile
let swTpl = fs.readFileSync(path.resolve(__dirname, './templates/swRegister.tpl.js'));
compiledSWRegTpl = tpl(swTpl)({path: hexo.config.pwa.serviceWorker.path + '?t=' + Date.now()});
compiledSWRegTpl = tpl(swTpl)({
path: hexo.config.pwa.serviceWorker.path + '?t=' + Date.now(),
console: hexo.config.pwa.serviceWorker.console,
});
// generate service worker file
hexo.extend.generator.register('serviceWorker', serviceWorkerGenerator);
}
Expand Down
12 changes: 10 additions & 2 deletions templates/swRegister.tpl.js
@@ -1,5 +1,13 @@
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('<%=path %>')
.then(function () {console.log('ServiceWorker Register Successfully.')})
.catch(function (e) {console.error(e)});
.then(function() {
if ('<%=console %>' !== 'none') {
console.log('ServiceWorker Register Successfully.');
}
})
.catch(function(e) {
if (['error', 'none'].indexOf('<%=console %>') === -1) {
console.error(e);
}
});
}