Skip to content

Instantly share code, notes, and snippets.

@haigopi
Created November 27, 2020 04:48
Show Gist options
  • Save haigopi/281d4390baed58f31f81ab37f874ba72 to your computer and use it in GitHub Desktop.
Save haigopi/281d4390baed58f31f81ab37f874ba72 to your computer and use it in GitHub Desktop.
workbox.setConfig({ debug: `${process.env.VUE_APP_SW_DEBUG}` });
workbox.core.setCacheNameDetails({
// set the default cache name prefix. each domain should be unique to stop clashes
// this is used for runtime and precaching only
prefix: 'test-me',
});
workbox.routing.registerRoute(
new RegExp('https://assets.abcd.com/(.*)'),
new workbox.strategies.CacheFirst({
cacheName: 'assets',
plugins: [
new workbox.rangeRequests.Plugin(),
new workbox.cacheableResponse.Plugin({
statuses: [0, 200, 206], // 206 for Partial content.
})
],
})
);
workbox.routing.registerRoute(
new RegExp('https://auth.abcd.com/(.*)'),
new workbox.strategies.CacheFirst({
cacheName: 'auth',
plugins: [
new workbox.rangeRequests.Plugin(),
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
})
],
})
);
// Requires to precache first.
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open("assets").then(function(cache) {
return cache.addAll([
'https://assets.abcd.com/random_1.mp4',
'https://assets.abcd.com/random_2.mp4'
]);
})
);
});
workbox.precaching.precacheAndRoute([]);
// skip the 'worker in waiting' phase and activate immediately
workbox.core.skipWaiting();
// claim any clients that match the worker scope immediately. requests on these pages will
// now go via the service worker.
workbox.core.clientsClaim();
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment