@lobehub/lobehub 2.0.0-next.320 → 2.0.0-next.321
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.codex/skills/vercel-react-best-practices/AGENTS.md +2410 -0
- package/.codex/skills/vercel-react-best-practices/SKILL.md +125 -0
- package/.codex/skills/vercel-react-best-practices/rules/advanced-event-handler-refs.md +55 -0
- package/.codex/skills/vercel-react-best-practices/rules/advanced-use-latest.md +49 -0
- package/.codex/skills/vercel-react-best-practices/rules/async-api-routes.md +38 -0
- package/.codex/skills/vercel-react-best-practices/rules/async-defer-await.md +80 -0
- package/.codex/skills/vercel-react-best-practices/rules/async-dependencies.md +36 -0
- package/.codex/skills/vercel-react-best-practices/rules/async-parallel.md +28 -0
- package/.codex/skills/vercel-react-best-practices/rules/async-suspense-boundaries.md +99 -0
- package/.codex/skills/vercel-react-best-practices/rules/bundle-barrel-imports.md +59 -0
- package/.codex/skills/vercel-react-best-practices/rules/bundle-conditional.md +31 -0
- package/.codex/skills/vercel-react-best-practices/rules/bundle-defer-third-party.md +49 -0
- package/.codex/skills/vercel-react-best-practices/rules/bundle-dynamic-imports.md +35 -0
- package/.codex/skills/vercel-react-best-practices/rules/bundle-preload.md +50 -0
- package/.codex/skills/vercel-react-best-practices/rules/client-event-listeners.md +74 -0
- package/.codex/skills/vercel-react-best-practices/rules/client-localstorage-schema.md +71 -0
- package/.codex/skills/vercel-react-best-practices/rules/client-passive-event-listeners.md +48 -0
- package/.codex/skills/vercel-react-best-practices/rules/client-swr-dedup.md +56 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-batch-dom-css.md +57 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-cache-function-results.md +80 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-cache-property-access.md +28 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-cache-storage.md +70 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-combine-iterations.md +32 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-early-exit.md +50 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-hoist-regexp.md +45 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-index-maps.md +37 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-length-check-first.md +49 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-min-max-loop.md +82 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-set-map-lookups.md +24 -0
- package/.codex/skills/vercel-react-best-practices/rules/js-tosorted-immutable.md +57 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-activity.md +26 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-animate-svg-wrapper.md +47 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-conditional-render.md +40 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-content-visibility.md +38 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-hoist-jsx.md +46 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-hydration-no-flicker.md +82 -0
- package/.codex/skills/vercel-react-best-practices/rules/rendering-svg-precision.md +28 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-defer-reads.md +39 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-dependencies.md +45 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-derived-state.md +29 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-functional-setstate.md +74 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-lazy-state-init.md +58 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-memo.md +44 -0
- package/.codex/skills/vercel-react-best-practices/rules/rerender-transitions.md +40 -0
- package/.codex/skills/vercel-react-best-practices/rules/server-after-nonblocking.md +73 -0
- package/.codex/skills/vercel-react-best-practices/rules/server-cache-lru.md +41 -0
- package/.codex/skills/vercel-react-best-practices/rules/server-cache-react.md +76 -0
- package/.codex/skills/vercel-react-best-practices/rules/server-parallel-fetching.md +83 -0
- package/.codex/skills/vercel-react-best-practices/rules/server-serialization.md +38 -0
- package/.cursor/skills/vercel-react-best-practices/AGENTS.md +2410 -0
- package/.cursor/skills/vercel-react-best-practices/SKILL.md +125 -0
- package/.cursor/skills/vercel-react-best-practices/rules/advanced-event-handler-refs.md +55 -0
- package/.cursor/skills/vercel-react-best-practices/rules/advanced-use-latest.md +49 -0
- package/.cursor/skills/vercel-react-best-practices/rules/async-api-routes.md +38 -0
- package/.cursor/skills/vercel-react-best-practices/rules/async-defer-await.md +80 -0
- package/.cursor/skills/vercel-react-best-practices/rules/async-dependencies.md +36 -0
- package/.cursor/skills/vercel-react-best-practices/rules/async-parallel.md +28 -0
- package/.cursor/skills/vercel-react-best-practices/rules/async-suspense-boundaries.md +99 -0
- package/.cursor/skills/vercel-react-best-practices/rules/bundle-barrel-imports.md +59 -0
- package/.cursor/skills/vercel-react-best-practices/rules/bundle-conditional.md +31 -0
- package/.cursor/skills/vercel-react-best-practices/rules/bundle-defer-third-party.md +49 -0
- package/.cursor/skills/vercel-react-best-practices/rules/bundle-dynamic-imports.md +35 -0
- package/.cursor/skills/vercel-react-best-practices/rules/bundle-preload.md +50 -0
- package/.cursor/skills/vercel-react-best-practices/rules/client-event-listeners.md +74 -0
- package/.cursor/skills/vercel-react-best-practices/rules/client-localstorage-schema.md +71 -0
- package/.cursor/skills/vercel-react-best-practices/rules/client-passive-event-listeners.md +48 -0
- package/.cursor/skills/vercel-react-best-practices/rules/client-swr-dedup.md +56 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-batch-dom-css.md +57 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-cache-function-results.md +80 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-cache-property-access.md +28 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-cache-storage.md +70 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-combine-iterations.md +32 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-early-exit.md +50 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-hoist-regexp.md +45 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-index-maps.md +37 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-length-check-first.md +49 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-min-max-loop.md +82 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-set-map-lookups.md +24 -0
- package/.cursor/skills/vercel-react-best-practices/rules/js-tosorted-immutable.md +57 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-activity.md +26 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-animate-svg-wrapper.md +47 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-conditional-render.md +40 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-content-visibility.md +38 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-hoist-jsx.md +46 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-hydration-no-flicker.md +82 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rendering-svg-precision.md +28 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-defer-reads.md +39 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-dependencies.md +45 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-derived-state.md +29 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-functional-setstate.md +74 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-lazy-state-init.md +58 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-memo.md +44 -0
- package/.cursor/skills/vercel-react-best-practices/rules/rerender-transitions.md +40 -0
- package/.cursor/skills/vercel-react-best-practices/rules/server-after-nonblocking.md +73 -0
- package/.cursor/skills/vercel-react-best-practices/rules/server-cache-lru.md +41 -0
- package/.cursor/skills/vercel-react-best-practices/rules/server-cache-react.md +76 -0
- package/.cursor/skills/vercel-react-best-practices/rules/server-parallel-fetching.md +83 -0
- package/.cursor/skills/vercel-react-best-practices/rules/server-serialization.md +38 -0
- package/CHANGELOG.md +25 -0
- package/changelog/v1.json +5 -0
- package/package.json +1 -1
- package/src/layout/GlobalProvider/FaviconProvider.tsx +45 -21
- package/src/server/globalConfig/parseMemoryExtractionConfig.ts +43 -4
- package/src/server/services/memory/userMemory/__tests__/extract.payload.test.ts +101 -0
- package/src/server/services/memory/userMemory/__tests__/extract.runtime.test.ts +111 -0
- package/src/server/services/memory/userMemory/extract.ts +155 -17
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Build Index Maps for Repeated Lookups
|
|
3
|
+
impact: LOW-MEDIUM
|
|
4
|
+
impactDescription: 1M ops to 2K ops
|
|
5
|
+
tags: javascript, map, indexing, optimization, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Build Index Maps for Repeated Lookups
|
|
9
|
+
|
|
10
|
+
Multiple `.find()` calls by the same key should use a Map.
|
|
11
|
+
|
|
12
|
+
**Incorrect (O(n) per lookup):**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
function processOrders(orders: Order[], users: User[]) {
|
|
16
|
+
return orders.map(order => ({
|
|
17
|
+
...order,
|
|
18
|
+
user: users.find(u => u.id === order.userId)
|
|
19
|
+
}))
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Correct (O(1) per lookup):**
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
function processOrders(orders: Order[], users: User[]) {
|
|
27
|
+
const userById = new Map(users.map(u => [u.id, u]))
|
|
28
|
+
|
|
29
|
+
return orders.map(order => ({
|
|
30
|
+
...order,
|
|
31
|
+
user: userById.get(order.userId)
|
|
32
|
+
}))
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Build map once (O(n)), then all lookups are O(1).
|
|
37
|
+
For 1000 orders × 1000 users: 1M ops → 2K ops.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Early Length Check for Array Comparisons
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: avoids expensive operations when lengths differ
|
|
5
|
+
tags: javascript, arrays, performance, optimization, comparison
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Early Length Check for Array Comparisons
|
|
9
|
+
|
|
10
|
+
When comparing arrays with expensive operations (sorting, deep equality, serialization), check lengths first. If lengths differ, the arrays cannot be equal.
|
|
11
|
+
|
|
12
|
+
In real-world applications, this optimization is especially valuable when the comparison runs in hot paths (event handlers, render loops).
|
|
13
|
+
|
|
14
|
+
**Incorrect (always runs expensive comparison):**
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
function hasChanges(current: string[], original: string[]) {
|
|
18
|
+
// Always sorts and joins, even when lengths differ
|
|
19
|
+
return current.sort().join() !== original.sort().join()
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Two O(n log n) sorts run even when `current.length` is 5 and `original.length` is 100. There is also overhead of joining the arrays and comparing the strings.
|
|
24
|
+
|
|
25
|
+
**Correct (O(1) length check first):**
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
function hasChanges(current: string[], original: string[]) {
|
|
29
|
+
// Early return if lengths differ
|
|
30
|
+
if (current.length !== original.length) {
|
|
31
|
+
return true
|
|
32
|
+
}
|
|
33
|
+
// Only sort when lengths match
|
|
34
|
+
const currentSorted = current.toSorted()
|
|
35
|
+
const originalSorted = original.toSorted()
|
|
36
|
+
for (let i = 0; i < currentSorted.length; i++) {
|
|
37
|
+
if (currentSorted[i] !== originalSorted[i]) {
|
|
38
|
+
return true
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return false
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
This new approach is more efficient because:
|
|
46
|
+
- It avoids the overhead of sorting and joining the arrays when lengths differ
|
|
47
|
+
- It avoids consuming memory for the joined strings (especially important for large arrays)
|
|
48
|
+
- It avoids mutating the original arrays
|
|
49
|
+
- It returns early when a difference is found
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Loop for Min/Max Instead of Sort
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: O(n) instead of O(n log n)
|
|
5
|
+
tags: javascript, arrays, performance, sorting, algorithms
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Loop for Min/Max Instead of Sort
|
|
9
|
+
|
|
10
|
+
Finding the smallest or largest element only requires a single pass through the array. Sorting is wasteful and slower.
|
|
11
|
+
|
|
12
|
+
**Incorrect (O(n log n) - sort to find latest):**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
interface Project {
|
|
16
|
+
id: string
|
|
17
|
+
name: string
|
|
18
|
+
updatedAt: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function getLatestProject(projects: Project[]) {
|
|
22
|
+
const sorted = [...projects].sort((a, b) => b.updatedAt - a.updatedAt)
|
|
23
|
+
return sorted[0]
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Sorts the entire array just to find the maximum value.
|
|
28
|
+
|
|
29
|
+
**Incorrect (O(n log n) - sort for oldest and newest):**
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
function getOldestAndNewest(projects: Project[]) {
|
|
33
|
+
const sorted = [...projects].sort((a, b) => a.updatedAt - b.updatedAt)
|
|
34
|
+
return { oldest: sorted[0], newest: sorted[sorted.length - 1] }
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Still sorts unnecessarily when only min/max are needed.
|
|
39
|
+
|
|
40
|
+
**Correct (O(n) - single loop):**
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
function getLatestProject(projects: Project[]) {
|
|
44
|
+
if (projects.length === 0) return null
|
|
45
|
+
|
|
46
|
+
let latest = projects[0]
|
|
47
|
+
|
|
48
|
+
for (let i = 1; i < projects.length; i++) {
|
|
49
|
+
if (projects[i].updatedAt > latest.updatedAt) {
|
|
50
|
+
latest = projects[i]
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return latest
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function getOldestAndNewest(projects: Project[]) {
|
|
58
|
+
if (projects.length === 0) return { oldest: null, newest: null }
|
|
59
|
+
|
|
60
|
+
let oldest = projects[0]
|
|
61
|
+
let newest = projects[0]
|
|
62
|
+
|
|
63
|
+
for (let i = 1; i < projects.length; i++) {
|
|
64
|
+
if (projects[i].updatedAt < oldest.updatedAt) oldest = projects[i]
|
|
65
|
+
if (projects[i].updatedAt > newest.updatedAt) newest = projects[i]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { oldest, newest }
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Single pass through the array, no copying, no sorting.
|
|
73
|
+
|
|
74
|
+
**Alternative (Math.min/Math.max for small arrays):**
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const numbers = [5, 2, 8, 1, 9]
|
|
78
|
+
const min = Math.min(...numbers)
|
|
79
|
+
const max = Math.max(...numbers)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
This works for small arrays, but can be slower or just throw an error for very large arrays due to spread operator limitations. Maximal array length is approximately 124000 in Chrome 143 and 638000 in Safari 18; exact numbers may vary - see [the fiddle](https://jsfiddle.net/qw1jabsx/4/). Use the loop approach for reliability.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Set/Map for O(1) Lookups
|
|
3
|
+
impact: LOW-MEDIUM
|
|
4
|
+
impactDescription: O(n) to O(1)
|
|
5
|
+
tags: javascript, set, map, data-structures, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Set/Map for O(1) Lookups
|
|
9
|
+
|
|
10
|
+
Convert arrays to Set/Map for repeated membership checks.
|
|
11
|
+
|
|
12
|
+
**Incorrect (O(n) per check):**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
const allowedIds = ['a', 'b', 'c', ...]
|
|
16
|
+
items.filter(item => allowedIds.includes(item.id))
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Correct (O(1) per check):**
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
const allowedIds = new Set(['a', 'b', 'c', ...])
|
|
23
|
+
items.filter(item => allowedIds.has(item.id))
|
|
24
|
+
```
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use toSorted() Instead of sort() for Immutability
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: prevents mutation bugs in React state
|
|
5
|
+
tags: javascript, arrays, immutability, react, state, mutation
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use toSorted() Instead of sort() for Immutability
|
|
9
|
+
|
|
10
|
+
`.sort()` mutates the array in place, which can cause bugs with React state and props. Use `.toSorted()` to create a new sorted array without mutation.
|
|
11
|
+
|
|
12
|
+
**Incorrect (mutates original array):**
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
function UserList({ users }: { users: User[] }) {
|
|
16
|
+
// Mutates the users prop array!
|
|
17
|
+
const sorted = useMemo(
|
|
18
|
+
() => users.sort((a, b) => a.name.localeCompare(b.name)),
|
|
19
|
+
[users]
|
|
20
|
+
)
|
|
21
|
+
return <div>{sorted.map(renderUser)}</div>
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (creates new array):**
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
function UserList({ users }: { users: User[] }) {
|
|
29
|
+
// Creates new sorted array, original unchanged
|
|
30
|
+
const sorted = useMemo(
|
|
31
|
+
() => users.toSorted((a, b) => a.name.localeCompare(b.name)),
|
|
32
|
+
[users]
|
|
33
|
+
)
|
|
34
|
+
return <div>{sorted.map(renderUser)}</div>
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**Why this matters in React:**
|
|
39
|
+
|
|
40
|
+
1. Props/state mutations break React's immutability model - React expects props and state to be treated as read-only
|
|
41
|
+
2. Causes stale closure bugs - Mutating arrays inside closures (callbacks, effects) can lead to unexpected behavior
|
|
42
|
+
|
|
43
|
+
**Browser support (fallback for older browsers):**
|
|
44
|
+
|
|
45
|
+
`.toSorted()` is available in all modern browsers (Chrome 110+, Safari 16+, Firefox 115+, Node.js 20+). For older environments, use spread operator:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// Fallback for older browsers
|
|
49
|
+
const sorted = [...items].sort((a, b) => a.value - b.value)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**Other immutable array methods:**
|
|
53
|
+
|
|
54
|
+
- `.toSorted()` - immutable sort
|
|
55
|
+
- `.toReversed()` - immutable reverse
|
|
56
|
+
- `.toSpliced()` - immutable splice
|
|
57
|
+
- `.with()` - immutable element replacement
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Activity Component for Show/Hide
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: preserves state/DOM
|
|
5
|
+
tags: rendering, activity, visibility, state-preservation
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Activity Component for Show/Hide
|
|
9
|
+
|
|
10
|
+
Use React's `<Activity>` to preserve state/DOM for expensive components that frequently toggle visibility.
|
|
11
|
+
|
|
12
|
+
**Usage:**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { Activity } from 'react'
|
|
16
|
+
|
|
17
|
+
function Dropdown({ isOpen }: Props) {
|
|
18
|
+
return (
|
|
19
|
+
<Activity mode={isOpen ? 'visible' : 'hidden'}>
|
|
20
|
+
<ExpensiveMenu />
|
|
21
|
+
</Activity>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Avoids expensive re-renders and state loss.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Animate SVG Wrapper Instead of SVG Element
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: enables hardware acceleration
|
|
5
|
+
tags: rendering, svg, css, animation, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Animate SVG Wrapper Instead of SVG Element
|
|
9
|
+
|
|
10
|
+
Many browsers don't have hardware acceleration for CSS3 animations on SVG elements. Wrap SVG in a `<div>` and animate the wrapper instead.
|
|
11
|
+
|
|
12
|
+
**Incorrect (animating SVG directly - no hardware acceleration):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function LoadingSpinner() {
|
|
16
|
+
return (
|
|
17
|
+
<svg
|
|
18
|
+
className="animate-spin"
|
|
19
|
+
width="24"
|
|
20
|
+
height="24"
|
|
21
|
+
viewBox="0 0 24 24"
|
|
22
|
+
>
|
|
23
|
+
<circle cx="12" cy="12" r="10" stroke="currentColor" />
|
|
24
|
+
</svg>
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Correct (animating wrapper div - hardware accelerated):**
|
|
30
|
+
|
|
31
|
+
```tsx
|
|
32
|
+
function LoadingSpinner() {
|
|
33
|
+
return (
|
|
34
|
+
<div className="animate-spin">
|
|
35
|
+
<svg
|
|
36
|
+
width="24"
|
|
37
|
+
height="24"
|
|
38
|
+
viewBox="0 0 24 24"
|
|
39
|
+
>
|
|
40
|
+
<circle cx="12" cy="12" r="10" stroke="currentColor" />
|
|
41
|
+
</svg>
|
|
42
|
+
</div>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
This applies to all CSS transforms and transitions (`transform`, `opacity`, `translate`, `scale`, `rotate`). The wrapper div allows browsers to use GPU acceleration for smoother animations.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Explicit Conditional Rendering
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: prevents rendering 0 or NaN
|
|
5
|
+
tags: rendering, conditional, jsx, falsy-values
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Explicit Conditional Rendering
|
|
9
|
+
|
|
10
|
+
Use explicit ternary operators (`? :`) instead of `&&` for conditional rendering when the condition can be `0`, `NaN`, or other falsy values that render.
|
|
11
|
+
|
|
12
|
+
**Incorrect (renders "0" when count is 0):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function Badge({ count }: { count: number }) {
|
|
16
|
+
return (
|
|
17
|
+
<div>
|
|
18
|
+
{count && <span className="badge">{count}</span>}
|
|
19
|
+
</div>
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// When count = 0, renders: <div>0</div>
|
|
24
|
+
// When count = 5, renders: <div><span class="badge">5</span></div>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Correct (renders nothing when count is 0):**
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
function Badge({ count }: { count: number }) {
|
|
31
|
+
return (
|
|
32
|
+
<div>
|
|
33
|
+
{count > 0 ? <span className="badge">{count}</span> : null}
|
|
34
|
+
</div>
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// When count = 0, renders: <div></div>
|
|
39
|
+
// When count = 5, renders: <div><span class="badge">5</span></div>
|
|
40
|
+
```
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: CSS content-visibility for Long Lists
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: faster initial render
|
|
5
|
+
tags: rendering, css, content-visibility, long-lists
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## CSS content-visibility for Long Lists
|
|
9
|
+
|
|
10
|
+
Apply `content-visibility: auto` to defer off-screen rendering.
|
|
11
|
+
|
|
12
|
+
**CSS:**
|
|
13
|
+
|
|
14
|
+
```css
|
|
15
|
+
.message-item {
|
|
16
|
+
content-visibility: auto;
|
|
17
|
+
contain-intrinsic-size: 0 80px;
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Example:**
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
function MessageList({ messages }: { messages: Message[] }) {
|
|
25
|
+
return (
|
|
26
|
+
<div className="overflow-y-auto h-screen">
|
|
27
|
+
{messages.map(msg => (
|
|
28
|
+
<div key={msg.id} className="message-item">
|
|
29
|
+
<Avatar user={msg.author} />
|
|
30
|
+
<div>{msg.content}</div>
|
|
31
|
+
</div>
|
|
32
|
+
))}
|
|
33
|
+
</div>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
For 1000 messages, browser skips layout/paint for ~990 off-screen items (10× faster initial render).
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Hoist Static JSX Elements
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: avoids re-creation
|
|
5
|
+
tags: rendering, jsx, static, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Hoist Static JSX Elements
|
|
9
|
+
|
|
10
|
+
Extract static JSX outside components to avoid re-creation.
|
|
11
|
+
|
|
12
|
+
**Incorrect (recreates element every render):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function LoadingSkeleton() {
|
|
16
|
+
return <div className="animate-pulse h-20 bg-gray-200" />
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function Container() {
|
|
20
|
+
return (
|
|
21
|
+
<div>
|
|
22
|
+
{loading && <LoadingSkeleton />}
|
|
23
|
+
</div>
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Correct (reuses same element):**
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
const loadingSkeleton = (
|
|
32
|
+
<div className="animate-pulse h-20 bg-gray-200" />
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
function Container() {
|
|
36
|
+
return (
|
|
37
|
+
<div>
|
|
38
|
+
{loading && loadingSkeleton}
|
|
39
|
+
</div>
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
This is especially helpful for large and static SVG nodes, which can be expensive to recreate on every render.
|
|
45
|
+
|
|
46
|
+
**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, the compiler automatically hoists static JSX elements and optimizes component re-renders, making manual hoisting unnecessary.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Prevent Hydration Mismatch Without Flickering
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: avoids visual flicker and hydration errors
|
|
5
|
+
tags: rendering, ssr, hydration, localStorage, flicker
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Prevent Hydration Mismatch Without Flickering
|
|
9
|
+
|
|
10
|
+
When rendering content that depends on client-side storage (localStorage, cookies), avoid both SSR breakage and post-hydration flickering by injecting a synchronous script that updates the DOM before React hydrates.
|
|
11
|
+
|
|
12
|
+
**Incorrect (breaks SSR):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function ThemeWrapper({ children }: { children: ReactNode }) {
|
|
16
|
+
// localStorage is not available on server - throws error
|
|
17
|
+
const theme = localStorage.getItem('theme') || 'light'
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<div className={theme}>
|
|
21
|
+
{children}
|
|
22
|
+
</div>
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Server-side rendering will fail because `localStorage` is undefined.
|
|
28
|
+
|
|
29
|
+
**Incorrect (visual flickering):**
|
|
30
|
+
|
|
31
|
+
```tsx
|
|
32
|
+
function ThemeWrapper({ children }: { children: ReactNode }) {
|
|
33
|
+
const [theme, setTheme] = useState('light')
|
|
34
|
+
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
// Runs after hydration - causes visible flash
|
|
37
|
+
const stored = localStorage.getItem('theme')
|
|
38
|
+
if (stored) {
|
|
39
|
+
setTheme(stored)
|
|
40
|
+
}
|
|
41
|
+
}, [])
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<div className={theme}>
|
|
45
|
+
{children}
|
|
46
|
+
</div>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Component first renders with default value (`light`), then updates after hydration, causing a visible flash of incorrect content.
|
|
52
|
+
|
|
53
|
+
**Correct (no flicker, no hydration mismatch):**
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
function ThemeWrapper({ children }: { children: ReactNode }) {
|
|
57
|
+
return (
|
|
58
|
+
<>
|
|
59
|
+
<div id="theme-wrapper">
|
|
60
|
+
{children}
|
|
61
|
+
</div>
|
|
62
|
+
<script
|
|
63
|
+
dangerouslySetInnerHTML={{
|
|
64
|
+
__html: `
|
|
65
|
+
(function() {
|
|
66
|
+
try {
|
|
67
|
+
var theme = localStorage.getItem('theme') || 'light';
|
|
68
|
+
var el = document.getElementById('theme-wrapper');
|
|
69
|
+
if (el) el.className = theme;
|
|
70
|
+
} catch (e) {}
|
|
71
|
+
})();
|
|
72
|
+
`,
|
|
73
|
+
}}
|
|
74
|
+
/>
|
|
75
|
+
</>
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The inline script executes synchronously before showing the element, ensuring the DOM already has the correct value. No flickering, no hydration mismatch.
|
|
81
|
+
|
|
82
|
+
This pattern is especially useful for theme toggles, user preferences, authentication states, and any client-only data that should render immediately without flashing default values.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Optimize SVG Precision
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: reduces file size
|
|
5
|
+
tags: rendering, svg, optimization, svgo
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Optimize SVG Precision
|
|
9
|
+
|
|
10
|
+
Reduce SVG coordinate precision to decrease file size. The optimal precision depends on the viewBox size, but in general reducing precision should be considered.
|
|
11
|
+
|
|
12
|
+
**Incorrect (excessive precision):**
|
|
13
|
+
|
|
14
|
+
```svg
|
|
15
|
+
<path d="M 10.293847 20.847362 L 30.938472 40.192837" />
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
**Correct (1 decimal place):**
|
|
19
|
+
|
|
20
|
+
```svg
|
|
21
|
+
<path d="M 10.3 20.8 L 30.9 40.2" />
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Automate with SVGO:**
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npx svgo --precision=1 --multipass icon.svg
|
|
28
|
+
```
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Defer State Reads to Usage Point
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: avoids unnecessary subscriptions
|
|
5
|
+
tags: rerender, searchParams, localStorage, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Defer State Reads to Usage Point
|
|
9
|
+
|
|
10
|
+
Don't subscribe to dynamic state (searchParams, localStorage) if you only read it inside callbacks.
|
|
11
|
+
|
|
12
|
+
**Incorrect (subscribes to all searchParams changes):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function ShareButton({ chatId }: { chatId: string }) {
|
|
16
|
+
const searchParams = useSearchParams()
|
|
17
|
+
|
|
18
|
+
const handleShare = () => {
|
|
19
|
+
const ref = searchParams.get('ref')
|
|
20
|
+
shareChat(chatId, { ref })
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return <button onClick={handleShare}>Share</button>
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Correct (reads on demand, no subscription):**
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
function ShareButton({ chatId }: { chatId: string }) {
|
|
31
|
+
const handleShare = () => {
|
|
32
|
+
const params = new URLSearchParams(window.location.search)
|
|
33
|
+
const ref = params.get('ref')
|
|
34
|
+
shareChat(chatId, { ref })
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return <button onClick={handleShare}>Share</button>
|
|
38
|
+
}
|
|
39
|
+
```
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Narrow Effect Dependencies
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: minimizes effect re-runs
|
|
5
|
+
tags: rerender, useEffect, dependencies, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Narrow Effect Dependencies
|
|
9
|
+
|
|
10
|
+
Specify primitive dependencies instead of objects to minimize effect re-runs.
|
|
11
|
+
|
|
12
|
+
**Incorrect (re-runs on any user field change):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
console.log(user.id)
|
|
17
|
+
}, [user])
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**Correct (re-runs only when id changes):**
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
console.log(user.id)
|
|
25
|
+
}, [user.id])
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**For derived state, compute outside effect:**
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
// Incorrect: runs on width=767, 766, 765...
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (width < 768) {
|
|
34
|
+
enableMobileMode()
|
|
35
|
+
}
|
|
36
|
+
}, [width])
|
|
37
|
+
|
|
38
|
+
// Correct: runs only on boolean transition
|
|
39
|
+
const isMobile = width < 768
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (isMobile) {
|
|
42
|
+
enableMobileMode()
|
|
43
|
+
}
|
|
44
|
+
}, [isMobile])
|
|
45
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Subscribe to Derived State
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: reduces re-render frequency
|
|
5
|
+
tags: rerender, derived-state, media-query, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Subscribe to Derived State
|
|
9
|
+
|
|
10
|
+
Subscribe to derived boolean state instead of continuous values to reduce re-render frequency.
|
|
11
|
+
|
|
12
|
+
**Incorrect (re-renders on every pixel change):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function Sidebar() {
|
|
16
|
+
const width = useWindowWidth() // updates continuously
|
|
17
|
+
const isMobile = width < 768
|
|
18
|
+
return <nav className={isMobile ? 'mobile' : 'desktop'} />
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Correct (re-renders only when boolean changes):**
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
function Sidebar() {
|
|
26
|
+
const isMobile = useMediaQuery('(max-width: 767px)')
|
|
27
|
+
return <nav className={isMobile ? 'mobile' : 'desktop'} />
|
|
28
|
+
}
|
|
29
|
+
```
|