- docs/agent/external: Vercel react-best-practices and composition-patterns (MIT), Addy Osmani accessibility (MIT), Anthropic frontend-design (Apache-2.0), official Payload skill at v3.92.1 (MIT) — text only, each with its licence, SOURCE.md (repo, commit) and an override note in front of the SKILL.md (ipal-kit first, Payload fetching over swr, DESIGN.md tokens, Payload 3 not 4); manifest.json with sha256 per file - scripts/vendor-external-skills.mjs (maintainer, not shipped): fetches by full commit SHA only, rejects non-text or executable files, regenerates the manifest; --check verifies it - ipal-skills --external [names]: copies ext-* after checking every file against the manifest, downloads nothing, never touches ext-* folders without ipal-kit's SOURCE.md, removes only retired ones it installed; an unknown name stops the run before anything is copied - AGENT.md §14, ipal-setup stage 2 (optional step), README: usage, overrides, how to update the pins - test/externalSkills.test.ts: pins are SHAs, no branch fetch, checksums, text only, licences, override note, installer leaves other folders alone Co-Authored-By: Claude Opus 5.5 <[email protected]>
71 lines
1.6 KiB
Markdown
71 lines
1.6 KiB
Markdown
---
|
|
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()
|
|
}
|
|
})
|
|
```
|