Get started with workbox-sw CDN
MIT licensed
Library for caching, network handling in JS apps: Service Worker-Workbox-SW.
Tags:- workbox
- workboxjs
- service worker
- sw
Stable version
Copied!
How to start using workbox-sw CDN
// Register the service worker as soon as the page is loaded
self.registration = await navigator.serviceWorker.register('service-worker.js', { scope: '/' });
// Import Workbox SW and its modules
importScripts('https://cdn.cdnhub.io/workbox-sw/7.0.0/workbox-sw.js');
workbox.precaching.precacheAndRoute([
// List of files to precache
'/index.html',
'/styles.css',
'/main.js',
// Add your assets here
]);
// Cache and route network requests
workbox.routing.registerRoute(
/\.(?:json|xml)$/,
new workbox.strategies.CacheFirst({
cacheName: 'json-cache',
plugins: [
new workbox.expiration.Plugin({ maxEntries: 60, maxAgeSeconds: 30 * 24 * 60 * 60 }), // Cache for 30 days
],
})
);
workbox.routing.registerRoute(
/\.(?:png|jpg|jpeg|gif|svg)$/,
new workbox.strategies.CacheFirst({
cacheName: 'image-cache',
plugins: [
new workbox.expiration.Plugin({ maxEntries: 300, maxAgeSeconds: 30 * 24 * 60 * 60 }), // Cache for 30 days
],
})
);
// Cache and route Google Fonts
workbox.routing.registerRoute(
/^https:\/\/fonts\.googleapis\.com/,
new workbox.strategies.CacheFirst({
cacheName: 'google-fonts-cache',
plugins: [
new workbox.cacheableResponse.Plugin({ statuses: [0, 200] }),
new workbox.expiration.Plugin({ maxEntries: 25, maxAgeSeconds: 60 * 60 * 24 * 30 }), // Cache for 30 days
],
})
);
// Cache and route Google Analytics
workbox.routing.registerRoute(
/^https:\/\/www.goog-analytics.com/,
new workbox.strategies.NetworkFirst({
cacheName: 'google-analytics-cache',
plugins: [
new workbox.cacheableResponse.Plugin({ statuses: [0, 200] }),
],
})
);
Copied!
Copied!
Copied!
All versions
5.0.0
5.0.0-beta.0
5.0.0-beta.1
5.0.0-rc.0
5.0.0-rc.1
5.0.0-rc.2
5.1.0
5.1.1
5.1.2
5.1.3
5.1.4
6.0.0
6.0.0-alpha.0
6.0.0-alpha.1
6.0.0-alpha.2
6.0.0-alpha.3
6.0.0-rc.0
6.0.2
6.1.0
6.1.1
6.1.2
6.1.4
6.1.5
6.2.0
6.2.0-alpha.0
6.2.0-alpha.1
6.2.0-alpha.2
6.2.1
6.2.2
6.2.3
6.2.4
6.3.0
6.4.0
6.4.1
6.4.2
6.5.0
6.5.1
6.5.2
6.5.3
6.5.4
6.6.0
6.6.1
7.0.0
*** 7.1.0