All jobs

Add Sentry Crash Reporting to a Vite React App

Yes. An agent wires Sentry into your Vite React app's error boundaries, router and API client, uploads source maps from CI, and keeps it silent when no DSN is set.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent clones the repository into its own cloud sandbox and reads where errors already surface before adding a reporter. In an app like the react-vite app of alan2207/bulletproof-react (React 18.3, React Router 7, Vite 5, Vitest 2, TanStack Query 5), src/app/provider.tsx wraps everything in react-error-boundary with MainErrorFallback, a red screen with a Refresh button; src/app/router.tsx builds a data router with createBrowserRouter, lazy route modules and a route-level ErrorBoundary on the app root that renders a one-line something-went-wrong message; src/lib/api-client.ts is an axios instance whose response interceptor turns every failure into a toast and redirects to login on a 401; and src/lib/auth.tsx uses react-query-auth to load the signed-in User from /auth/me. Configuration goes through src/config/env.ts, a zod schema over VITE_APP_* variables that throws at startup when one is missing or malformed, with .env.example setting API_URL and ENABLE_API_MOCKING. main.tsx starts MSW mocking before rendering when that flag is on. None of these report anywhere: a crash in production is a red screen and nothing else.

The agent installs @sentry/react and @sentry/vite-plugin and adds VITE_APP_SENTRY_DSN to the env schema as an optional string, empty in .env.example and .env.example-e2e, so the CI job that renames the example file keeps working and no test or mock run ever sends an event. A new src/lib/sentry.ts calls Sentry.init only when the DSN is present, with environment from Vite's mode, release from the commit the build plugin injects, the React Router v7 tracing integration fed with the router's hooks, and a tracesSampleRate set low and named in one constant. main.tsx calls it before enableMocking, so a failure in the mocking bootstrap is reported too.

Then it connects the three places errors already pass through. In provider.tsx the existing ErrorBoundary gains an onError that calls Sentry.captureException with the component stack as context, so the fallback screen stays as it is. In routes/app/root.tsx the route ErrorBoundary reads useRouteError() and reports it before rendering. In api-client.ts the interceptor reports responses with a status of 500 or above and network failures with no response, and leaves 4xx alone, because a validation error or an expired session is not a crash. A small SentryUser component mounted inside the auth loader calls Sentry.setUser with the user's id when useUser resolves and clears it on logout, so events carry who was signed in and nothing more. createBrowserRouter is wrapped with Sentry's router wrapper so navigations become transactions with route names rather than raw paths.

Source maps come from the build. vite.config.ts gets build.sourcemap: 'hidden' and the Sentry Vite plugin, enabled only when SENTRY_AUTH_TOKEN is in the build environment, with org and project read from SENTRY_ORG and SENTRY_PROJECT, so a local yarn build without the token produces no upload and no warning. .github/workflows/react-vite-ci.yml passes the three secrets to the build step. The agent adds two Vitest tests next to src/lib/sentry.ts: one that init is not called when the DSN is empty, and one that the error boundary's onError reaches captureException, with @sentry/react mocked. It runs yarn build, yarn test, yarn lint and yarn check-types in the sandbox, the four commands the CI job runs, and puts their output in the pull request.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add Sentry crash reporting to this Vite React app. Read src/config/env.ts, src/app/provider.tsx, src/app/router.tsx, src/lib/api-client.ts, src/lib/auth.tsx, vite.config.ts and the CI workflow first. Initialise Sentry only when a DSN is configured through the existing env schema, report errors from the existing React error boundary and the route error boundary without replacing their fallbacks, report server and network failures from the axios interceptor but not 4xx responses, set the Sentry user from the signed-in user, add router tracing, and upload hidden source maps from CI with the Sentry Vite plugin only when an auth token is present. Add Vitest tests for the no-DSN case and the boundary reporting, run build, tests, lint and type check, and open a pull request.",
  "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 on its own branch that adds src/lib/sentry.ts, src/lib/sentry.test.ts and the SentryUser component, and changes src/config/env.ts, src/main.tsx, src/app/provider.tsx, src/app/router.tsx, src/app/routes/app/root.tsx, src/lib/api-client.ts, vite.config.ts, .env.example, .env.example-e2e, package.json, yarn.lock and the CI workflow. The description lists the three capture points and what each one filters, the three secrets the workflow expects, and the build, test, lint and type-check output from the sandbox.

The run's record keeps every command, including the build with no token present showing no upload attempt, so a reviewer can see that the default path is quiet and that the test suite passes with the new tests counted.

Before merging, a reviewer creates the Sentry project and adds SENTRY_AUTH_TOKEN, SENTRY_ORG and SENTRY_PROJECT as repository secrets and the DSN to the production environment, decides on the trace sample rate, and checks the user context against their privacy policy; the pull request sets only the user id, and the reviewer can remove even that.

Run it on every app you hand off

Start it from a terminal with POST /o/{org}/sessions, this body and the repository in resources, and come back to a pull request with the CI commands' output in it. A follow-up through POST /o/{org}/sessions/{id}/messages wakes the parked sandbox for the next step: session replay on the error path only, a beforeSend that strips query strings, or the same wiring for the Next.js apps in the same repository.

A team with several front ends can start one run per app, each in its own sandbox with its own pull request. A request_id, a UUID per repository, makes a rerun of the script return the run it already started.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Dropping Sentry.init into main.tsx takes a minute and reports almost nothing useful: no user, no route names, no source maps, and a flood of 401s from the API client. The agent reads where the app already catches errors and attaches the reporter there, filters what is not a crash, and sets up the source map upload so the first event is readable.

The sandbox runs the real build, tests, lint and type check and keeps the output in the run's record, so the pull request shows the same four checks passing that CI will run. Several apps can be done at once in separate sandboxes.

Frequently asked questions

Will events be sent from local development or from tests?

No. Sentry.init runs only when VITE_APP_SENTRY_DSN is set, and the example env files leave it empty, so yarn dev, Vitest and the Playwright job send nothing. The test for that case is part of the pull request.

Does it replace my error screens?

No. The MainErrorFallback component and the route-level boundary render exactly as before; the agent adds reporting beside them through onError and useRouteError. If you later want Sentry's own fallback with a feedback form, that is a one-line swap.

Why are 4xx responses not reported?

Because the interceptor already handles them as user-facing messages and a 401 as a redirect to login; sending each one to Sentry would bury real crashes. Responses of 500 and above and requests that never got a response are reported with the method and URL, and the threshold is one constant.

What does the source map upload need?

An auth token with project write scope, the org slug and the project slug, read from SENTRY_AUTH_TOKEN, SENTRY_ORG and SENTRY_PROJECT at build time. Without the token the plugin is not added and the build is unchanged; with it, hidden source maps are uploaded and not served to visitors.

What personal data is attached to an event?

Only the signed-in user's id, set when the auth query resolves and cleared on logout. Sentry's default also records the page URL and the browser user agent. Email and name are left out on purpose, and the pull request points at the single call to change that.

More Vite + React jobs

The same job on other stacks

Keep reading