Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/client-localstorage-schema.md
1---2title: Version and Minimize localStorage Data3impact: MEDIUM4impactDescription: prevents schema conflicts, reduces storage size5tags: client, localStorage, storage, versioning, data-minimization6---78## Version and Minimize localStorage Data910Add version prefix to keys and store only needed fields. Prevents schema conflicts and accidental storage of sensitive data.1112**Incorrect:**1314```typescript15// No version, stores everything, no error handling16localStorage.setItem('userConfig', JSON.stringify(fullUserObject))17const data = localStorage.getItem('userConfig')18```1920**Correct:**2122```typescript23const VERSION = 'v2'2425function saveConfig(config: { theme: string; language: string }) {26try {27localStorage.setItem(`userConfig:${VERSION}`, JSON.stringify(config))28} catch {29// Throws in incognito/private browsing, quota exceeded, or disabled30}31}3233function loadConfig() {34try {35const data = localStorage.getItem(`userConfig:${VERSION}`)36return data ? JSON.parse(data) : null37} catch {38return null39}40}4142// Migration from v1 to v243function migrate() {44try {45const v1 = localStorage.getItem('userConfig:v1')46if (v1) {47const old = JSON.parse(v1)48saveConfig({ theme: old.darkMode ? 'dark' : 'light', language: old.lang })49localStorage.removeItem('userConfig:v1')50}51} catch {}52}53```5455**Store minimal fields from server responses:**5657```typescript58// User object has 20+ fields, only store what UI needs59function cachePrefs(user: FullUser) {60try {61localStorage.setItem('prefs:v1', JSON.stringify({62theme: user.preferences.theme,63notifications: user.preferences.notifications64}))65} catch {}66}67```6869**Always wrap in try-catch:** `getItem()` and `setItem()` throw in incognito/private browsing (Safari, Firefox), when quota exceeded, or when disabled.7071**Benefits:** Schema evolution via versioning, reduced storage size, prevents storing tokens/PII/internal flags.72