actually decent service worker wip

This commit is contained in:
vabene1111
2021-01-17 18:33:12 +01:00
parent d7894e07e9
commit 2e3e629406
9 changed files with 120 additions and 21 deletions

View File

@ -0,0 +1,32 @@
/* 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 () {
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)
}
})
}

39
vue/src/sw.js Normal file
View File

@ -0,0 +1,39 @@
// These JavaScript module imports need to be bundled:
import {precacheAndRoute} from 'workbox-precaching';
import {registerRoute} from 'workbox-routing';
import {CacheFirst, NetworkFirst, StaleWhileRevalidate} from 'workbox-strategies';
precacheAndRoute(self.__WB_MANIFEST);
registerRoute(
({request}) => request.destination === 'image',
new CacheFirst({cacheName: 'images'}),
);
registerRoute(
({request}) => request.destination === 'script' || request.destination === 'style',
new StaleWhileRevalidate({
cacheName: 'assets'
})
)
registerRoute(
new RegExp('jsi18n/'),
new StaleWhileRevalidate({
cacheName: 'assets'
})
)
registerRoute(
new RegExp('api/*'),
new NetworkFirst({
cacheName: 'api'
})
)
registerRoute(
({request}) => request.destination === 'document',
new NetworkFirst({
cacheName: 'html'
})
)