Fixed service worker
This commit is contained in:
parent
ad9850a8e2
commit
474e417dca
@ -203,7 +203,7 @@ GEM
|
|||||||
thread_safe (0.3.6)
|
thread_safe (0.3.6)
|
||||||
tzinfo (1.2.5)
|
tzinfo (1.2.5)
|
||||||
thread_safe (~> 0.1)
|
thread_safe (~> 0.1)
|
||||||
tzinfo-data (1.2019.2)
|
tzinfo-data (1.2019.3)
|
||||||
tzinfo (>= 1.0.0)
|
tzinfo (>= 1.0.0)
|
||||||
unitwise (2.2.0)
|
unitwise (2.2.0)
|
||||||
liner (~> 0.2)
|
liner (~> 0.2)
|
||||||
|
2
Procfile
Normal file
2
Procfile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
rails: bundle exec rails s -b 0.0.0.0
|
||||||
|
webpacker: bin/webpack-dev-server
|
@ -1,25 +1,36 @@
|
|||||||
|
|
||||||
function trackInstall(worker, cb) {
|
function trackInstall(worker, cb) {
|
||||||
worker.addEventListener('statechange', function() {
|
worker.addEventListener('statechange', function() {
|
||||||
//If the worker is now installed, let the user know that there is an update ready
|
|
||||||
if (worker.state == 'installed') {
|
if (worker.state == 'installed') {
|
||||||
cb();
|
cb();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function trackActive(worker, cb) {
|
||||||
|
worker.addEventListener('statechange', function() {
|
||||||
|
if (worker.state == 'activated') {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function swUpdate() {
|
export function swUpdate() {
|
||||||
navigator.serviceWorker.getRegistration().then(reg => {
|
navigator.serviceWorker.getRegistration().then(reg => {
|
||||||
if (reg && reg.waiting) {
|
if (reg && reg.waiting) {
|
||||||
|
trackActive(reg.waiting, () => window.location.reload(true));
|
||||||
reg.waiting.postMessage("skipWaiting");
|
reg.waiting.postMessage("skipWaiting");
|
||||||
|
} else {
|
||||||
window.location.reload(true);
|
window.location.reload(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function swInit(store) {
|
export function swInit(store) {
|
||||||
|
|
||||||
const updateReady = () => store.commit("setUpdateAvailable", true);
|
const updateReady = () => store.commit("setUpdateAvailable", true);
|
||||||
|
const clearUpdateReady = () => store.commit("setUpdateAvailable", false);
|
||||||
|
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
navigator.serviceWorker.register('/sw.js')
|
navigator.serviceWorker.register('/sw.js')
|
||||||
@ -27,17 +38,15 @@ export function swInit(store) {
|
|||||||
console.log('Registration succeeded. Scope is ' + reg.scope);
|
console.log('Registration succeeded. Scope is ' + reg.scope);
|
||||||
|
|
||||||
if (reg.waiting) {
|
if (reg.waiting) {
|
||||||
|
console.log('waiting');
|
||||||
updateReady();
|
updateReady();
|
||||||
}
|
trackActive(reg.waiting, clearUpdateReady);
|
||||||
|
|
||||||
// If there's an updated worker installing, track its progress. If it becomes "installed", call
|
|
||||||
// indexController._updateReady()
|
|
||||||
if (reg.installing) {
|
|
||||||
trackInstall(reg.installing, updateReady);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reg.addEventListener('updatefound', function () {
|
reg.addEventListener('updatefound', function () {
|
||||||
trackInstall(reg.installing, updateReady);
|
if (reg.active) {
|
||||||
|
trackInstall(reg.installing, updateReady);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}).catch(function (error) {
|
}).catch(function (error) {
|
||||||
|
@ -63,7 +63,6 @@ Vue.use(VueProgressBar, {
|
|||||||
Vue.use(VueResize);
|
Vue.use(VueResize);
|
||||||
|
|
||||||
sync(store, router);
|
sync(store, router);
|
||||||
swInit(store);
|
|
||||||
responsiveSync(store);
|
responsiveSync(store);
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
@ -76,6 +75,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
store,
|
store,
|
||||||
router,
|
router,
|
||||||
render: createElement => createElement('App'),
|
render: createElement => createElement('App'),
|
||||||
|
mounted() {
|
||||||
|
this.$nextTick(() => swInit(store));
|
||||||
|
},
|
||||||
components: { App }
|
components: { App }
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -24,6 +24,7 @@ self.addEventListener('install', function(event) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('activate', function(event) {
|
self.addEventListener('activate', function(event) {
|
||||||
|
self.clients.claim();
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.keys().then(function (keyList) {
|
caches.keys().then(function (keyList) {
|
||||||
return Promise.all(keyList.map(function (key) {
|
return Promise.all(keyList.map(function (key) {
|
||||||
|
Loading…
Reference in New Issue
Block a user