# Persist Redux Toolkit State Across Page Reloads

Yes. The agent persists the slices you name to localStorage, keeps request status and the RTK Query cache out of it, and tests the reload round trip.

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

## What the agent does

Persisting Redux state is two decisions and a little code: which state survives a reload, and how it gets back into the store. The agent settles the first by reading the store. In a project like the official Vite template in reduxjs/redux-templates (`packages/vite-template-redux`), `src/app/store.ts` combines two things with `combineSlices`: `counterSlice`, whose state is `{ value, status }`, and `quotesApiSlice`, the RTK Query cache under `quotesApi`. Only `value` is worth keeping. The cache is server data that has to be refetched anyway, and `status` describes a request in flight.

That second exclusion is the one people miss. `incrementAsync` sets `status` to `loading` while its promise is pending, and `Counter.tsx` disables the Add Async button whenever `status` is not `idle`. Persist the whole slice, close the tab during that half second, and the stored state reads `{"value":1,"status":"loading"}`. On the next load the store rehydrates it as it was: no request is pending, nothing will ever set the status back, and the button stays disabled until someone clears site data. So the agent stores the value alone and rebuilds the rest from the slice's initial state.

How it gets back in is already half built. `makeStore` takes a `preloadedState` typed `Partial<RootState>`, which the template's test helper already uses. The agent writes a small `src/app/persist.ts` with `loadState`, which reads one localStorage key, parses it, accepts only a finite number and returns `{ counter: { value, status: "idle" } }` or nothing, and `saveState`, which writes the value and swallows a storage error. `store.ts` then calls `makeStore(loadState())` and subscribes a writer that saves only when the value has changed, so the stream of RTK Query actions does not touch storage. No dependency is added, and the type check passes without a cast.

It weighs `redux-persist` first, since that is what the request usually names, and the trial is instructive on this template. Version 6.0.0, published in September 2019 and still the latest when this page was written, does work at runtime with Redux Toolkit 2: with `whitelist: ["counter"]` it stores the slice under `persist:root`. But `persistReducer` returns a reducer whose state includes `_persist`, and passing it to `configureStore` beside `preloadedState?: Partial<RootState>` fails `tsc -b` in this template, so every test helper that preloads state would need a cast. It also stores `status` unless a transform strips it, and needs its six action types added to `serializableCheck.ignoredActions`. The agent uses it when the prompt insists and says what each of those costs.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Persist part of this app's Redux Toolkit state across page reloads. Read package.json, the store setup, every slice and RTK Query API slice, the components that read them, the entry file, the test helper that builds a store and the existing tests first. Install dependencies and record the test, type check and lint results before changing anything. Decide what to persist field by field and justify each: never persist an RTK Query API reducer, and never persist request status, loading flags or errors, only the data a user would expect to see again. If the store factory already accepts preloaded state, use that: read the saved state once at startup, validate its shape and types, fall back to the initial state on anything unexpected, and write changes with a store subscription that saves only when a persisted field changed. Guard every storage call so the app works when localStorage is blocked or full. Add no dependency for this unless the store cannot take preloaded state; if you consider redux-persist, try it against the type check and report what you found. Include a version in the stored value or key so a later shape change can be handled. Add tests: a saved value comes back in a new store; transient fields come back at their initial values; malformed or wrong-typed storage falls back to the initial state; the existing tests still start from a clean state. Run tests, type check, lint and the build. Open a pull request with the list of persisted and excluded fields and the output of each command.",
  "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

A pull request with one new module, a small change where the store is created, and a test file. The description leads with a table of every field in the store and a verdict for each: persisted, or excluded with the reason. For the template that table has three rows, and the row for `status` explains the disabled button a reader would otherwise find in production.

It then gives the storage format, the key name and its version, and what happens to a value saved by an older build. The outputs of `npm test`, `npm run type-check`, `npm run lint` and `npm run build` follow, along with the result of the `redux-persist` trial when one was made, including the compiler error it produced.

A reviewer looks hardest at validation. Whatever is in localStorage was written by some earlier version of the app, or by a user with developer tools open, and it goes straight into the store; the tests for malformed and wrong-typed values are the ones that keep a bad string from crashing startup. They also confirm nothing sensitive is in the persisted set, since localStorage is readable by any script on the page.

## Run it on every Linear issue labelled persistence

Create an automation in the Project that has the repository, with this prompt and a `linear` trigger on `linear.issue.labeled` for the label you choose. The issue is placed after the prompt as data and no placeholder is filled in, so the prompt should say that the issue names the state to keep, for example a draft form or a theme choice, and that anything it does not name stays out. The trigger needs a Linear connection for the member the automation runs as, made in the console under Settings → Connections.

Each labelled issue starts one run, up to the automation's max runs per hour, and later ones wait as queued runs. Two runs that both edit the store setup will conflict, so merge them in order. To start one from a script, send this body to `POST /o/{org}/sessions` with a `request_id` that is a UUID v5 of the issue id; a retry then returns the same run.

## Why run it on Rush?

Whether a library fits a codebase is settled by trying it, and trying it takes an install, a type check and a test run. In its own sandbox the agent does that for the dependency-free route and, where asked, for `redux-persist`, and pastes the compiler output for each, so the choice in the pull request rests on output and not on a blog post. Every command is in the transcript at `GET /o/{org}/sessions/{id}/messages`.

The stuck-button case is also only convincing when reproduced: dispatch the async action, save mid-flight, build a second store from what was saved and read its state. That is a few lines in a test, run on a clean checkout that no developer has to set aside their own work to provide.

## Frequently asked questions

### Should the RTK Query cache be persisted too?

Usually not. It holds server data with its own freshness rules, and restoring it shows stale results until a refetch replaces them. RTK Query has an `extractRehydrationInfo` hook for the cases where it is wanted, such as offline-first apps; say so in the prompt and the agent wires it and tests that a refetch still happens.

### Does this work with sessionStorage or IndexedDB?

sessionStorage is a one-word change, since it has the same synchronous API. IndexedDB is asynchronous, so the store can no longer be created with the saved state in hand: the app has to render a loading state, or dispatch a hydrate action once the read finishes. The agent implements that variant when the data is too large for localStorage and says which it chose.

### Will persisted state break server-side rendering?

It can, if the server renders the initial state and the client hydrates with the saved one, because the markup then differs. The template is a client-only Vite app, so this does not arise. For a server-rendered app the agent loads saved state after hydration, in an effect, and notes the brief flash of initial values that causes.

### What happens when the slice's shape changes later?

The stored value carries a version. A build that expects a newer version either migrates the old value in `loadState` or discards it and starts from the initial state. The agent writes the discard path now and leaves a commented place for the first migration, so the next shape change is a small edit and not a production incident.

## More Redux Toolkit jobs

- [Write Vitest Tests for RTK Query and Redux Toolkit Slices](https://getrush.ai/run/write-vitest-tests/redux-toolkit/)

## The same job on other stacks

- [Persist a Zustand Store with the Persist Middleware](https://getrush.ai/run/add-state-persistence/zustand/)

## More jobs like this: Turn a Linear label into a PR

- [Add Storybook to a SolidJS App](https://getrush.ai/run/add-storybook/solidjs/)
- [Add Streamable HTTP to a stdio MCP Server](https://getrush.ai/run/add-streamable-http-transport/mcp-server/)
- [Add Structured Data to a Pelican Blog](https://getrush.ai/run/add-structured-data/pelican/)
- [Add JSON-LD Structured Data to a Zola Site](https://getrush.ai/run/add-structured-data/zola/)

## 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/)
