Skip to content

Browser Service Worker Registration Error Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Browser Service Worker Registration Error Fix. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Service workers enable offline functionality, push notifications, and background sync. Registration fails when the script is not served over HTTPS, the scope is invalid, or the service worker script contains syntax errors.

The Wrong Way

// service-worker.js
// Missing self.addEventListener('install') and self.addEventListener('fetch')
console.log("Service worker loaded");
// main.js
if ("serviceWorker" in navigator) {
    navigator.serviceWorker.register("/service-worker.js");
}

Output in console:

Failed to register a ServiceWorker for scope 'http://localhost:8080/'
with script 'http://localhost:8080/service-worker.js':
The script has an unsupported MIME type 'text/html'.

The Right Way

Register with proper scope and handle errors:

// service-worker.js
self.addEventListener("install", event => {
    console.log("Service worker installing");
    self.skipWaiting();
});

self.addEventListener("activate", event => {
    console.log("Service worker activating");
    event.waitUntil(clients.claim());
});

self.addEventListener("fetch", event => {
    event.respondWith(fetch(event.request));
});
// main.js
if ("serviceWorker" in navigator) {
    navigator.serviceWorker.register("/service-worker.js", {
        scope: "/"
    }).then(registration => {
        console.log("Service worker registered:", registration.scope);

        // Check for updates
        registration.addEventListener("updatefound", () => {
            console.log("New service worker found");
        });
    }).catch(error => {
        console.error("Service worker registration failed:", error);
    });
}

Step-by-Step Fix

1. Serve service worker over HTTPS

# Redirect HTTP to HTTPS
server {
    listen 80;
    return 301 https://$host$request_uri;
}

2. Use localhost for development

// localhost is exempt from HTTPS requirement
// Configure your dev server
{
    "scripts": {
        "dev": "serve -l 3000 --ssl-cert cert.pem --ssl-key key.pem"
    }
}

3. Set correct scope

// Service worker can only control pages within its scope
// scope: "/app/" means only /app/* pages are controlled

navigator.serviceWorker.register("/sw.js", {
    scope: "/"
});

4. Serve the correct MIME type

# Nginx should serve .js files with correct MIME type
location /service-worker.js {
    add_header Service-Worker-Allowed "/";
    add_header Content-Type "application/javascript";
}

5. Handle updates properly

navigator.serviceWorker.register("/sw.js").then(reg => {
    reg.addEventListener("updatefound", () => {
        const newWorker = reg.installing;
        newWorker.addEventListener("statechange", () => {
            if (newWorker.state === "installed" && navigator.serviceWorker.controller) {
                // New version available
                const reload = confirm("New version available. Reload?");
                if (reload) {
                    newWorker.postMessage({action: "skipWaiting"});
                }
            }
        });
    });
});

Prevention Tips

  • Always serve service workers over HTTPS (localhost works for development).
  • Place the service worker in the root directory for maximum scope.
  • Handle registration errors and show user-friendly fallbacks.
  • Use self.skipWaiting() and clients.claim() for immediate activation.
  • Test service worker updates with the Application tab in DevTools.

Common Mistakes with service worker

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world BROWSER 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 must service workers use HTTPS?

Service workers have powerful capabilities (intercepting network requests, Caching). HTTPS ensures the service worker script has not been tampered with, preventing man-in-the-middle attacks.

What scope can a service worker control?

A service worker can only control pages within its scope path. A worker at /app/sw.js can only control pages under /app/. Place it in the root for full site control.

How do I unregister a service worker?

In Chrome DevTools: Application > Service Workers > Unregister. Programmatically: navigator.serviceWorker.getRegistrations().then(regs => regs.forEach(r => r.unregister())).

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro