Fixed service worker

This commit is contained in:
Dan Elbert 2019-09-13 10:50:28 -05:00
parent ad9850a8e2
commit 474e417dca
5 changed files with 24 additions and 10 deletions

View File

@ -203,7 +203,7 @@ GEM
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
tzinfo-data (1.2019.2)
tzinfo-data (1.2019.3)
tzinfo (>= 1.0.0)
unitwise (2.2.0)
liner (~> 0.2)

2
Procfile Normal file
View File

@ -0,0 +1,2 @@
rails: bundle exec rails s -b 0.0.0.0
webpacker: bin/webpack-dev-server

View File

@ -1,25 +1,36 @@
function trackInstall(worker, cb) {
worker.addEventListener('statechange', function() {
//If the worker is now installed, let the user know that there is an update ready
if (worker.state == 'installed') {
cb();
}
});
}
function trackActive(worker, cb) {
worker.addEventListener('statechange', function() {
if (worker.state == 'activated') {
cb();
}
});
}
export function swUpdate() {
navigator.serviceWorker.getRegistration().then(reg => {
if (reg && reg.waiting) {
trackActive(reg.waiting, () => window.location.reload(true));
reg.waiting.postMessage("skipWaiting");
} else {
window.location.reload(true);
}
});
}
export function swInit(store) {
const updateReady = () => store.commit("setUpdateAvailable", true);
const clearUpdateReady = () => store.commit("setUpdateAvailable", false);
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js')
@ -27,17 +38,15 @@ export function swInit(store) {
console.log('Registration succeeded. Scope is ' + reg.scope);
if (reg.waiting) {
console.log('waiting');
updateReady();
}
// If there's an updated worker installing, track its progress. If it becomes "installed", call
// indexController._updateReady()
if (reg.installing) {
trackInstall(reg.installing, updateReady);
trackActive(reg.waiting, clearUpdateReady);
}
reg.addEventListener('updatefound', function () {
if (reg.active) {
trackInstall(reg.installing, updateReady);
}
});
}).catch(function (error) {

View File

@ -63,7 +63,6 @@ Vue.use(VueProgressBar, {
Vue.use(VueResize);
sync(store, router);
swInit(store);
responsiveSync(store);
document.addEventListener('DOMContentLoaded', () => {
@ -76,6 +75,9 @@ document.addEventListener('DOMContentLoaded', () => {
store,
router,
render: createElement => createElement('App'),
mounted() {
this.$nextTick(() => swInit(store));
},
components: { App }
});
});

View File

@ -24,6 +24,7 @@ self.addEventListener('install', function(event) {
});
self.addEventListener('activate', function(event) {
self.clients.claim();
event.waitUntil(
caches.keys().then(function (keyList) {
return Promise.all(keyList.map(function (key) {