JavaScript Service Worker Registration Scope Fix
In this tutorial, you'll learn about JavaScript Service Worker Registration Scope Fix. We cover key concepts, practical examples, and best practices.
The Problem
Service worker registration scope determines which pages the service worker controls. Registering with a default scope from a nested path limits control to that subdirectory. Pages outside the scope are not intercepted, leading to inconsistent offline behavior.
Quick Fix
Step 1: Register with explicit scope
// Wrong — scope defaults to the service worker file's location
// If sw.js is in /js/sw.js, scope is /js/
navigator.serviceWorker.register('/js/sw.js');
// Right — specify scope to control the entire site
navigator.serviceWorker.register('/sw.js', {
scope: '/'
});
// Or place sw.js at the root — scope defaults to /
navigator.serviceWorker.register('/sw.js');
Step 2: Understand scope rules
// Wrong — scope cannot be wider than the service worker's directory
navigator.serviceWorker.register('/blog/sw.js', {
scope: '/' // This fails!
});
// Error: The path of the provided scope '/'
// is not under the max scope allowed '/blog/'
// Right — scope must be at or below SW location
navigator.serviceWorker.register('/blog/sw.js', {
scope: '/blog/'
});
Step 3: Verify registration and scope
// Wrong — assumes registration succeeded
navigator.serviceWorker.register('/sw.js');
// Right — check registration result and scope
navigator.serviceWorker.register('/sw.js', { scope: '/' })
.then((registration) => {
console.log('SW registered. Scope:', registration.scope);
console.log('Active:', registration.active ? 'yes' : 'no');
})
.catch((error) => {
console.error('Registration failed:', error.message);
});
Step 4: Handle multiple service workers
// Wrong — registering without unregistering previous workers
navigator.serviceWorker.register('/sw-v2.js');
// Right — check which SW is active
navigator.serviceWorker.getRegistration('/').then((reg) => {
if (reg && reg.active) {
console.log('Active SW:', reg.active.scriptURL);
}
// Register new one — old will be replaced on next page load
navigator.serviceWorker.register('/sw-v2.js', { scope: '/' });
});
Step 5: Use updateViaCache correctly
// Wrong — default cache behavior may serve stale SW
navigator.serviceWorker.register('/sw.js');
// Right — control cache behavior
navigator.serviceWorker.register('/sw.js', {
scope: '/',
updateViaCache: 'none' // Always check for updates
});
Prevention
- Place the service worker file at the root (
/sw.js) for site-wide control - Always specify an explicit
scopeoption during registration - Verify scope is at or below the service worker's directory path
- Use
updateViaCache: 'none'to ensure quick SW updates - Monitor registration with
navigator.serviceWorker.getRegistration()
Common Mistakes with service worker register
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world JS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro