Skip to content

Instantly share code, notes, and snippets.

@matthewmorek
Last active August 6, 2019 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewmorek/60865b763feb841f787e670f5cccee23 to your computer and use it in GitHub Desktop.
Save matthewmorek/60865b763feb841f787e670f5cccee23 to your computer and use it in GitHub Desktop.
PWA Service Worker
<template>
<div id="app">
<h1>My app</h1>
</div>
</template>
<script>
export default {
name: 'My app',
mounted() {
document.addEventListener('updateReady', this.showRefreshUI);
},
beforeDestroy() {
document.removeEventListener('updateReady', this.showRefreshUI);
},
methods: {
showRefreshUI: function() {
alert('Pew version available, please reload!');
}
}
};
</script>
/* eslint-disable no-console */
import { register } from 'register-service-worker';
if (process.env.NODE_ENV === 'production') {
register(`${process.env.BASE_URL}service-worker.js`, {
ready() {
console.log(
'App is being served from cache by a service worker.\n' +
'For more details, visit https://goo.gl/AFskqB'
);
},
registered() {
console.log('Service worker has been registered.');
},
cached() {
console.log('Content has been cached for offline use.');
},
updatefound() {
console.log('New content is downloading.');
},
updated(registration) {
new CustomEvent('swUpdated', { detail: registration });
console.log('New content is available; please refresh.');
},
offline() {
console.log(
'No internet connection found. App is running in offline mode.'
);
},
error(error) {
console.error('Error during service worker registration:', error);
}
});
}
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.core.setCacheNameDetails({
prefix: 'tramspotter',
suffix: 'v0.2.4'
});
workbox.precaching.suppressWarnings();
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment