create-modern-react 2.3.4 → 2.3.5
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/lib/setup.js +24 -1
- package/package.json +4 -2
- package/scripts/postinstall.js +18 -0
- package/templates/base/.claude/skills.tar.gz +0 -0
- package/templates/base/.claude/skills/agent-browser/SKILL.md +0 -356
- package/templates/base/.claude/skills/agent-browser/references/authentication.md +0 -188
- package/templates/base/.claude/skills/agent-browser/references/proxy-support.md +0 -175
- package/templates/base/.claude/skills/agent-browser/references/session-management.md +0 -181
- package/templates/base/.claude/skills/agent-browser/references/snapshot-refs.md +0 -186
- package/templates/base/.claude/skills/agent-browser/references/video-recording.md +0 -162
- package/templates/base/.claude/skills/agent-browser/templates/authenticated-session.sh +0 -91
- package/templates/base/.claude/skills/agent-browser/templates/capture-workflow.sh +0 -68
- package/templates/base/.claude/skills/agent-browser/templates/form-automation.sh +0 -64
- package/templates/base/.claude/skills/autoskill/skill.md +0 -134
- package/templates/base/.claude/skills/design-principles/skill.md +0 -237
- package/templates/base/.claude/skills/frontend-design/skill.md +0 -42
- package/templates/base/.claude/skills/learn-together/skill.md +0 -448
- package/templates/base/.claude/skills/question-me/skill.md +0 -175
- package/templates/base/.claude/skills/react-best-practices/AGENTS.md +0 -2410
- package/templates/base/.claude/skills/react-best-practices/README.md +0 -123
- package/templates/base/.claude/skills/react-best-practices/SKILL.md +0 -135
- package/templates/base/.claude/skills/react-best-practices/metadata.json +0 -15
- package/templates/base/.claude/skills/react-best-practices/rules/_sections.md +0 -46
- package/templates/base/.claude/skills/react-best-practices/rules/_template.md +0 -28
- package/templates/base/.claude/skills/react-best-practices/rules/advanced-event-handler-refs.md +0 -55
- package/templates/base/.claude/skills/react-best-practices/rules/advanced-use-latest.md +0 -49
- package/templates/base/.claude/skills/react-best-practices/rules/async-api-routes.md +0 -38
- package/templates/base/.claude/skills/react-best-practices/rules/async-defer-await.md +0 -80
- package/templates/base/.claude/skills/react-best-practices/rules/async-dependencies.md +0 -36
- package/templates/base/.claude/skills/react-best-practices/rules/async-parallel.md +0 -28
- package/templates/base/.claude/skills/react-best-practices/rules/async-suspense-boundaries.md +0 -99
- package/templates/base/.claude/skills/react-best-practices/rules/bundle-barrel-imports.md +0 -59
- package/templates/base/.claude/skills/react-best-practices/rules/bundle-conditional.md +0 -31
- package/templates/base/.claude/skills/react-best-practices/rules/bundle-defer-third-party.md +0 -49
- package/templates/base/.claude/skills/react-best-practices/rules/bundle-dynamic-imports.md +0 -35
- package/templates/base/.claude/skills/react-best-practices/rules/bundle-preload.md +0 -50
- package/templates/base/.claude/skills/react-best-practices/rules/client-event-listeners.md +0 -74
- package/templates/base/.claude/skills/react-best-practices/rules/client-localstorage-schema.md +0 -71
- package/templates/base/.claude/skills/react-best-practices/rules/client-passive-event-listeners.md +0 -48
- package/templates/base/.claude/skills/react-best-practices/rules/client-swr-dedup.md +0 -56
- package/templates/base/.claude/skills/react-best-practices/rules/js-batch-dom-css.md +0 -57
- package/templates/base/.claude/skills/react-best-practices/rules/js-cache-function-results.md +0 -80
- package/templates/base/.claude/skills/react-best-practices/rules/js-cache-property-access.md +0 -28
- package/templates/base/.claude/skills/react-best-practices/rules/js-cache-storage.md +0 -70
- package/templates/base/.claude/skills/react-best-practices/rules/js-combine-iterations.md +0 -32
- package/templates/base/.claude/skills/react-best-practices/rules/js-early-exit.md +0 -50
- package/templates/base/.claude/skills/react-best-practices/rules/js-hoist-regexp.md +0 -45
- package/templates/base/.claude/skills/react-best-practices/rules/js-index-maps.md +0 -37
- package/templates/base/.claude/skills/react-best-practices/rules/js-length-check-first.md +0 -49
- package/templates/base/.claude/skills/react-best-practices/rules/js-min-max-loop.md +0 -82
- package/templates/base/.claude/skills/react-best-practices/rules/js-set-map-lookups.md +0 -24
- package/templates/base/.claude/skills/react-best-practices/rules/js-tosorted-immutable.md +0 -57
- package/templates/base/.claude/skills/react-best-practices/rules/rendering-activity.md +0 -26
- package/templates/base/.claude/skills/react-best-practices/rules/rendering-animate-svg-wrapper.md +0 -47
- package/templates/base/.claude/skills/react-best-practices/rules/rendering-conditional-render.md +0 -40
- package/templates/base/.claude/skills/react-best-practices/rules/rendering-content-visibility.md +0 -38
- package/templates/base/.claude/skills/react-best-practices/rules/rendering-hoist-jsx.md +0 -46
- package/templates/base/.claude/skills/react-best-practices/rules/rendering-hydration-no-flicker.md +0 -82
- package/templates/base/.claude/skills/react-best-practices/rules/rendering-svg-precision.md +0 -28
- package/templates/base/.claude/skills/react-best-practices/rules/rerender-defer-reads.md +0 -39
- package/templates/base/.claude/skills/react-best-practices/rules/rerender-dependencies.md +0 -45
- package/templates/base/.claude/skills/react-best-practices/rules/rerender-derived-state.md +0 -29
- package/templates/base/.claude/skills/react-best-practices/rules/rerender-functional-setstate.md +0 -74
- package/templates/base/.claude/skills/react-best-practices/rules/rerender-lazy-state-init.md +0 -58
- package/templates/base/.claude/skills/react-best-practices/rules/rerender-memo.md +0 -44
- package/templates/base/.claude/skills/react-best-practices/rules/rerender-transitions.md +0 -40
- package/templates/base/.claude/skills/react-best-practices/rules/server-after-nonblocking.md +0 -73
- package/templates/base/.claude/skills/react-best-practices/rules/server-cache-lru.md +0 -41
- package/templates/base/.claude/skills/react-best-practices/rules/server-cache-react.md +0 -76
- package/templates/base/.claude/skills/react-best-practices/rules/server-parallel-fetching.md +0 -83
- package/templates/base/.claude/skills/react-best-practices/rules/server-serialization.md +0 -38
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
# React Best Practices
|
|
2
|
-
|
|
3
|
-
A structured repository for creating and maintaining React Best Practices optimized for agents and LLMs.
|
|
4
|
-
|
|
5
|
-
## Structure
|
|
6
|
-
|
|
7
|
-
- `rules/` - Individual rule files (one per rule)
|
|
8
|
-
- `_sections.md` - Section metadata (titles, impacts, descriptions)
|
|
9
|
-
- `_template.md` - Template for creating new rules
|
|
10
|
-
- `area-description.md` - Individual rule files
|
|
11
|
-
- `src/` - Build scripts and utilities
|
|
12
|
-
- `metadata.json` - Document metadata (version, organization, abstract)
|
|
13
|
-
- __`AGENTS.md`__ - Compiled output (generated)
|
|
14
|
-
- __`test-cases.json`__ - Test cases for LLM evaluation (generated)
|
|
15
|
-
|
|
16
|
-
## Getting Started
|
|
17
|
-
|
|
18
|
-
1. Install dependencies:
|
|
19
|
-
```bash
|
|
20
|
-
pnpm install
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
2. Build AGENTS.md from rules:
|
|
24
|
-
```bash
|
|
25
|
-
pnpm build
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
3. Validate rule files:
|
|
29
|
-
```bash
|
|
30
|
-
pnpm validate
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
4. Extract test cases:
|
|
34
|
-
```bash
|
|
35
|
-
pnpm extract-tests
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
## Creating a New Rule
|
|
39
|
-
|
|
40
|
-
1. Copy `rules/_template.md` to `rules/area-description.md`
|
|
41
|
-
2. Choose the appropriate area prefix:
|
|
42
|
-
- `async-` for Eliminating Waterfalls (Section 1)
|
|
43
|
-
- `bundle-` for Bundle Size Optimization (Section 2)
|
|
44
|
-
- `server-` for Server-Side Performance (Section 3)
|
|
45
|
-
- `client-` for Client-Side Data Fetching (Section 4)
|
|
46
|
-
- `rerender-` for Re-render Optimization (Section 5)
|
|
47
|
-
- `rendering-` for Rendering Performance (Section 6)
|
|
48
|
-
- `js-` for JavaScript Performance (Section 7)
|
|
49
|
-
- `advanced-` for Advanced Patterns (Section 8)
|
|
50
|
-
3. Fill in the frontmatter and content
|
|
51
|
-
4. Ensure you have clear examples with explanations
|
|
52
|
-
5. Run `pnpm build` to regenerate AGENTS.md and test-cases.json
|
|
53
|
-
|
|
54
|
-
## Rule File Structure
|
|
55
|
-
|
|
56
|
-
Each rule file should follow this structure:
|
|
57
|
-
|
|
58
|
-
```markdown
|
|
59
|
-
---
|
|
60
|
-
title: Rule Title Here
|
|
61
|
-
impact: MEDIUM
|
|
62
|
-
impactDescription: Optional description
|
|
63
|
-
tags: tag1, tag2, tag3
|
|
64
|
-
---
|
|
65
|
-
|
|
66
|
-
## Rule Title Here
|
|
67
|
-
|
|
68
|
-
Brief explanation of the rule and why it matters.
|
|
69
|
-
|
|
70
|
-
**Incorrect (description of what's wrong):**
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
// Bad code example
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
**Correct (description of what's right):**
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
// Good code example
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
Optional explanatory text after examples.
|
|
83
|
-
|
|
84
|
-
Reference: [Link](https://example.com)
|
|
85
|
-
|
|
86
|
-
## File Naming Convention
|
|
87
|
-
|
|
88
|
-
- Files starting with `_` are special (excluded from build)
|
|
89
|
-
- Rule files: `area-description.md` (e.g., `async-parallel.md`)
|
|
90
|
-
- Section is automatically inferred from filename prefix
|
|
91
|
-
- Rules are sorted alphabetically by title within each section
|
|
92
|
-
- IDs (e.g., 1.1, 1.2) are auto-generated during build
|
|
93
|
-
|
|
94
|
-
## Impact Levels
|
|
95
|
-
|
|
96
|
-
- `CRITICAL` - Highest priority, major performance gains
|
|
97
|
-
- `HIGH` - Significant performance improvements
|
|
98
|
-
- `MEDIUM-HIGH` - Moderate-high gains
|
|
99
|
-
- `MEDIUM` - Moderate performance improvements
|
|
100
|
-
- `LOW-MEDIUM` - Low-medium gains
|
|
101
|
-
- `LOW` - Incremental improvements
|
|
102
|
-
|
|
103
|
-
## Scripts
|
|
104
|
-
|
|
105
|
-
- `pnpm build` - Compile rules into AGENTS.md
|
|
106
|
-
- `pnpm validate` - Validate all rule files
|
|
107
|
-
- `pnpm extract-tests` - Extract test cases for LLM evaluation
|
|
108
|
-
- `pnpm dev` - Build and validate
|
|
109
|
-
|
|
110
|
-
## Contributing
|
|
111
|
-
|
|
112
|
-
When adding or modifying rules:
|
|
113
|
-
|
|
114
|
-
1. Use the correct filename prefix for your section
|
|
115
|
-
2. Follow the `_template.md` structure
|
|
116
|
-
3. Include clear bad/good examples with explanations
|
|
117
|
-
4. Add appropriate tags
|
|
118
|
-
5. Run `pnpm build` to regenerate AGENTS.md and test-cases.json
|
|
119
|
-
6. Rules are automatically sorted by title - no need to manage numbers!
|
|
120
|
-
|
|
121
|
-
## Acknowledgments
|
|
122
|
-
|
|
123
|
-
Originally created by [@shuding](https://x.com/shuding) at [Vercel](https://vercel.com).
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: react-best-practices
|
|
3
|
-
description: React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.
|
|
4
|
-
license: MIT
|
|
5
|
-
metadata:
|
|
6
|
-
author: vercel
|
|
7
|
-
version: "1.0.0"
|
|
8
|
-
---
|
|
9
|
-
|
|
10
|
-
# Vercel React Best Practices
|
|
11
|
-
|
|
12
|
-
Comprehensive performance optimization guide for React and Next.js applications, maintained by Vercel. Contains 45 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
|
|
13
|
-
|
|
14
|
-
## When to Apply
|
|
15
|
-
|
|
16
|
-
Reference these guidelines when:
|
|
17
|
-
- Writing new React components or Next.js pages
|
|
18
|
-
- Implementing data fetching (client or server-side)
|
|
19
|
-
- Reviewing code for performance issues
|
|
20
|
-
- Refactoring existing React/Next.js code
|
|
21
|
-
- Optimizing bundle size or load times
|
|
22
|
-
|
|
23
|
-
## Metadata
|
|
24
|
-
|
|
25
|
-
**Trigger Phrases:**
|
|
26
|
-
- "review this React component"
|
|
27
|
-
- "optimize this component"
|
|
28
|
-
- "React performance review"
|
|
29
|
-
- "check for React anti-patterns"
|
|
30
|
-
- "apply React best practices"
|
|
31
|
-
- "React code review"
|
|
32
|
-
|
|
33
|
-
## Rule Categories by Priority
|
|
34
|
-
|
|
35
|
-
| Priority | Category | Impact | Prefix |
|
|
36
|
-
|----------|----------|--------|--------|
|
|
37
|
-
| 1 | Eliminating Waterfalls | CRITICAL | `async-` |
|
|
38
|
-
| 2 | Bundle Size Optimization | CRITICAL | `bundle-` |
|
|
39
|
-
| 3 | Server-Side Performance | HIGH | `server-` |
|
|
40
|
-
| 4 | Client-Side Data Fetching | MEDIUM-HIGH | `client-` |
|
|
41
|
-
| 5 | Re-render Optimization | MEDIUM | `rerender-` |
|
|
42
|
-
| 6 | Rendering Performance | MEDIUM | `rendering-` |
|
|
43
|
-
| 7 | JavaScript Performance | LOW-MEDIUM | `js-` |
|
|
44
|
-
| 8 | Advanced Patterns | LOW | `advanced-` |
|
|
45
|
-
|
|
46
|
-
## Quick Reference
|
|
47
|
-
|
|
48
|
-
### 1. Eliminating Waterfalls (CRITICAL)
|
|
49
|
-
|
|
50
|
-
- `async-defer-await` - Move await into branches where actually used
|
|
51
|
-
- `async-parallel` - Use Promise.all() for independent operations
|
|
52
|
-
- `async-dependencies` - Use better-all for partial dependencies
|
|
53
|
-
- `async-api-routes` - Start promises early, await late in API routes
|
|
54
|
-
- `async-suspense-boundaries` - Use Suspense to stream content
|
|
55
|
-
|
|
56
|
-
### 2. Bundle Size Optimization (CRITICAL)
|
|
57
|
-
|
|
58
|
-
- `bundle-barrel-imports` - Import directly, avoid barrel files
|
|
59
|
-
- `bundle-dynamic-imports` - Use next/dynamic for heavy components
|
|
60
|
-
- `bundle-defer-third-party` - Load analytics/logging after hydration
|
|
61
|
-
- `bundle-conditional` - Load modules only when feature is activated
|
|
62
|
-
- `bundle-preload` - Preload on hover/focus for perceived speed
|
|
63
|
-
|
|
64
|
-
### 3. Server-Side Performance (HIGH)
|
|
65
|
-
|
|
66
|
-
- `server-cache-react` - Use React.cache() for per-request deduplication
|
|
67
|
-
- `server-cache-lru` - Use LRU cache for cross-request caching
|
|
68
|
-
- `server-serialization` - Minimize data passed to client components
|
|
69
|
-
- `server-parallel-fetching` - Restructure components to parallelize fetches
|
|
70
|
-
- `server-after-nonblocking` - Use after() for non-blocking operations
|
|
71
|
-
|
|
72
|
-
### 4. Client-Side Data Fetching (MEDIUM-HIGH)
|
|
73
|
-
|
|
74
|
-
- `client-swr-dedup` - Use SWR for automatic request deduplication
|
|
75
|
-
- `client-event-listeners` - Deduplicate global event listeners
|
|
76
|
-
|
|
77
|
-
### 5. Re-render Optimization (MEDIUM)
|
|
78
|
-
|
|
79
|
-
- `rerender-defer-reads` - Don't subscribe to state only used in callbacks
|
|
80
|
-
- `rerender-memo` - Extract expensive work into memoized components
|
|
81
|
-
- `rerender-dependencies` - Use primitive dependencies in effects
|
|
82
|
-
- `rerender-derived-state` - Subscribe to derived booleans, not raw values
|
|
83
|
-
- `rerender-functional-setstate` - Use functional setState for stable callbacks
|
|
84
|
-
- `rerender-lazy-state-init` - Pass function to useState for expensive values
|
|
85
|
-
- `rerender-transitions` - Use startTransition for non-urgent updates
|
|
86
|
-
|
|
87
|
-
### 6. Rendering Performance (MEDIUM)
|
|
88
|
-
|
|
89
|
-
- `rendering-animate-svg-wrapper` - Animate div wrapper, not SVG element
|
|
90
|
-
- `rendering-content-visibility` - Use content-visibility for long lists
|
|
91
|
-
- `rendering-hoist-jsx` - Extract static JSX outside components
|
|
92
|
-
- `rendering-svg-precision` - Reduce SVG coordinate precision
|
|
93
|
-
- `rendering-hydration-no-flicker` - Use inline script for client-only data
|
|
94
|
-
- `rendering-activity` - Use Activity component for show/hide
|
|
95
|
-
- `rendering-conditional-render` - Use ternary, not && for conditionals
|
|
96
|
-
|
|
97
|
-
### 7. JavaScript Performance (LOW-MEDIUM)
|
|
98
|
-
|
|
99
|
-
- `js-batch-dom-css` - Group CSS changes via classes or cssText
|
|
100
|
-
- `js-index-maps` - Build Map for repeated lookups
|
|
101
|
-
- `js-cache-property-access` - Cache object properties in loops
|
|
102
|
-
- `js-cache-function-results` - Cache function results in module-level Map
|
|
103
|
-
- `js-cache-storage` - Cache localStorage/sessionStorage reads
|
|
104
|
-
- `js-combine-iterations` - Combine multiple filter/map into one loop
|
|
105
|
-
- `js-length-check-first` - Check array length before expensive comparison
|
|
106
|
-
- `js-early-exit` - Return early from functions
|
|
107
|
-
- `js-hoist-regexp` - Hoist RegExp creation outside loops
|
|
108
|
-
- `js-min-max-loop` - Use loop for min/max instead of sort
|
|
109
|
-
- `js-set-map-lookups` - Use Set/Map for O(1) lookups
|
|
110
|
-
- `js-tosorted-immutable` - Use toSorted() for immutability
|
|
111
|
-
|
|
112
|
-
### 8. Advanced Patterns (LOW)
|
|
113
|
-
|
|
114
|
-
- `advanced-event-handler-refs` - Store event handlers in refs
|
|
115
|
-
- `advanced-use-latest` - useLatest for stable callback refs
|
|
116
|
-
|
|
117
|
-
## How to Use
|
|
118
|
-
|
|
119
|
-
Read individual rule files for detailed explanations and code examples:
|
|
120
|
-
|
|
121
|
-
```
|
|
122
|
-
rules/async-parallel.md
|
|
123
|
-
rules/bundle-barrel-imports.md
|
|
124
|
-
rules/_sections.md
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
Each rule file contains:
|
|
128
|
-
- Brief explanation of why it matters
|
|
129
|
-
- Incorrect code example with explanation
|
|
130
|
-
- Correct code example with explanation
|
|
131
|
-
- Additional context and references
|
|
132
|
-
|
|
133
|
-
## Full Compiled Document
|
|
134
|
-
|
|
135
|
-
For the complete guide with all rules expanded: `AGENTS.md`
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": "1.0.0",
|
|
3
|
-
"organization": "Vercel Engineering",
|
|
4
|
-
"date": "January 2026",
|
|
5
|
-
"abstract": "Comprehensive performance optimization guide for React and Next.js applications, designed for AI agents and LLMs. Contains 40+ rules across 8 categories, prioritized by impact from critical (eliminating waterfalls, reducing bundle size) to incremental (advanced patterns). Each rule includes detailed explanations, real-world examples comparing incorrect vs. correct implementations, and specific impact metrics to guide automated refactoring and code generation.",
|
|
6
|
-
"references": [
|
|
7
|
-
"https://react.dev",
|
|
8
|
-
"https://nextjs.org",
|
|
9
|
-
"https://swr.vercel.app",
|
|
10
|
-
"https://github.com/shuding/better-all",
|
|
11
|
-
"https://github.com/isaacs/node-lru-cache",
|
|
12
|
-
"https://vercel.com/blog/how-we-optimized-package-imports-in-next-js",
|
|
13
|
-
"https://vercel.com/blog/how-we-made-the-vercel-dashboard-twice-as-fast"
|
|
14
|
-
]
|
|
15
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# Sections
|
|
2
|
-
|
|
3
|
-
This file defines all sections, their ordering, impact levels, and descriptions.
|
|
4
|
-
The section ID (in parentheses) is the filename prefix used to group rules.
|
|
5
|
-
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## 1. Eliminating Waterfalls (async)
|
|
9
|
-
|
|
10
|
-
**Impact:** CRITICAL
|
|
11
|
-
**Description:** Waterfalls are the #1 performance killer. Each sequential await adds full network latency. Eliminating them yields the largest gains.
|
|
12
|
-
|
|
13
|
-
## 2. Bundle Size Optimization (bundle)
|
|
14
|
-
|
|
15
|
-
**Impact:** CRITICAL
|
|
16
|
-
**Description:** Reducing initial bundle size improves Time to Interactive and Largest Contentful Paint.
|
|
17
|
-
|
|
18
|
-
## 3. Server-Side Performance (server)
|
|
19
|
-
|
|
20
|
-
**Impact:** HIGH
|
|
21
|
-
**Description:** Optimizing server-side rendering and data fetching eliminates server-side waterfalls and reduces response times.
|
|
22
|
-
|
|
23
|
-
## 4. Client-Side Data Fetching (client)
|
|
24
|
-
|
|
25
|
-
**Impact:** MEDIUM-HIGH
|
|
26
|
-
**Description:** Automatic deduplication and efficient data fetching patterns reduce redundant network requests.
|
|
27
|
-
|
|
28
|
-
## 5. Re-render Optimization (rerender)
|
|
29
|
-
|
|
30
|
-
**Impact:** MEDIUM
|
|
31
|
-
**Description:** Reducing unnecessary re-renders minimizes wasted computation and improves UI responsiveness.
|
|
32
|
-
|
|
33
|
-
## 6. Rendering Performance (rendering)
|
|
34
|
-
|
|
35
|
-
**Impact:** MEDIUM
|
|
36
|
-
**Description:** Optimizing the rendering process reduces the work the browser needs to do.
|
|
37
|
-
|
|
38
|
-
## 7. JavaScript Performance (js)
|
|
39
|
-
|
|
40
|
-
**Impact:** LOW-MEDIUM
|
|
41
|
-
**Description:** Micro-optimizations for hot paths can add up to meaningful improvements.
|
|
42
|
-
|
|
43
|
-
## 8. Advanced Patterns (advanced)
|
|
44
|
-
|
|
45
|
-
**Impact:** LOW
|
|
46
|
-
**Description:** Advanced patterns for specific cases that require careful implementation.
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Rule Title Here
|
|
3
|
-
impact: MEDIUM
|
|
4
|
-
impactDescription: Optional description of impact (e.g., "20-50% improvement")
|
|
5
|
-
tags: tag1, tag2
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Rule Title Here
|
|
9
|
-
|
|
10
|
-
**Impact: MEDIUM (optional impact description)**
|
|
11
|
-
|
|
12
|
-
Brief explanation of the rule and why it matters. This should be clear and concise, explaining the performance implications.
|
|
13
|
-
|
|
14
|
-
**Incorrect (description of what's wrong):**
|
|
15
|
-
|
|
16
|
-
```typescript
|
|
17
|
-
// Bad code example here
|
|
18
|
-
const bad = example()
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
**Correct (description of what's right):**
|
|
22
|
-
|
|
23
|
-
```typescript
|
|
24
|
-
// Good code example here
|
|
25
|
-
const good = example()
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
Reference: [Link to documentation or resource](https://example.com)
|
package/templates/base/.claude/skills/react-best-practices/rules/advanced-event-handler-refs.md
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Store Event Handlers in Refs
|
|
3
|
-
impact: LOW
|
|
4
|
-
impactDescription: stable subscriptions
|
|
5
|
-
tags: advanced, hooks, refs, event-handlers, optimization
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Store Event Handlers in Refs
|
|
9
|
-
|
|
10
|
-
Store callbacks in refs when used in effects that shouldn't re-subscribe on callback changes.
|
|
11
|
-
|
|
12
|
-
**Incorrect (re-subscribes on every render):**
|
|
13
|
-
|
|
14
|
-
```tsx
|
|
15
|
-
function useWindowEvent(event: string, handler: (e) => void) {
|
|
16
|
-
useEffect(() => {
|
|
17
|
-
window.addEventListener(event, handler)
|
|
18
|
-
return () => window.removeEventListener(event, handler)
|
|
19
|
-
}, [event, handler])
|
|
20
|
-
}
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
**Correct (stable subscription):**
|
|
24
|
-
|
|
25
|
-
```tsx
|
|
26
|
-
function useWindowEvent(event: string, handler: (e) => void) {
|
|
27
|
-
const handlerRef = useRef(handler)
|
|
28
|
-
useEffect(() => {
|
|
29
|
-
handlerRef.current = handler
|
|
30
|
-
}, [handler])
|
|
31
|
-
|
|
32
|
-
useEffect(() => {
|
|
33
|
-
const listener = (e) => handlerRef.current(e)
|
|
34
|
-
window.addEventListener(event, listener)
|
|
35
|
-
return () => window.removeEventListener(event, listener)
|
|
36
|
-
}, [event])
|
|
37
|
-
}
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
**Alternative: use `useEffectEvent` if you're on latest React:**
|
|
41
|
-
|
|
42
|
-
```tsx
|
|
43
|
-
import { useEffectEvent } from 'react'
|
|
44
|
-
|
|
45
|
-
function useWindowEvent(event: string, handler: (e) => void) {
|
|
46
|
-
const onEvent = useEffectEvent(handler)
|
|
47
|
-
|
|
48
|
-
useEffect(() => {
|
|
49
|
-
window.addEventListener(event, onEvent)
|
|
50
|
-
return () => window.removeEventListener(event, onEvent)
|
|
51
|
-
}, [event])
|
|
52
|
-
}
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
`useEffectEvent` provides a cleaner API for the same pattern: it creates a stable function reference that always calls the latest version of the handler.
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: useLatest for Stable Callback Refs
|
|
3
|
-
impact: LOW
|
|
4
|
-
impactDescription: prevents effect re-runs
|
|
5
|
-
tags: advanced, hooks, useLatest, refs, optimization
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## useLatest for Stable Callback Refs
|
|
9
|
-
|
|
10
|
-
Access latest values in callbacks without adding them to dependency arrays. Prevents effect re-runs while avoiding stale closures.
|
|
11
|
-
|
|
12
|
-
**Implementation:**
|
|
13
|
-
|
|
14
|
-
```typescript
|
|
15
|
-
function useLatest<T>(value: T) {
|
|
16
|
-
const ref = useRef(value)
|
|
17
|
-
useLayoutEffect(() => {
|
|
18
|
-
ref.current = value
|
|
19
|
-
}, [value])
|
|
20
|
-
return ref
|
|
21
|
-
}
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
**Incorrect (effect re-runs on every callback change):**
|
|
25
|
-
|
|
26
|
-
```tsx
|
|
27
|
-
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
|
|
28
|
-
const [query, setQuery] = useState('')
|
|
29
|
-
|
|
30
|
-
useEffect(() => {
|
|
31
|
-
const timeout = setTimeout(() => onSearch(query), 300)
|
|
32
|
-
return () => clearTimeout(timeout)
|
|
33
|
-
}, [query, onSearch])
|
|
34
|
-
}
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
**Correct (stable effect, fresh callback):**
|
|
38
|
-
|
|
39
|
-
```tsx
|
|
40
|
-
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
|
|
41
|
-
const [query, setQuery] = useState('')
|
|
42
|
-
const onSearchRef = useLatest(onSearch)
|
|
43
|
-
|
|
44
|
-
useEffect(() => {
|
|
45
|
-
const timeout = setTimeout(() => onSearchRef.current(query), 300)
|
|
46
|
-
return () => clearTimeout(timeout)
|
|
47
|
-
}, [query])
|
|
48
|
-
}
|
|
49
|
-
```
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Prevent Waterfall Chains in API Routes
|
|
3
|
-
impact: CRITICAL
|
|
4
|
-
impactDescription: 2-10× improvement
|
|
5
|
-
tags: api-routes, server-actions, waterfalls, parallelization
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Prevent Waterfall Chains in API Routes
|
|
9
|
-
|
|
10
|
-
In API routes and Server Actions, start independent operations immediately, even if you don't await them yet.
|
|
11
|
-
|
|
12
|
-
**Incorrect (config waits for auth, data waits for both):**
|
|
13
|
-
|
|
14
|
-
```typescript
|
|
15
|
-
export async function GET(request: Request) {
|
|
16
|
-
const session = await auth()
|
|
17
|
-
const config = await fetchConfig()
|
|
18
|
-
const data = await fetchData(session.user.id)
|
|
19
|
-
return Response.json({ data, config })
|
|
20
|
-
}
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
**Correct (auth and config start immediately):**
|
|
24
|
-
|
|
25
|
-
```typescript
|
|
26
|
-
export async function GET(request: Request) {
|
|
27
|
-
const sessionPromise = auth()
|
|
28
|
-
const configPromise = fetchConfig()
|
|
29
|
-
const session = await sessionPromise
|
|
30
|
-
const [config, data] = await Promise.all([
|
|
31
|
-
configPromise,
|
|
32
|
-
fetchData(session.user.id)
|
|
33
|
-
])
|
|
34
|
-
return Response.json({ data, config })
|
|
35
|
-
}
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
For operations with more complex dependency chains, use `better-all` to automatically maximize parallelism (see Dependency-Based Parallelization).
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Defer Await Until Needed
|
|
3
|
-
impact: HIGH
|
|
4
|
-
impactDescription: avoids blocking unused code paths
|
|
5
|
-
tags: async, await, conditional, optimization
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Defer Await Until Needed
|
|
9
|
-
|
|
10
|
-
Move `await` operations into the branches where they're actually used to avoid blocking code paths that don't need them.
|
|
11
|
-
|
|
12
|
-
**Incorrect (blocks both branches):**
|
|
13
|
-
|
|
14
|
-
```typescript
|
|
15
|
-
async function handleRequest(userId: string, skipProcessing: boolean) {
|
|
16
|
-
const userData = await fetchUserData(userId)
|
|
17
|
-
|
|
18
|
-
if (skipProcessing) {
|
|
19
|
-
// Returns immediately but still waited for userData
|
|
20
|
-
return { skipped: true }
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Only this branch uses userData
|
|
24
|
-
return processUserData(userData)
|
|
25
|
-
}
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
**Correct (only blocks when needed):**
|
|
29
|
-
|
|
30
|
-
```typescript
|
|
31
|
-
async function handleRequest(userId: string, skipProcessing: boolean) {
|
|
32
|
-
if (skipProcessing) {
|
|
33
|
-
// Returns immediately without waiting
|
|
34
|
-
return { skipped: true }
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Fetch only when needed
|
|
38
|
-
const userData = await fetchUserData(userId)
|
|
39
|
-
return processUserData(userData)
|
|
40
|
-
}
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
**Another example (early return optimization):**
|
|
44
|
-
|
|
45
|
-
```typescript
|
|
46
|
-
// Incorrect: always fetches permissions
|
|
47
|
-
async function updateResource(resourceId: string, userId: string) {
|
|
48
|
-
const permissions = await fetchPermissions(userId)
|
|
49
|
-
const resource = await getResource(resourceId)
|
|
50
|
-
|
|
51
|
-
if (!resource) {
|
|
52
|
-
return { error: 'Not found' }
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (!permissions.canEdit) {
|
|
56
|
-
return { error: 'Forbidden' }
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return await updateResourceData(resource, permissions)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Correct: fetches only when needed
|
|
63
|
-
async function updateResource(resourceId: string, userId: string) {
|
|
64
|
-
const resource = await getResource(resourceId)
|
|
65
|
-
|
|
66
|
-
if (!resource) {
|
|
67
|
-
return { error: 'Not found' }
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const permissions = await fetchPermissions(userId)
|
|
71
|
-
|
|
72
|
-
if (!permissions.canEdit) {
|
|
73
|
-
return { error: 'Forbidden' }
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return await updateResourceData(resource, permissions)
|
|
77
|
-
}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
This optimization is especially valuable when the skipped branch is frequently taken, or when the deferred operation is expensive.
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Dependency-Based Parallelization
|
|
3
|
-
impact: CRITICAL
|
|
4
|
-
impactDescription: 2-10× improvement
|
|
5
|
-
tags: async, parallelization, dependencies, better-all
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Dependency-Based Parallelization
|
|
9
|
-
|
|
10
|
-
For operations with partial dependencies, use `better-all` to maximize parallelism. It automatically starts each task at the earliest possible moment.
|
|
11
|
-
|
|
12
|
-
**Incorrect (profile waits for config unnecessarily):**
|
|
13
|
-
|
|
14
|
-
```typescript
|
|
15
|
-
const [user, config] = await Promise.all([
|
|
16
|
-
fetchUser(),
|
|
17
|
-
fetchConfig()
|
|
18
|
-
])
|
|
19
|
-
const profile = await fetchProfile(user.id)
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
**Correct (config and profile run in parallel):**
|
|
23
|
-
|
|
24
|
-
```typescript
|
|
25
|
-
import { all } from 'better-all'
|
|
26
|
-
|
|
27
|
-
const { user, config, profile } = await all({
|
|
28
|
-
async user() { return fetchUser() },
|
|
29
|
-
async config() { return fetchConfig() },
|
|
30
|
-
async profile() {
|
|
31
|
-
return fetchProfile((await this.$.user).id)
|
|
32
|
-
}
|
|
33
|
-
})
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
Reference: [https://github.com/shuding/better-all](https://github.com/shuding/better-all)
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Promise.all() for Independent Operations
|
|
3
|
-
impact: CRITICAL
|
|
4
|
-
impactDescription: 2-10× improvement
|
|
5
|
-
tags: async, parallelization, promises, waterfalls
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Promise.all() for Independent Operations
|
|
9
|
-
|
|
10
|
-
When async operations have no interdependencies, execute them concurrently using `Promise.all()`.
|
|
11
|
-
|
|
12
|
-
**Incorrect (sequential execution, 3 round trips):**
|
|
13
|
-
|
|
14
|
-
```typescript
|
|
15
|
-
const user = await fetchUser()
|
|
16
|
-
const posts = await fetchPosts()
|
|
17
|
-
const comments = await fetchComments()
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
**Correct (parallel execution, 1 round trip):**
|
|
21
|
-
|
|
22
|
-
```typescript
|
|
23
|
-
const [user, posts, comments] = await Promise.all([
|
|
24
|
-
fetchUser(),
|
|
25
|
-
fetchPosts(),
|
|
26
|
-
fetchComments()
|
|
27
|
-
])
|
|
28
|
-
```
|