workbox expirments
This commit is contained in:
32
vue/src/registerServiceWorker.js
Normal file
32
vue/src/registerServiceWorker.js
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
70
vue/src/serviceWorker.js
Normal file
70
vue/src/serviceWorker.js
Normal file
@ -0,0 +1,70 @@
|
||||
import { registerRoute } from 'workbox-routing';
|
||||
import {
|
||||
NetworkFirst,
|
||||
StaleWhileRevalidate,
|
||||
CacheFirst,
|
||||
} from 'workbox-strategies';
|
||||
|
||||
// Used for filtering matches based on status code, header, or both
|
||||
import { CacheableResponsePlugin } from 'workbox-cacheable-response';
|
||||
// Used to limit entries in cache, remove entries after a certain period of time
|
||||
import { ExpirationPlugin } from 'workbox-expiration';
|
||||
|
||||
// Cache page navigations (html) with a Network First strategy
|
||||
registerRoute(
|
||||
// Check to see if the request is a navigation to a new page
|
||||
({ request }) => request.mode === 'navigate',
|
||||
// Use a Network First caching strategy
|
||||
new NetworkFirst({
|
||||
// Put all cached files in a cache named 'pages'
|
||||
cacheName: 'pages',
|
||||
plugins: [
|
||||
// Ensure that only requests that result in a 200 status are cached
|
||||
new CacheableResponsePlugin({
|
||||
statuses: [200],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
// Cache CSS, JS, and Web Worker requests with a Stale While Revalidate strategy
|
||||
registerRoute(
|
||||
// Check to see if the request's destination is style for stylesheets, script for JavaScript, or worker for web worker
|
||||
({ request }) =>
|
||||
request.destination === 'style' ||
|
||||
request.destination === 'script' ||
|
||||
request.destination === 'worker',
|
||||
// Use a Stale While Revalidate caching strategy
|
||||
new StaleWhileRevalidate({
|
||||
// Put all cached files in a cache named 'assets'
|
||||
cacheName: 'assets',
|
||||
plugins: [
|
||||
// Ensure that only requests that result in a 200 status are cached
|
||||
new CacheableResponsePlugin({
|
||||
statuses: [200],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
// Cache images with a Cache First strategy
|
||||
registerRoute(
|
||||
// Check to see if the request's destination is style for an image
|
||||
({ request }) => request.destination === 'image',
|
||||
// Use a Cache First caching strategy
|
||||
new CacheFirst({
|
||||
// Put all cached files in a cache named 'images'
|
||||
cacheName: 'images',
|
||||
plugins: [
|
||||
// Ensure that only requests that result in a 200 status are cached
|
||||
new CacheableResponsePlugin({
|
||||
statuses: [200],
|
||||
}),
|
||||
// Don't cache more than 50 items, and expire them after 30 days
|
||||
new ExpirationPlugin({
|
||||
maxEntries: 50,
|
||||
maxAgeSeconds: 60 * 60 * 24 * 30, // 30 Days
|
||||
}),
|
||||
],
|
||||
}),
|
||||
);
|
Reference in New Issue
Block a user