Skip to content

Service Worker Registration Failed

Quick summary

Your service worker failed to register — the promise rejected and offline features do not work.

Why this happens

  • The SW script is served over HTTP instead of HTTPS (secure context required)

  • The SW script URL returns 404 or a MIME type other than text/javascript

  • A syntax error inside the SW file prevents it from installing

Minimal example

✗ Broken code
navigator.serviceWorker.register('/sw.js')
  .catch(err => console.error('SW reg failed:', err)); // rejects on HTTP
✓ Fixed code
if ('serviceWorker' in navigator && location.protocol === 'https:') {
  navigator.serviceWorker.register('/sw.js')
    .catch(err => console.error('SW reg failed:', err));
}

Registering a SW on HTTP fails. Guarding with a protocol check and only registering on HTTPS avoids the rejection.

How to diagnose

  • Check the console for the registration rejection message

  • Verify the SW script URL returns 200 with a JavaScript MIME type

  • Confirm the page is served over HTTPS or localhost

How to fix

  • Serve the site over HTTPS (or use localhost for development)

  • Correct the SW script path and ensure it is in the same origin

  • Fix any syntax errors inside the service worker file

How to prevent

  • Always develop SW on HTTPS or localhost

  • Log registration errors in production to catch deployment issues

Related Resources

Related Glossary

Related Lessons

  • HTTP

    Learn about service workers.

← Back to language