556
Bn
bees-nest / files
bees-nest / .opencode / skills / vercel-react-best-practices / rules / server-dedup-props.md
2.0 KB
1--- 2title: Avoid Duplicate Serialization in RSC Props 3impact: LOW 4impactDescription: reduces network payload by avoiding duplicate serialization 5tags: server, rsc, serialization, props, client-components 6--- 7 8## Avoid Duplicate Serialization in RSC Props 9 10**Impact: LOW (reduces network payload by avoiding duplicate serialization)** 11 12RSC→client serialization deduplicates by object reference, not value. Same reference = serialized once; new reference = serialized again. Do transformations (`.toSorted()`, `.filter()`, `.map()`) in client, not server. 13 14**Incorrect (duplicates array):** 15 16```tsx 17// RSC: sends 6 strings (2 arrays × 3 items) 18<ClientList usernames={usernames} usernamesOrdered={usernames.toSorted()} /> 19``` 20 21**Correct (sends 3 strings):** 22 23```tsx 24// RSC: send once 25<ClientList usernames={usernames} /> 26 27// Client: transform there 28'use client' 29const sorted = useMemo(() => [...usernames].sort(), [usernames]) 30``` 31 32**Nested deduplication behavior:** 33 34Deduplication works recursively. Impact varies by data type: 35 36- `string[]`, `number[]`, `boolean[]`: **HIGH impact** - array + all primitives fully duplicated 37- `object[]`: **LOW impact** - array duplicated, but nested objects deduplicated by reference 38 39```tsx 40// string[] - duplicates everything 41usernames={['a','b']} sorted={usernames.toSorted()} // sends 4 strings 42 43// object[] - duplicates array structure only 44users={[{id:1},{id:2}]} sorted={users.toSorted()} // sends 2 arrays + 2 unique objects (not 4) 45``` 46 47**Operations breaking deduplication (create new references):** 48 49- Arrays: `.toSorted()`, `.filter()`, `.map()`, `.slice()`, `[...arr]` 50- Objects: `{...obj}`, `Object.assign()`, `structuredClone()`, `JSON.parse(JSON.stringify())` 51 52**More examples:** 53 54```tsx 55// ❌ Bad 56<C users={users} active={users.filter(u => u.active)} /> 57<C product={product} productName={product.name} /> 58 59// ✅ Good 60<C users={users} /> 61<C product={product} /> 62// Do filtering/destructuring in client 63``` 64 65**Exception:** Pass derived data when transformation is expensive or client doesn't need original.