Firebase Messaging cannot register its default service worker
We 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.
Fix it fast
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.js2Match what you see, then jump to its fix
- 1
The curl above returns 404, or an HTML page instead of JavaScriptfirebase-messaging-sw.js is not at the root of the domainFix - 2
The worker file loads fine, but registration fails on an http:// addressThe page is not served over HTTPSFix - 3
The 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
- 1
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.js2Fix
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.jsConfirmed by firebase.google.com
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 32Fix
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:// address3Verify
Reload and request a token again; the error no longer appears.
# browser console: no failed-service-worker-registrationConfirmed by firebase.google.com
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
Attempted import error: firebase/app has no default export
Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase').
Since the modular SDK (version 9), firebase/app no longer exports one firebase object.
3 causes, each with a fixWeb apps
Firestore addDoc throws on a field whose value is undefined
FirebaseError: Function addDoc() called with invalid data. Unsupported field value
Firestore has no undefined type.
2 causes, each with a fixWeb apps
A Vite single page app shows 404 when you refresh a route on the live site
Page not found (Netlify) on a route that works inside the app
Client side routes like /dashboard are not real files in dist.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fix