firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope

Open SindhujaKanukolanu opened this issue 3 years ago • 3 comments

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

SindhujaKanukolanu avatar Sep 06 '22 12:09 SindhujaKanukolanu

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Sep 06 '22 12:09 google-oss-bot

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.

jbalidiong avatar Sep 07 '22 08:09 jbalidiong

Yes. We are using for react app with webpack. I have added service worker and config. Will try to share project if possible

SindhujaKanukolanu avatar Sep 07 '22 09:09 SindhujaKanukolanu

@SindhujaKanukolanu I am facing the same issue now. Did you find a solution please?

beriaanirudh avatar Oct 20 '22 13:10 beriaanirudh

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 avatar Oct 24 '22 19:10 zwu52

@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.

beriaanirudh avatar Oct 27 '22 11:10 beriaanirudh

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.

glennsimon avatar Nov 09 '22 19:11 glennsimon

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

?

obbila avatar Mar 21 '23 01:03 obbila

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.

hsubox76 avatar Jun 12 '23 18:06 hsubox76

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!

google-oss-bot avatar Jun 19 '23 01:06 google-oss-bot

Closing the issue.

SindhujaKanukolanu avatar Jun 19 '23 03:06 SindhujaKanukolanu