# Console: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script

**Code:** `Service worker registration failed` · **Category:** Browser · **Seen on:** Chrome, Firefox, JavaScript, Nginx

The browser could not fetch, parse or install the service worker script, or the page is not allowed to register one (HTTPS and scope rules).

## Meaning

Service workers need a secure context (HTTPS or localhost), a script reachable at its URL with a JavaScript MIME type, and a scope no wider than the script's folder unless `Service-Worker-Allowed` is sent. The console message names the exact reason: 404, MIME type, scope or SSL certificate error.

## Common causes

- Wrong URL for `sw.js` (404 or SPA fallback returning HTML)
- Not served over HTTPS (except localhost)
- Script served with a non-JavaScript MIME type
- Scope wider than the script location
- Syntax error in the worker or certificate error

## Quick fix

1. Open the script URL directly — it must return JavaScript
2. Use HTTPS (or localhost for development)
3. Place `sw.js` at the site root or send `Service-Worker-Allowed`

## Detailed fix by platform

### JavaScript

1. `navigator.serviceWorker.register('/sw.js', { scope: '/' }).catch(console.error);` — read the rejected error message
2. DevTools → Application → Service Workers shows status and errors

### Nginx

1. Serve with the right header for scope: `location = /static/sw.js { add_header Service-Worker-Allowed "/"; }`

## How to diagnose

1. **URL** — Does /sw.js load as JavaScript?
2. **Context** — HTTPS or localhost?
3. **Scope** — Within the script's folder?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/browser/service-worker-registration-failed)
