Skip to content
Advertisement

React.js builds with Vite does not include service-worker.ts

I am using Vite to build an SPA with React (typescript), and I am trying to register a service-worker. I am registering the script as type module, and service-worker.ts sits at src/web-worker/service-worker.ts. There is also a tsconfig.json at src/web-worker

Everything works in Dev, but when it’s built, src/web-worker/service-worker.ts is not replaced with anything equivalent.

Any suggestions?

index.html

JavaScript

src/web-worker/service-worker.ts

JavaScript

src/web-worker/tsconfig.json

JavaScript

Advertisement

Answer

There is a vitejs plugin for this https://github.com/antfu/vite-plugin-pwa. You can find the react documentation here https://vite-plugin-pwa.netlify.app/examples/react.html

Advertisement