Skip to content
Myr

docs(persist): clarify createJSONStorage behavior and warn about production use (#3541)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* docs: expand storage option description in persist.md * docs: warn that createJSONStorage lacks type safety and validation in persist.md * chore: empty commit to retrigger Vercel deploy ---------

authored by Copilot · committed by GitHub · parent a1f685c · Browse files · 1 file+18 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

Diff stats

1 file+18 added−2 removed

  • other:+18 added−2 removed

1 file+18 added−2 removed

modified:docs/reference/middlewares/persist.md+18 added-2 removed
48 unmodified lines
49- `stateCreatorFn`: A function that takes `set` function, `get` function and `store` as arguments.50 Usually, you will return an object with the methods you want to expose.51- `persistOptions`: An object to define storage options.52 - `name`: A unique name of the item for your store in the storage.53 - **optional** `storage`: Defaults to `createJSONStorage(() => localStorage)`.53 - **optional** `storage`: A `PersistStorage` object used to read and write the persisted state.54 Defaults to `createJSONStorage(() => localStorage)`. The getter function passed to55 `createJSONStorage` is evaluated lazily (only when the storage is first accessed), which56 prevents errors in environments where storage APIs like `localStorage` are unavailable at57 module evaluation time — for example, during server-side rendering (SSR).58 > **Warning:** `createJSONStorage` relies on `JSON.parse` and `JSON.stringify` and does not59 > perform any runtime validation. The value read from storage is cast directly to your state60 > type without checking its shape, so corrupt, stale, or tampered data will not be caught at61 > runtime. For production use, implement a custom `PersistStorage` that validates the62 > deserialized value — for example with a schema validation library such as63 > [Zod](https://zod.dev).64 - **optional** `partialize`: A function to filter state fields before persisting it.65 - **optional** `onRehydrateStorage`: A function or function returning a function that allows66 custom logic before and after state rehydration.67 - **optional** `version`: A version number for the persisted state. If the stored state version
278 unmodified lines
346```347348### Persisting a state with custom storage349350> **Note:** `createJSONStorage` is a convenience helper suited for quick prototyping. It uses351> `JSON.parse`/`JSON.stringify` without runtime validation, so unexpected or corrupted data in352> storage is not caught. For production applications, implement a custom `PersistStorage` that353> validates the persisted value, for example using a schema validation library like354> [Zod](https://zod.dev).355340In this mini tutorial, we’ll create a simple position-tracking system using vanilla store, where356In this mini tutorial, we'll create a simple position-tracking system using vanilla store, where357the position state is persisted in the URL's search parameters. This approach allows state358persistence directly in the browser's URL, which can be useful for maintaining state across page359reloads or sharing links with state embedded.360
642 unmodified lines