# Add PWA Support to a Vite React App

Yes. An agent adds vite-plugin-pwa to your Vite React app without breaking its MSW mock worker, builds the manifest from your icons, and opens a PR with CI passing.

[Run this on Rush](https://cloud.getrush.ai/run/add-pwa-support/vite-react/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads what the app already ships before it adds a worker, because this app already has one. In bulletproof-react's `apps/react-vite` workspace (Vite 5, React 18, React Router 7, Tailwind 3, Vitest 2, Playwright, MSW 2, Yarn), `public/` holds `favicon.ico`, `logo192.png`, `logo512.png`, `robots.txt`, a `_redirects` file that sends every path to `index.html`, and `mockServiceWorker.js`, the worker MSW generates; `package.json` pins it there with `"msw": {"workerDirectory": "public"}`. `index.html` sets `theme-color` to `#000000`, links the favicon, loads the Inter font from a remote stylesheet and has no manifest link. `src/main.tsx` calls `enableMocking()` before rendering, and `src/testing/mocks/index.ts` starts the MSW worker from `msw/browser` whenever `VITE_APP_ENABLE_API_MOCKING` is `true`, which `.env.example` sets and `.env.example-e2e` turns off. `vite.config.ts` registers `react()` and `viteTsconfigPaths()`, serves on port 3000, and configures Vitest with jsdom and `src/testing/setup-tests.ts`. There is no manifest, no app service worker and no offline behaviour.

Two service workers on the same scope is the trap. In development and in the CI build, MSW registers `mockServiceWorker.js` at the root to intercept `fetch`; a PWA worker registered at the same scope would replace it or be replaced, and every mocked request would start failing in a way that looks like the API is down. So the agent keeps the PWA worker out of development entirely: `vite-plugin-pwa` leaves its `devOptions` disabled by default, which means `yarn dev` and `yarn test-e2e`, which runs Playwright against the mock server from `mock-server.ts`, behave as before. The agent also registers the worker only when API mocking is off, by reading the same `env.ENABLE_API_MOCKING` from `src/config/env.ts`, so a production build with mocking turned on for a demo still gets MSW and not the cache.

The change is one dev dependency and a few files. `vite.config.ts` adds `VitePWA` with `registerType: 'autoUpdate'`, a manifest built from the existing `logo192.png` and `logo512.png` with the app's title, `start_url` of `/`, `display: standalone`, the `#000000` theme colour `index.html` already declares, and a `workbox` section that precaches the built assets and sets `navigateFallback` to the index so a deep link to a discussion page opens offline, with `navigateFallbackDenylist` for the `/api` prefix so the mock server and the real API are never served from cache. Requests to `env.API_URL` get no runtime caching: `src/lib/api-client.ts` sends credentials on every call, and caching those responses would keep private data on disk. `index.html` gains the `apple-touch-icon` link for `logo192.png`, and a new `src/lib/pwa.ts` calls `registerSW` from `virtual:pwa-register` after `createRoot(...).render` in `src/main.tsx`, so a failed registration never blocks first paint. `src/vite-env.d.ts` gains the `vite-plugin-pwa/client` type reference so `yarn check-types` passes; file names stay kebab case for the `check-file` rule in `.eslintrc.cjs`.

Then it runs what `.github/workflows/react-vite-ci.yml` runs in `apps/react-vite`: `mv .env.example .env`, `yarn install`, `yarn build` (which is `tsc && vite build --base=/`), `yarn test`, `yarn lint` and `yarn check-types`, and the e2e job's `mv .env.example-e2e .env`, `yarn playwright install --with-deps` and `yarn test-e2e`. It lists `dist/` to show `sw.js`, the Workbox runtime and `manifest.webmanifest` beside `mockServiceWorker.js`, serves the build with `yarn preview` with mocking off, opens it in the sandbox browser, and reads the application panel to confirm the manifest and the registered worker are the PWA's, then repeats with mocking on to confirm MSW's worker is the one registered.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Make the Vite React app in apps/react-vite a progressive web app and open a pull request. Read package.json, vite.config.ts, index.html, src/main.tsx, src/config/env.ts, src/testing/mocks/index.ts, src/testing/mocks/browser.ts, src/lib/api-client.ts, src/vite-env.d.ts, .eslintrc.cjs, .env.example, .env.example-e2e, the files under public/, playwright.config.ts and .github/workflows/react-vite-ci.yml first, and note that MSW already registers public/mockServiceWorker.js when VITE_APP_ENABLE_API_MOCKING is true. Add vite-plugin-pwa as a dev dependency with yarn and register VitePWA in vite.config.ts with registerType autoUpdate, a manifest built from logo192.png and logo512.png with start_url /, and a workbox config that precaches the build, uses the index as navigateFallback and denies the /api prefix; add no runtime caching for the API. Leave devOptions off. Add the apple-touch-icon link to index.html. Add src/lib/pwa.ts that calls registerSW from virtual:pwa-register and call it from src/main.tsx after render, only when API mocking is off, so the MSW worker is never replaced. Add the plugin's client types to src/vite-env.d.ts. Run yarn build, yarn test, yarn lint and yarn check-types with .env.example, then yarn test-e2e with .env.example-e2e, list dist, serve the build with yarn preview with mocking off and on, and confirm in the browser which worker is registered each time. Paste the results and the dist listing in the pull request. Do not change any route, feature or component.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds `vite-plugin-pwa` to `package.json` and `yarn.lock`, the `VitePWA` block in `vite.config.ts`, one link in `index.html`, `src/lib/pwa.ts`, the call in `src/main.tsx`, and a type reference in `src/vite-env.d.ts`. Nothing under `src/features`, `src/app/routes` or `src/components` changes. The description shows the `dist/` listing with `sw.js` and `manifest.webmanifest`, the build, test, lint and type-check output, the Playwright summary, and which worker the browser reported with mocking on and off.

The repository's `React Vite CI` workflow runs both of its jobs on the pull request: the build, unit tests, lint and type check with `.env.example`, and the Playwright suite against the mock server with `.env.example-e2e`, so the e2e tests prove routing and login behave as before with the plugin in the build. The run's record keeps every command with its output, including the two `yarn preview` sessions.

A reviewer checks that no `/api` route is precached or runtime-cached, that the worker is registered after render and only when mocking is off, that `devOptions` stays off, and decides whether `autoUpdate` or a prompt on update suits their users. Then they merge.

## Run it on every new frontend release

A team with several Vite apps starts one run per repository from the terminal with `POST /o/{org}/sessions`, this prompt and the repository in `resources`. Each run reads that app's own icons, API client and mock setup, so an app without MSW gets the worker registered unconditionally and an app that already links a manifest keeps it, and each run ends in its own pull request.

The key's `webhook_url` receives `run.completed` for each run it starts, with a `session_url` for the result, so a script can post the pull request links and the `dist` listings to the team channel.

## Why run it on Rush?

A service worker is the one piece of a frontend that can break the app for every returning visitor at once, and in this app it can also break every developer's mocked API if it replaces the MSW worker. In a Rush sandbox the agent builds the app, serves the real `dist` with mocking off and on, reads which worker the browser registered each time, and runs the full Playwright suite behind the plugin, with every command and screenshot kept in the run's record, so the pull request shows the two workers staying out of each other's way rather than asserting it.

The result passes the repository's own CI, both jobs, before a reviewer opens it, and the API stays uncached by design. The same request works on every Vite app the team ships, each in its own sandbox at the same time.

## Frequently asked questions

### Will the PWA worker fight with MSW's mockServiceWorker.js?

Not with this change. `devOptions` stays off, so no PWA worker exists in `yarn dev`, and the registration call runs only when `ENABLE_API_MOCKING` is false, so a build with mocking on registers MSW alone. The agent confirms both in the browser from `yarn preview`.

### Why is nothing from the API cached?

`src/lib/api-client.ts` sends credentials with every request, and the responses are discussions, comments and users for the signed-in person. Caching them would keep private data on disk and show stale pages. The `/api` prefix is also denied as a navigation fallback so the mock server's routes never resolve to `index.html`.

### Does the Playwright suite still run against the mock server?

Yes. `yarn test-e2e` starts `mock-server.ts` with pm2 and runs with `.env.example-e2e`, which turns mocking off and points `VITE_APP_API_URL` at port 8080. The plugin only changes the production build output, so the e2e job in CI is unchanged.

### How do users get a new version?

`registerType: 'autoUpdate'` makes the worker take over on the next load after a deploy. A reviewer who prefers a prompt switches to `prompt` and adds a small banner; the pull request names the option.

## More Vite React jobs

- [Dockerize a Vite React App](https://getrush.ai/run/dockerize-app/vite-react/)
- [Find Dead Code in a Vite React App](https://getrush.ai/run/find-dead-code/vite-react/)
- [Migrate ESLint to Flat Config in a Vite React App](https://getrush.ai/run/migrate-eslint-flat-config/vite-react/)

## The same job on other stacks

- [Add PWA Support to a Vue App](https://getrush.ai/run/add-pwa-support/vue/)
- [Add PWA Support to an Angular App](https://getrush.ai/run/add-pwa-support/angular/)
- [Add PWA Support to an Astro Site](https://getrush.ai/run/add-pwa-support/astro/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
