Create React App (version before 4.0.0) by default includes a hidden Service Worker that will do some background magic for you in order for your ap...
For further actions, you may consider blocking this person and/or reporting abuse
Hi Michal, thanks for this!
Quick question: Why do you still register the firebase-messaging-sw.js ?
shouldn't you use the CRA
serviceworker.register()
function to register the CRA sw, which is now appended with the firebase sw?Hi! That's a great question that. I should explain that better.
It's because cra-append-sw on
npm start
is ran in a dev mode meaning that as per their documentation:So if you want it running in development you need to register it. I actually modified the if statement later on and didn't update it in the post. Now it looks like this:
awesome! thanks for this clarification
@sjbuysse apparently we missed that there is a simpler way now for. CRA 4.0.0 has the service worker file available for extension: dev.to/m3h0w/don-t-struggle-with-e...
Thanks for the last answer, one more comment that I needed to make it work for me in production as well.
Firebase is expecting a firebase-messaging-sw.js file to function as a service worker, but you are actually appending your firebase sw to the CRA serviceworker.js in your production build.
So you will have to let firebase messaging know that the serviceworker was registered else where by passing your serviceworker registration to the
getToken()
method when you call it.Interesting. I wonder how I had it working then without passing that in (some kind of version discrepancy maybe?). Thanks for pointing it out! I'll include it in the post.
Wow, thanks a lot. I spent countless hours finding the right solution, and this one actually worked perfectly. :)
Hey @daniel Dopiriak there's a better solution I found today. CRA 4.0.0 has the service worker file available in the src folder: dev.to/m3h0w/don-t-struggle-with-e...
Thanks, Michal. We do use some libraries which might not be supported by the new version of react. I will have to look at it more closely. But I really appreciate your comment.
Based on this discussion I think there is a minor version upgrade in React 16 that is compatible with CRA 4 so maybe you don't have to upgrade to React 17 if that's what you meant :)
Me too! ☀️
for those of us newer to service workers, it would be cool to show an example of your "firebase-messaging-sw.js" file.
Also, how you're handling requesting the user permissions would be nice to see.
Sure. I don't remember where I got the code from to glue it together unfortunately but I appended it to the post so you can take a look if you'd like.
The gist.
Happy to help! ☀️