FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope
An error occurred while retrieving token. FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope ('http://localhost:3000/firebase-cloud-messaging-push-scope')) with script ('http://localhost:3000/firebase-messaging-sw.js')::) The script has an unsupported MIME type ('text/html'). (messaging/failed-service-worker-registration). at registerDefaultSw (registerDefaultSw.ts:43:1) at async updateSwReg (updateSwReg.ts:28:1) at async getToken$1 (getToken.ts:43:1)
Added the firebase-messaging-sw.js in public folder. Every time this error comes. Please help
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
Hi @SindhujaKanukolanu, thanks for reporting. Are you adding a service worker for your react app? Also, we've stopped supporting getToken for sw because it has some compatibility issues with older browsers (v9 API doc). If I can replicate the issue, I can have a better look into it. Please share a minimal, but complete sample of a project that I can run locally.
Yes. We are using for react app with webpack. I have added service worker and config. Will try to share project if possible
@SindhujaKanukolanu I am facing the same issue now. Did you find a solution please?
This error could have been thrown for multiple reasons. The most common being an runtime error in the sw script. Would suggest lookin into and fix any sus error log from console.
@zwu52 thanks, I finally took the working FlutterFire example project, and kept adding my code to it till it broke and debug it that way . Details here for anyone facing this.
May be related. I have a Firebase plain javascript web app, and I was getting Uncaught SyntaxError: Cannot use import statement outside a module (at firebase-messaging-sw.js:1:1). I am using webpack and the CopyPlugin to copy my firebase-messaging-sw.js file directly to the /dist directory. Changed the "to:" value to a .mjs extension:
new CopyPlugin({patterns: [ { from: './firebase-messaging-sw.js', to: './firebase-messaging-sw.mjs', }]
And the error goes away. Hopefully this helps somebody out there. Probably a webpack thing in my case.
Hi @SindhujaKanukolanu, thanks for reporting. Are you adding a service worker for your react app? Also, we've stopped supporting getToken for sw because it has some compatibility issues with older browsers (v9 API doc). If I can replicate the issue, I can have a better look into it. Please share a minimal, but complete sample of a project that I can run locally.
Hi am I being dumb is this not what is recommended in the docs here:
https://firebase.google.com/docs/cloud-messaging/js/client?authuser=0
?
What are you saying is recommended? Can you elaborate? It doesn't recommend using getToken() in the service worker on that page. It recommends using a service worker file (empty if needed), and getToken() somewhere else (not the service worker) if I understand correctly. We could probably sort this out quickly if you provide a minimal repro.
Hey @SindhujaKanukolanu. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
Closing the issue.