All jobs

Add Route-Based Code Splitting to a React App

Yes. An agent lazy-loads each React Router page with React.lazy and Suspense, checks the Vite build splits them into chunks, and opens a PR with Cypress passing.

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 first maps how the app is bundled and routed. In an app shaped like cypress-io/cypress-realworld-app, package.json pins react 18.2.0 and react-router-dom 5.3.4, builds with vite through @vitejs/plugin-react, and sets engines to Node 22 or 24 under Yarn 1. vite.config.ts writes to build/ with sourcemap: true and loads vite-plugin-eslint and vite-plugin-istanbul for Cypress coverage. src/index.tsx mounts <App /> inside a Router with a shared history, and src/containers/App.tsx renders PrivateRoutesContainer when the authService machine is authorized and the SignInForm and SignUpForm routes otherwise. src/containers/PrivateRoutesContainer.tsx imports six page containers at the top of the file (TransactionsContainer, UserSettingsContainer, NotificationsContainer, BankAccountsContainer, TransactionCreateContainer, TransactionDetailContainer) and renders each inside a PrivateRoute under a Switch. There is no React.lazy anywhere under src, so every page, with its MUI and XState imports, ships in the first chunk a visitor downloads on the sign-in screen.

The agent converts the six static imports in PrivateRoutesContainer.tsx to React.lazy(() => import(...)) and wraps the Switch in a Suspense whose fallback is a centered MUI CircularProgress carrying a data-test attribute, matching the selectors the Cypress specs use. MainLayout and UserOnboardingContainer stay eager, because they render on every private route. SignInForm stays eager too: it is the first screen, and splitting it would add a round trip to the page a user lands on. The four alternate entry points, index.auth0.tsx, index.cognito.tsx, index.google.tsx and index.okta.tsx, render the same containers and need no change. Vite names each dynamic import's chunk after its file, so the agent leaves rollupOptions alone rather than hand-tuning manualChunks.

Each container reads authService and the data machines through props, so lazy loading changes nothing about state. What the agent does check is that vite-plugin-istanbul still instruments the split chunks, since the CI run sets CYPRESS_COVERAGE=true in build:ci, and that yarn types passes with the lazy components typed as React.LazyExoticComponent. It runs yarn build before and after and lists build/assets to show the new per-route chunks beside the main bundle.

The verification follows .github/workflows/main.yml: yarn types, yarn lint, yarn test:unit:ci for the Vitest backend tests in src/__tests__, then yarn build:ci and the seven specs in cypress/tests/ui against the app started the way start:ci starts it, with the Express backend from backend/app.ts on port 3001. Route changes in transaction-feeds.spec.ts, bankaccounts.spec.ts and user-settings.spec.ts now cross chunk boundaries, which is exactly what the suite should prove still works.

Start it with one API call

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

{
  "agent": "codex",
  "input": "Add route-based code splitting to this React app and open a pull request. Read package.json, vite.config.ts, tsconfig.json, src/index.tsx, src/containers/App.tsx, src/containers/PrivateRoutesContainer.tsx, src/components/PrivateRoute.tsx, cypress.config.ts and .github/workflows/main.yml first. In PrivateRoutesContainer.tsx replace the static imports of the six page containers with React.lazy dynamic imports and wrap the Switch in a Suspense whose fallback is a centered MUI CircularProgress with a data-test attribute; keep MainLayout, UserOnboardingContainer and the sign-in and sign-up forms eager, and leave the auth-provider entry points unchanged. Do not add manualChunks. Run yarn build before and after and include the build/assets listing in the pull request description. Then run yarn types, yarn lint, yarn test:unit:ci, yarn build:ci, and the Cypress specs in cypress/tests/ui against the app started the way the workflow's start:ci script starts it, and paste the summaries. Do not change any machine under src/machines or any backend file.",
  "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 touches src/containers/PrivateRoutesContainer.tsx and nothing else in the app code: six React.lazy imports, one Suspense with a fallback, and the data-test attribute on it. The description holds two build/assets listings, the one before with a single application chunk and the one after with one chunk per page container, plus the type check, lint, unit test and Cypress summaries from the sandbox.

The repository's Cypress Tests workflow runs on the pull request: the install job builds with coverage on and the ui-chrome-tests job runs the UI specs in parallel containers against that build, so the split chunks are exercised by the same suite the team already trusts.

A reviewer checks that no container was split that renders on every route, that the fallback does not reuse a data-test value a spec already waits on, and that the auth-provider entry points still compile. Then they look at the first-load chunk size in the listing against the one before.

Run it on every frontend repository

A platform team with several React apps starts one run per repository with POST /o/{org}/sessions, this prompt and the repository in resources. Each run reads that app's own router, so an app on React Router 6 gets lazy on its route objects and an app with a different fallback component keeps its own, and each run ends in its own pull request.

The key's webhook_url receives run.completed for every run it starts, with a session_url for the result, so one script can gather the chunk listings from each description into a single report.

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

Why run it on Rush?

Code splitting is easy to write and easy to get subtly wrong: a route that renders under a different Suspense boundary flashes, a chunk that the coverage plugin skips lowers the reported numbers, and a lazy import inside a Switch in React Router 5 behaves differently from one outside it. In a Rush sandbox the agent builds the app twice, reads the chunk listing, and drives the real browser through the Cypress specs, with every command and its output kept in the run.

The result arrives as a pull request whose own CI runs the same suite in parallel containers, so a reviewer compares two build listings and a green check rather than reasoning about bundler behavior. The same request works across every React repository the team owns, each run in its own sandbox at the same time.

Frequently asked questions

Does React.lazy work with React Router 5's Switch and a custom PrivateRoute?

Yes. Switch picks the matching Route and the PrivateRoute component renders its children, which can be a lazy component as long as a Suspense boundary sits above it. The agent places one Suspense around the Switch in PrivateRoutesContainer.tsx, so every private page shares the same fallback.

Why keep the sign-in form in the main chunk?

Because it is the first screen an unauthenticated visitor sees. Splitting it would make the browser download the main chunk and then a second chunk before anything renders. The six private pages are the ones a visitor never needs before signing in, so they are the ones that move.

Will Cypress code coverage still see the split code?

The vite-plugin-istanbul configuration in vite.config.ts instruments every module Vite transforms, including the ones that end up in dynamic chunks, and build:ci sets CYPRESS_COVERAGE=true the same way. The agent runs the specs with coverage on and includes the summary so a drop would be visible.

What about the Auth0, Cognito, Google and Okta entry points?

They each mount their own App variant that renders the same PrivateRoutesContainer, so the lazy imports apply to them without any change. The agent runs yarn types to confirm all of them still compile.

More React jobs

More jobs like this: Hand off a long task

Keep reading