Skip to content

Commit

Permalink
refactor: simplify service worker notification
Browse files Browse the repository at this point in the history
  • Loading branch information
reuixiy committed Jan 30, 2020
1 parent af1de8b commit ff1d110
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions layouts/partials/third-party/service-worker.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@
</div>

<script>
if('serviceWorker' in navigator) {
{{ printf `navigator.serviceWorker.register('%s')` $url | safeJS }}
.then(reg => {
reg.addEventListener('updatefound', () => {
newWorker = reg.installing;
newWorker.addEventListener('statechange', () => {
if (newWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
showNotification();
}
}
});
if ('serviceWorker' in navigator) {
if (navigator.serviceWorker.controller) {
navigator.serviceWorker.addEventListener('controllerchange', function() {
showNotification();
});
}

window.addEventListener('load', function() {
{{ printf `navigator.serviceWorker.register('%s')` $url | safeJS }}
});
}

function showNotification() {
document.querySelector('meta[name=theme-color]').content = '#000';
document.getElementById('app-refresh').className += ' app-refresh-show';
Expand Down

0 comments on commit ff1d110

Please sign in to comment.