Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Firebase Messaging cannot register its default service worker

ErrorWe are unable to register the default service worker.

Short answer

Firebase Messaging registers a file called firebase-messaging-sw.js from the root of your domain. The error means the browser could not load or start that file. Check that the file exists at the root URL, that the site uses HTTPS, and tell the SDK about your own worker if you use another file name.

  • Applies to Firebase JS SDK 13, Chrome, Flutter web, React
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the worker URL directly. The browser error text that follows the sentence in the console says why it failed (a 404 is the most common).

    curl -I https://example.com/firebase-messaging-sw.js
  2. 2Match what you see, then jump to its fix

    1. 1The curl above returns 404, or an HTML page instead of JavaScriptfirebase-messaging-sw.js is not at the root of the domainFix
    2. 2The worker file loads fine, but registration fails on an http:// addressThe page is not served over HTTPSFix
    3. 3The site already registers a different worker (a PWA worker, or a differently named file) and the Firebase default is missingYou use your own worker file, but the SDK still looks for the default oneFix

What does the error look like?

  • We are unable to register the default service worker.
  • messaging/failed-service-worker-registration

Cause 1 of 3

firebase-messaging-sw.js is not at the root of the domain

1Check

The SDK looks for a file with this exact name. Firebase's web client guide says to create it in the root of your domain before registering. Your build must therefore publish it at that root path.

# run in the folder your build publishes at the root
ls firebase-messaging-sw.js

2Fix

Create the file in the folder your build publishes at the root. An empty file registers; add the messaging code after it works.

// firebase-messaging-sw.js
importScripts('https://www.gstatic.com/firebasejs/13.0.0/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/13.0.0/firebase-messaging-compat.js');

firebase.initializeApp({ /* your Firebase config object */ });
const messaging = firebase.messaging();

3Verify

The URL now returns the file with a 200 status.

curl -I https://example.com/firebase-messaging-sw.js

Confirmed by firebase.google.com

Did this fix it?

Cause 2 of 3

The page is not served over HTTPS

1Check

Firebase documents that service workers are only available on HTTPS sites, so FCM works only on pages served over HTTPS.

curl -I http://example.com/ | head -n 3

2Fix

Serve the site over HTTPS, including in staging, so the SDK can register its worker.

# put a TLS certificate on the domain and use the https:// address

3Verify

Reload and request a token again; the error no longer appears.

# browser console: no failed-service-worker-registration

Confirmed by firebase.google.com

Did this fix it?

Cause 3 of 3

You use your own worker file, but the SDK still looks for the default one

1Check

getToken accepts a serviceWorkerRegistration option, so you can hand the SDK a worker you registered yourself instead of letting it register the default.

grep -rn "serviceWorker.register" src/

2Fix

Register your worker, then pass the registration to getToken. In Flutter's FCM plugin for web, register() takes a serviceWorkerScriptPath for the same purpose.

const registration = await navigator.serviceWorker.register('/my-worker.js');
const token = await getToken(messaging, {
  vapidKey: 'YOUR_PUBLIC_VAPID_KEY',
  serviceWorkerRegistration: registration,
});

3Verify

getToken resolves with a token.

console.log(token);

Confirmed by firebase.google.com

Did this fix it?

How do I stop it coming back?

  • Keep firebase-messaging-sw.js in the folder that your build publishes at the domain root.
  • Check the worker URL with curl after every deploy.
  • Use HTTPS in every environment that tests notifications.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.