js-cache-storage — Claude Code rule: what it does, how to install
js-cache-storage keeps localStorage, sessionStorage and cookie values in memory so reads stop hitting synchronous browser storage.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/js-cache-storage.mdWhat js-cache-storage does
Reads from localStorage, sessionStorage and document.cookie block, because all three are synchronous. The rule says to fetch a key once and serve later calls from memory.
Two patterns come with it. Storage gets a Map filled on the first lookup, and the setter writes through so the cache never trails the store. Cookies get parsed once into a plain object. A Map rather than a React hook, so the same helper works in utilities and event handlers.
Another tab, or a cookie set by the server, can change values behind your back. The rule wires a storage listener that drops the touched key and clears the cache on visibilitychange when the tab becomes visible again. Skip that and stale reads follow.
What it helps with
- cut repeated localStorage reads out of a hot render path
- parse document.cookie once instead of on every lookup
- keep the cache in step when code calls setItem
- drop cached keys after another tab changes them
- share one cache helper between utilities and components
Example prompts for the agent
- Rewrite
getThemeso localStorage is read once and cached in aMap - Add cache invalidation for the
storageevent - Cache cookie reads in an object instead of parsing
document.cookieeach time
How to install js-cache-storage
Manually: put the files in place
.claude/rules/js-cache-storage.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/js-cache-storage.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/js-cache-storage.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/js-cache-storage.mdManually: put the files in place
.clinerules/js-cache-storage.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about js-cache-storage
How do I install js-cache-storage in Claude Code?
Copy the rule file to .claude/rules/js-cache-storage.md in your project.
What does js-cache-storage do?
It has the agent cache reads from localStorage, sessionStorage and document.cookie in memory rather than calling the synchronous APIs on every access.
When does the cache need invalidating?
When values can change outside the current code, such as another tab writing to storage or a server-set cookie. The rule handles both with the storage event and visibilitychange.
Source text
js-cache-storage.md · MIT
---
title: Cache Storage API Calls
impact: LOW-MEDIUM
impactDescription: reduces expensive I/O
tags: javascript, localStorage, storage, caching, performance
---
## Cache Storage API Calls
`localStorage`, `sessionStorage`, and `document.cookie` are synchronous and expensive. Cache reads in memory.
**Incorrect (reads storage on every call):**
```typescript
function getTheme() {
return localStorage.getItem('theme') ?? 'light'
}
// Called 10 times = 10 storage reads
```
**Correct (Map cache):**
```typescript
const storageCache = new Map<string, string | null>()
function getLocalStorage(key: string) {
if (!storageCache.has(key)) {
storageCache.set(key, localStorage.getItem(key))
}
return storageCache.get(key)
}
function setLocalStorage(key: string, value: string) {
localStorage.setItem(key, value)
storageCache.set(key, value) // keep cache in sync
}
```
Use a Map (not a hook) so it works everywhere: utilities, event handlers, not just React components.
**Cookie caching:**
```typescript
let cookieCache: Record<string, string> | null = null
function getCookie(name: string) {
if (!cookieCache) {
cookieCache = Object.fromEntries(
document.cookie.split('; ').map(c => c.split('='))
)
}
return cookieCache[name]
}
```
**Important (invalidate on external changes):**
If storage can change externally (another tab, server-set cookies), invalidate cache:
```typescript
window.addEventListener('storage', (e) => {
if (e.key) storageCache.delete(e.key)
})
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
storageCache.clear()
}
})
```
© tech-leads-club · MIT · tech-leads-club/agent-skills