Stop the agent from reading localStorage directly when the repo has a storage wrapper
The PR calls localStorage.getItem or localStorage.setItem in a component, or it JSON-parses a raw key on first render. The repo already has a storage wrapper with a key prefix, parse/serialize, an SSR guard, and maybe a
The PR calls localStorage.getItem or localStorage.setItem in a component, or it JSON-parses a raw key on first render. The repo already has a storage wrapper with a key prefix, parse/serialize, an SSR guard, and maybe a schema version. Nearby preference and draft code uses it. The direct call crashes during SSR, collides with an unprefixed key, and skips the migration the wrapper already owns.
I've started treating a raw localStorage call as a contract break, not as a quicker persist. Saving a string is the easy part. A safe key, a parse path, and a server-safe read are why the wrapper is there.
Why the direct call shows up
A tutorial "remember the tab" example is localStorage.setItem("tab", id) in an effect. The model can write that without finding lib/storage or useLocalStorage. Your wrapper may be named storage.get, prefs.get, or safeStorage, which is easy to miss if the prompt only says "persist this."
You also skip quota errors, private-mode exceptions, and the rule about not reading storage during render on a server-rendered page.
A simple storage policy
- New browser persistence uses the existing storage wrapper
- Do not call localStorage or sessionStorage directly in a feature file
- Do not read storage during render on an SSR path
- A new key, prefix, or schema version is a wrapper change, with a test
If the value belongs on the server or in the existing data library, that is not a localStorage feature. If the team already persists this key through the wrapper, extend that key instead of inventing a second one.
What to put in the prompt
Use the existing storage wrapper for this preference.
Do not call localStorage or sessionStorage directly in this file.
Keep the key prefix, parse path, and SSR guard on the wrapper.
If the value should live on the server or in the existing data library, use that instead.
Paste one existing wrapper usage. In review, search the diff for localStorage, sessionStorage, and a JSON.parse of a raw storage key in a component.
Reject / send back
Reject
- A feature-level persist that calls
localStorageorsessionStoragedirectly - A raw key with no prefix when the wrapper already namespaces keys
- A first-render storage read that is unsafe on the server
- A second storage helper added for one preference
Accept
- A preference that uses the existing storage wrapper
- A wrapper PR that adds a documented key or schema version and a test
- A value that belongs on the server and never entered localStorage
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.