Skip to main content
rule CX Codex

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

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/js-cache-storage.md
Open on GitHub

What 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 getTheme so localStorage is read once and cached in a Map
  • Add cache invalidation for the storage event
  • Cache cookie reads in an object instead of parsing document.cookie each time

How to install js-cache-storage

Manually: put the files in place

.claude/rules/js-cache-storage.md

The paths frontmatter field limits the files

Step 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

Similar IDE rules

All IDE rules for Frontend and UI →
js-cache-property-access jarrodwatts js-cache-property-access guides Claude Code to store deep object properties and array bounds in local variables before executing JavaScript loops. CX Codex +1 ★ 1,064repo stars react-fetch-cache-patterns pproenca react-fetch-cache-patterns — a skill with 48 rules for scalable data fetching and caching in React: request orchestration, backend protection, prefetching, error resilience. CX Codex +1 180installs cache-components laguagu A skill for mastering Next.js Cache Components and Partial Prerendering in version 16.4+. Advises on the use cache directive, setting cache lifetime with cacheLife(), tagging data with cacheTag(), and invalidating via updateTag(). CX Codex +1 222installs performance-optimization Mindrally performance-optimization — a rule set for web performance: server-side rendering, CSS, JavaScript, assets and caching. Useful when your agent writes frontend code and you want a bar to hold it to. CX Codex +1 907installs competition-browser-persistence zhaoxuya520 competition-browser-persistence — a skill for Claude Code that investigates browser persistence surfaces in CTF sandboxes. It activates when stored client state alters request flows, rendering, or access rules. CX Codex +1 109installs nextjs-cache-architecture mohamed-hossam1 nextjs-cache-architecture structures caching architecture in Next.js 16+ App Router projects. It sets up the "use cache" directive, a typed tag registry, and Suspense boundaries. CX Codex +1 5,100installs

From the same set130

The whole set →
advanced-event-handler-refs advanced-event-handler-refs guides Claude Code to keep event callbacks in refs or useEffectEvent, stopping React effects from re-subscribing on every render. CX Codex +1 ★ 7,191repo stars server-parallel-fetching The server-parallel-fetching rule shows how to compose React Server Components so independent data requests can run in parallel. Use it when one component waits on another component’s fetch. CX Codex +1 ★ 7,191repo stars _sections Defines eight performance optimization sections, ranks them by impact, and guides the agent in prioritizing code improvements. Install once, reference by ID when analyzing bottlenecks. CX Codex +1 ★ 7,191repo stars client-passive-event-listeners client-passive-event-listeners instructs the agent to add { passive: true } to touch and wheel listeners, preventing scroll stutter in browser viewports. CX Codex +1 ★ 7,191repo stars rerender-use-ref-transient-values The rerender-use-ref-transient-values rule teaches the agent to store frequently changing values in useRef instead of useState to avoid re-rendering the component on every update. CX Codex +1 ★ 7,191repo stars js-length-check-first js-length-check-first — a rule for Claude Code that prompts the AI to check array lengths before running expensive operations like sorting or deep equality checks. If lengths differ, arrays cannot be equal—no expensive work needed. CX Codex +1 ★ 7,191repo stars js-tosorted-immutable The js-tosorted-immutable rule replaces mutating .sort() with .toSorted(), keeping React props and state arrays unchanged during sorting. CX Codex +1 ★ 7,191repo stars patterns-children-over-render-props patterns-children-over-render-props — a Claude rule that pushes React components toward composition with children instead of renderX props, so call sites read as plain JSX. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open