Skip to content

JavaScript Service Worker Registration Scope Fix

DodaTech Updated 2026-06-24 3 min read

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 scope option 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

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' 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

### Why does my service worker only control one page?

Check the scope. If your SW file is in /blog/sw.js with no explicit scope, it only controls /blog/ and its subpaths. Register with { scope: '/' } and place the SW at the root level.

Can I register multiple service workers?

Yes, but each scope can only have one active service worker. Multiple registrations with different scopes are allowed. The browser picks the most specific scope match for each page.

What does the Service-Worker-Allowed header do?

It allows a service worker to expand its maximum scope beyond its directory. For example, /blog/sw.js can control / if the server sends Service-Worker-Allowed: /. This is a server-side security restriction.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro