How to add a service worker to an existing, old, react project?

Viewed 141

I'm working on an old react project, which I need to add functionality to, but when deploying the react build on the server, it fails, claiming it cannot find several css and js files, although I published all files within the build folder. I tried different things:

  • First, I kept the old service-worker.js in the production folder the IIS uses, but replaced everything else.
  • Then, I tried also deleting the service-worker.js, since I thought it was optional, and my npm run build didn't create a service-worker.js file.
  • Then, I tried copying the service-worker.js file that existed on production, and manually changing it to point to my css and js files in the /static/ folder of my build folder.

All of these solutions have yielded the same result. So I have a few questions:

  • Is the service worker necessary? If not, could this error relate to something entirely different other than the service worker?
  • If it is necessary, why could my npm run build command not create the service worker with the rest of the files in the build folder?
  • If I do need it, how can I manually add it to a project that already exists?

If the production folder already had a service worker, and my build is not building it, I can also assume maybe my react version is newer, but I find that odd, since the computer I use is one an older employee in my company used, and I didn't manually change anything about this project.

0 Answers
Related