DEV Community

Discussion on: The easiest way to extend or customize Create React App service worker without ejecting

Collapse
 
sjbuysse profile image
sjbuysse

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?

Collapse
 
m3h0w profile image
Michał Gacka • Edited

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:

dev creates public/<file> instead of appending the code to build/service-worker.js
Enter fullscreen mode Exit fullscreen mode

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:

if ('serviceWorker' in navigator && process.env.NODE_ENV !== 'production')
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sjbuysse profile image
sjbuysse

awesome! thanks for this clarification

Thread Thread
 
m3h0w profile image
Michał Gacka

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