Generate a basic service worker script to enable caching and offline support. Useful starting point for PWAs.
Assets to cache (one per line)
About service workers
Service workers run in the background and enable caching, offline experiences and push notifications. This generator creates a simple install/fetch/activate service worker to get started. Adapt the script to your app needs and test thoroughly.
Common tips
- Use a versioned cache name to force updates when the app changes.
- Prefer network-first for dynamic content and cache-first for static assets.
- Provide an offline fallback page for navigation requests when offline.
- Combine with a manifest and HTTPS to enable installable PWA behavior.
Registration
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/service-worker.js').then(function(reg) {
console.log('ServiceWorker registration successful with scope: ', reg.scope);
}, function(err) {
console.log('ServiceWorker registration failed: ', err);
});
});
}
Tip: Serve the generated service-worker script from your site's root as /service-worker.js and ensure it's updated when assets change.
© 2025-2026 - DRMA Tech.