Service Worker fails to build in typescript Create React App

Viewed 293

How do I add a service worker to an existing create-react-app project without re-bootstrapping it?

I've been working for a while on a typescript project bootstrapped with create-react-app. I'm not sure exactly how it was originally bootstrapped, but I've invested a significant amount of time into the project so re-bootstrapping it (deleting it and starting over) isn't really an option.

I want to add service worker functionality into the app. However I'm finding that no matter what files I edit, after I run yarn build, the file build/service-worker.js is the default auto-generated service worker that comes with workbox. I've tried creating and editing files src/sw.js, src/sw.ts, src/service-worker.ts and src/service-worker.js.

I can't find any reference in the documentation for which file I'm supposed to edit, or any config file to edit where I can specify that I want my service worker file to be compiled.

When the project is run (yarn build + serve -s build -l 4000), I can see that the service worker registration completes successfully (through src/serviceWorkerRegister.ts and the default service worker is loaded into the app. This is the same file found in build/service-worker.js and is auto-generated.

0 Answers
Related