@0xsown/vibe-code-fe 1.0.0
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/bin/index.js +181 -0
- package/package.json +32 -0
- package/skills/claude-md-improver/SKILL.md +179 -0
- package/skills/claude-md-improver/references/quality-criteria.md +109 -0
- package/skills/claude-md-improver/references/templates.md +253 -0
- package/skills/claude-md-improver/references/update-guidelines.md +150 -0
- package/skills/find-skills/SKILL.md +133 -0
- package/skills/frontend-design/LICENSE.txt +177 -0
- package/skills/frontend-design/SKILL.md +42 -0
- package/skills/next-best-practices/SKILL.md +153 -0
- package/skills/next-best-practices/async-patterns.md +87 -0
- package/skills/next-best-practices/bundling.md +180 -0
- package/skills/next-best-practices/data-patterns.md +297 -0
- package/skills/next-best-practices/debug-tricks.md +105 -0
- package/skills/next-best-practices/directives.md +73 -0
- package/skills/next-best-practices/error-handling.md +227 -0
- package/skills/next-best-practices/file-conventions.md +140 -0
- package/skills/next-best-practices/font.md +245 -0
- package/skills/next-best-practices/functions.md +108 -0
- package/skills/next-best-practices/hydration-error.md +91 -0
- package/skills/next-best-practices/image.md +173 -0
- package/skills/next-best-practices/metadata.md +301 -0
- package/skills/next-best-practices/parallel-routes.md +287 -0
- package/skills/next-best-practices/route-handlers.md +146 -0
- package/skills/next-best-practices/rsc-boundaries.md +159 -0
- package/skills/next-best-practices/runtime-selection.md +39 -0
- package/skills/next-best-practices/scripts.md +141 -0
- package/skills/next-best-practices/self-hosting.md +371 -0
- package/skills/next-best-practices/suspense-boundaries.md +67 -0
- package/skills/next-cache-components/SKILL.md +411 -0
- package/skills/shadcn-ui/README.md +248 -0
- package/skills/shadcn-ui/SKILL.md +326 -0
- package/skills/shadcn-ui/examples/auth-layout.tsx +177 -0
- package/skills/shadcn-ui/examples/data-table.tsx +313 -0
- package/skills/shadcn-ui/examples/form-pattern.tsx +177 -0
- package/skills/shadcn-ui/resources/component-catalog.md +481 -0
- package/skills/shadcn-ui/resources/customization-guide.md +516 -0
- package/skills/shadcn-ui/resources/migration-guide.md +463 -0
- package/skills/shadcn-ui/resources/setup-guide.md +412 -0
- package/skills/shadcn-ui/scripts/verify-setup.sh +134 -0
- package/skills/supabase-postgres-best-practices/AGENTS.md +68 -0
- package/skills/supabase-postgres-best-practices/CLAUDE.md +68 -0
- package/skills/supabase-postgres-best-practices/README.md +116 -0
- package/skills/supabase-postgres-best-practices/SKILL.md +64 -0
- package/skills/supabase-postgres-best-practices/references/advanced-full-text-search.md +55 -0
- package/skills/supabase-postgres-best-practices/references/advanced-jsonb-indexing.md +49 -0
- package/skills/supabase-postgres-best-practices/references/conn-idle-timeout.md +46 -0
- package/skills/supabase-postgres-best-practices/references/conn-limits.md +44 -0
- package/skills/supabase-postgres-best-practices/references/conn-pooling.md +41 -0
- package/skills/supabase-postgres-best-practices/references/conn-prepared-statements.md +46 -0
- package/skills/supabase-postgres-best-practices/references/data-batch-inserts.md +54 -0
- package/skills/supabase-postgres-best-practices/references/data-n-plus-one.md +53 -0
- package/skills/supabase-postgres-best-practices/references/data-pagination.md +50 -0
- package/skills/supabase-postgres-best-practices/references/data-upsert.md +50 -0
- package/skills/supabase-postgres-best-practices/references/lock-advisory.md +56 -0
- package/skills/supabase-postgres-best-practices/references/lock-deadlock-prevention.md +68 -0
- package/skills/supabase-postgres-best-practices/references/lock-short-transactions.md +50 -0
- package/skills/supabase-postgres-best-practices/references/lock-skip-locked.md +54 -0
- package/skills/supabase-postgres-best-practices/references/monitor-explain-analyze.md +45 -0
- package/skills/supabase-postgres-best-practices/references/monitor-pg-stat-statements.md +55 -0
- package/skills/supabase-postgres-best-practices/references/monitor-vacuum-analyze.md +55 -0
- package/skills/supabase-postgres-best-practices/references/query-composite-indexes.md +44 -0
- package/skills/supabase-postgres-best-practices/references/query-covering-indexes.md +40 -0
- package/skills/supabase-postgres-best-practices/references/query-index-types.md +48 -0
- package/skills/supabase-postgres-best-practices/references/query-missing-indexes.md +43 -0
- package/skills/supabase-postgres-best-practices/references/query-partial-indexes.md +45 -0
- package/skills/supabase-postgres-best-practices/references/schema-constraints.md +80 -0
- package/skills/supabase-postgres-best-practices/references/schema-data-types.md +46 -0
- package/skills/supabase-postgres-best-practices/references/schema-foreign-key-indexes.md +59 -0
- package/skills/supabase-postgres-best-practices/references/schema-lowercase-identifiers.md +55 -0
- package/skills/supabase-postgres-best-practices/references/schema-partitioning.md +55 -0
- package/skills/supabase-postgres-best-practices/references/schema-primary-keys.md +61 -0
- package/skills/supabase-postgres-best-practices/references/security-privileges.md +54 -0
- package/skills/supabase-postgres-best-practices/references/security-rls-basics.md +50 -0
- package/skills/supabase-postgres-best-practices/references/security-rls-performance.md +57 -0
- package/skills/tailwind-design-system/SKILL.md +874 -0
- package/skills/vercel-composition-patterns/AGENTS.md +946 -0
- package/skills/vercel-composition-patterns/README.md +60 -0
- package/skills/vercel-composition-patterns/SKILL.md +89 -0
- package/skills/vercel-composition-patterns/rules/architecture-avoid-boolean-props.md +100 -0
- package/skills/vercel-composition-patterns/rules/architecture-compound-components.md +112 -0
- package/skills/vercel-composition-patterns/rules/patterns-children-over-render-props.md +87 -0
- package/skills/vercel-composition-patterns/rules/patterns-explicit-variants.md +100 -0
- package/skills/vercel-composition-patterns/rules/react19-no-forwardref.md +42 -0
- package/skills/vercel-composition-patterns/rules/state-context-interface.md +191 -0
- package/skills/vercel-composition-patterns/rules/state-decouple-implementation.md +113 -0
- package/skills/vercel-composition-patterns/rules/state-lift-state.md +125 -0
- package/skills/vercel-react-best-practices/AGENTS.md +2934 -0
- package/skills/vercel-react-best-practices/README.md +123 -0
- package/skills/vercel-react-best-practices/SKILL.md +136 -0
- package/skills/vercel-react-best-practices/rules/advanced-event-handler-refs.md +55 -0
- package/skills/vercel-react-best-practices/rules/advanced-init-once.md +42 -0
- package/skills/vercel-react-best-practices/rules/advanced-use-latest.md +39 -0
- package/skills/vercel-react-best-practices/rules/async-api-routes.md +38 -0
- package/skills/vercel-react-best-practices/rules/async-defer-await.md +80 -0
- package/skills/vercel-react-best-practices/rules/async-dependencies.md +51 -0
- package/skills/vercel-react-best-practices/rules/async-parallel.md +28 -0
- package/skills/vercel-react-best-practices/rules/async-suspense-boundaries.md +99 -0
- package/skills/vercel-react-best-practices/rules/bundle-barrel-imports.md +59 -0
- package/skills/vercel-react-best-practices/rules/bundle-conditional.md +31 -0
- package/skills/vercel-react-best-practices/rules/bundle-defer-third-party.md +49 -0
- package/skills/vercel-react-best-practices/rules/bundle-dynamic-imports.md +35 -0
- package/skills/vercel-react-best-practices/rules/bundle-preload.md +50 -0
- package/skills/vercel-react-best-practices/rules/client-event-listeners.md +74 -0
- package/skills/vercel-react-best-practices/rules/client-localstorage-schema.md +71 -0
- package/skills/vercel-react-best-practices/rules/client-passive-event-listeners.md +48 -0
- package/skills/vercel-react-best-practices/rules/client-swr-dedup.md +56 -0
- package/skills/vercel-react-best-practices/rules/js-batch-dom-css.md +107 -0
- package/skills/vercel-react-best-practices/rules/js-cache-function-results.md +80 -0
- package/skills/vercel-react-best-practices/rules/js-cache-property-access.md +28 -0
- package/skills/vercel-react-best-practices/rules/js-cache-storage.md +70 -0
- package/skills/vercel-react-best-practices/rules/js-combine-iterations.md +32 -0
- package/skills/vercel-react-best-practices/rules/js-early-exit.md +50 -0
- package/skills/vercel-react-best-practices/rules/js-hoist-regexp.md +45 -0
- package/skills/vercel-react-best-practices/rules/js-index-maps.md +37 -0
- package/skills/vercel-react-best-practices/rules/js-length-check-first.md +49 -0
- package/skills/vercel-react-best-practices/rules/js-min-max-loop.md +82 -0
- package/skills/vercel-react-best-practices/rules/js-set-map-lookups.md +24 -0
- package/skills/vercel-react-best-practices/rules/js-tosorted-immutable.md +57 -0
- package/skills/vercel-react-best-practices/rules/rendering-activity.md +26 -0
- package/skills/vercel-react-best-practices/rules/rendering-animate-svg-wrapper.md +47 -0
- package/skills/vercel-react-best-practices/rules/rendering-conditional-render.md +40 -0
- package/skills/vercel-react-best-practices/rules/rendering-content-visibility.md +38 -0
- package/skills/vercel-react-best-practices/rules/rendering-hoist-jsx.md +46 -0
- package/skills/vercel-react-best-practices/rules/rendering-hydration-no-flicker.md +82 -0
- package/skills/vercel-react-best-practices/rules/rendering-hydration-suppress-warning.md +30 -0
- package/skills/vercel-react-best-practices/rules/rendering-svg-precision.md +28 -0
- package/skills/vercel-react-best-practices/rules/rendering-usetransition-loading.md +75 -0
- package/skills/vercel-react-best-practices/rules/rerender-defer-reads.md +39 -0
- package/skills/vercel-react-best-practices/rules/rerender-dependencies.md +45 -0
- package/skills/vercel-react-best-practices/rules/rerender-derived-state-no-effect.md +40 -0
- package/skills/vercel-react-best-practices/rules/rerender-derived-state.md +29 -0
- package/skills/vercel-react-best-practices/rules/rerender-functional-setstate.md +74 -0
- package/skills/vercel-react-best-practices/rules/rerender-lazy-state-init.md +58 -0
- package/skills/vercel-react-best-practices/rules/rerender-memo-with-default-value.md +38 -0
- package/skills/vercel-react-best-practices/rules/rerender-memo.md +44 -0
- package/skills/vercel-react-best-practices/rules/rerender-move-effect-to-event.md +45 -0
- package/skills/vercel-react-best-practices/rules/rerender-simple-expression-in-memo.md +35 -0
- package/skills/vercel-react-best-practices/rules/rerender-transitions.md +40 -0
- package/skills/vercel-react-best-practices/rules/rerender-use-ref-transient-values.md +73 -0
- package/skills/vercel-react-best-practices/rules/server-after-nonblocking.md +73 -0
- package/skills/vercel-react-best-practices/rules/server-auth-actions.md +96 -0
- package/skills/vercel-react-best-practices/rules/server-cache-lru.md +41 -0
- package/skills/vercel-react-best-practices/rules/server-cache-react.md +76 -0
- package/skills/vercel-react-best-practices/rules/server-dedup-props.md +65 -0
- package/skills/vercel-react-best-practices/rules/server-parallel-fetching.md +83 -0
- package/skills/vercel-react-best-practices/rules/server-serialization.md +38 -0
- package/skills/vercel-react-native-skills/AGENTS.md +2897 -0
- package/skills/vercel-react-native-skills/README.md +165 -0
- package/skills/vercel-react-native-skills/SKILL.md +121 -0
- package/skills/vercel-react-native-skills/rules/animation-derived-value.md +53 -0
- package/skills/vercel-react-native-skills/rules/animation-gesture-detector-press.md +95 -0
- package/skills/vercel-react-native-skills/rules/animation-gpu-properties.md +65 -0
- package/skills/vercel-react-native-skills/rules/design-system-compound-components.md +66 -0
- package/skills/vercel-react-native-skills/rules/fonts-config-plugin.md +71 -0
- package/skills/vercel-react-native-skills/rules/imports-design-system-folder.md +68 -0
- package/skills/vercel-react-native-skills/rules/js-hoist-intl.md +61 -0
- package/skills/vercel-react-native-skills/rules/list-performance-callbacks.md +44 -0
- package/skills/vercel-react-native-skills/rules/list-performance-function-references.md +132 -0
- package/skills/vercel-react-native-skills/rules/list-performance-images.md +53 -0
- package/skills/vercel-react-native-skills/rules/list-performance-inline-objects.md +97 -0
- package/skills/vercel-react-native-skills/rules/list-performance-item-expensive.md +94 -0
- package/skills/vercel-react-native-skills/rules/list-performance-item-memo.md +82 -0
- package/skills/vercel-react-native-skills/rules/list-performance-item-types.md +104 -0
- package/skills/vercel-react-native-skills/rules/list-performance-virtualize.md +67 -0
- package/skills/vercel-react-native-skills/rules/monorepo-native-deps-in-app.md +46 -0
- package/skills/vercel-react-native-skills/rules/monorepo-single-dependency-versions.md +63 -0
- package/skills/vercel-react-native-skills/rules/navigation-native-navigators.md +188 -0
- package/skills/vercel-react-native-skills/rules/react-compiler-destructure-functions.md +50 -0
- package/skills/vercel-react-native-skills/rules/react-compiler-reanimated-shared-values.md +48 -0
- package/skills/vercel-react-native-skills/rules/react-state-dispatcher.md +91 -0
- package/skills/vercel-react-native-skills/rules/react-state-fallback.md +56 -0
- package/skills/vercel-react-native-skills/rules/react-state-minimize.md +65 -0
- package/skills/vercel-react-native-skills/rules/rendering-no-falsy-and.md +74 -0
- package/skills/vercel-react-native-skills/rules/rendering-text-in-text-component.md +36 -0
- package/skills/vercel-react-native-skills/rules/scroll-position-no-state.md +82 -0
- package/skills/vercel-react-native-skills/rules/state-ground-truth.md +80 -0
- package/skills/vercel-react-native-skills/rules/ui-expo-image.md +66 -0
- package/skills/vercel-react-native-skills/rules/ui-image-gallery.md +104 -0
- package/skills/vercel-react-native-skills/rules/ui-measure-views.md +78 -0
- package/skills/vercel-react-native-skills/rules/ui-menus.md +174 -0
- package/skills/vercel-react-native-skills/rules/ui-native-modals.md +77 -0
- package/skills/vercel-react-native-skills/rules/ui-pressable.md +61 -0
- package/skills/vercel-react-native-skills/rules/ui-safe-area-scroll.md +65 -0
- package/skills/vercel-react-native-skills/rules/ui-scrollview-content-inset.md +45 -0
- package/skills/vercel-react-native-skills/rules/ui-styling.md +87 -0
- package/skills/web-design-guidelines/SKILL.md +39 -0
- package/templates/AGENTS.md +31 -0
- package/templates/CLAUDE.md +31 -0
|
@@ -0,0 +1,123 @@
|
|
|
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).
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: vercel-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 57 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
|
+
## Rule Categories by Priority
|
|
24
|
+
|
|
25
|
+
| Priority | Category | Impact | Prefix |
|
|
26
|
+
|----------|----------|--------|--------|
|
|
27
|
+
| 1 | Eliminating Waterfalls | CRITICAL | `async-` |
|
|
28
|
+
| 2 | Bundle Size Optimization | CRITICAL | `bundle-` |
|
|
29
|
+
| 3 | Server-Side Performance | HIGH | `server-` |
|
|
30
|
+
| 4 | Client-Side Data Fetching | MEDIUM-HIGH | `client-` |
|
|
31
|
+
| 5 | Re-render Optimization | MEDIUM | `rerender-` |
|
|
32
|
+
| 6 | Rendering Performance | MEDIUM | `rendering-` |
|
|
33
|
+
| 7 | JavaScript Performance | LOW-MEDIUM | `js-` |
|
|
34
|
+
| 8 | Advanced Patterns | LOW | `advanced-` |
|
|
35
|
+
|
|
36
|
+
## Quick Reference
|
|
37
|
+
|
|
38
|
+
### 1. Eliminating Waterfalls (CRITICAL)
|
|
39
|
+
|
|
40
|
+
- `async-defer-await` - Move await into branches where actually used
|
|
41
|
+
- `async-parallel` - Use Promise.all() for independent operations
|
|
42
|
+
- `async-dependencies` - Use better-all for partial dependencies
|
|
43
|
+
- `async-api-routes` - Start promises early, await late in API routes
|
|
44
|
+
- `async-suspense-boundaries` - Use Suspense to stream content
|
|
45
|
+
|
|
46
|
+
### 2. Bundle Size Optimization (CRITICAL)
|
|
47
|
+
|
|
48
|
+
- `bundle-barrel-imports` - Import directly, avoid barrel files
|
|
49
|
+
- `bundle-dynamic-imports` - Use next/dynamic for heavy components
|
|
50
|
+
- `bundle-defer-third-party` - Load analytics/logging after hydration
|
|
51
|
+
- `bundle-conditional` - Load modules only when feature is activated
|
|
52
|
+
- `bundle-preload` - Preload on hover/focus for perceived speed
|
|
53
|
+
|
|
54
|
+
### 3. Server-Side Performance (HIGH)
|
|
55
|
+
|
|
56
|
+
- `server-auth-actions` - Authenticate server actions like API routes
|
|
57
|
+
- `server-cache-react` - Use React.cache() for per-request deduplication
|
|
58
|
+
- `server-cache-lru` - Use LRU cache for cross-request caching
|
|
59
|
+
- `server-dedup-props` - Avoid duplicate serialization in RSC props
|
|
60
|
+
- `server-serialization` - Minimize data passed to client components
|
|
61
|
+
- `server-parallel-fetching` - Restructure components to parallelize fetches
|
|
62
|
+
- `server-after-nonblocking` - Use after() for non-blocking operations
|
|
63
|
+
|
|
64
|
+
### 4. Client-Side Data Fetching (MEDIUM-HIGH)
|
|
65
|
+
|
|
66
|
+
- `client-swr-dedup` - Use SWR for automatic request deduplication
|
|
67
|
+
- `client-event-listeners` - Deduplicate global event listeners
|
|
68
|
+
- `client-passive-event-listeners` - Use passive listeners for scroll
|
|
69
|
+
- `client-localstorage-schema` - Version and minimize localStorage data
|
|
70
|
+
|
|
71
|
+
### 5. Re-render Optimization (MEDIUM)
|
|
72
|
+
|
|
73
|
+
- `rerender-defer-reads` - Don't subscribe to state only used in callbacks
|
|
74
|
+
- `rerender-memo` - Extract expensive work into memoized components
|
|
75
|
+
- `rerender-memo-with-default-value` - Hoist default non-primitive props
|
|
76
|
+
- `rerender-dependencies` - Use primitive dependencies in effects
|
|
77
|
+
- `rerender-derived-state` - Subscribe to derived booleans, not raw values
|
|
78
|
+
- `rerender-derived-state-no-effect` - Derive state during render, not effects
|
|
79
|
+
- `rerender-functional-setstate` - Use functional setState for stable callbacks
|
|
80
|
+
- `rerender-lazy-state-init` - Pass function to useState for expensive values
|
|
81
|
+
- `rerender-simple-expression-in-memo` - Avoid memo for simple primitives
|
|
82
|
+
- `rerender-move-effect-to-event` - Put interaction logic in event handlers
|
|
83
|
+
- `rerender-transitions` - Use startTransition for non-urgent updates
|
|
84
|
+
- `rerender-use-ref-transient-values` - Use refs for transient frequent values
|
|
85
|
+
|
|
86
|
+
### 6. Rendering Performance (MEDIUM)
|
|
87
|
+
|
|
88
|
+
- `rendering-animate-svg-wrapper` - Animate div wrapper, not SVG element
|
|
89
|
+
- `rendering-content-visibility` - Use content-visibility for long lists
|
|
90
|
+
- `rendering-hoist-jsx` - Extract static JSX outside components
|
|
91
|
+
- `rendering-svg-precision` - Reduce SVG coordinate precision
|
|
92
|
+
- `rendering-hydration-no-flicker` - Use inline script for client-only data
|
|
93
|
+
- `rendering-hydration-suppress-warning` - Suppress expected mismatches
|
|
94
|
+
- `rendering-activity` - Use Activity component for show/hide
|
|
95
|
+
- `rendering-conditional-render` - Use ternary, not && for conditionals
|
|
96
|
+
- `rendering-usetransition-loading` - Prefer useTransition for loading state
|
|
97
|
+
|
|
98
|
+
### 7. JavaScript Performance (LOW-MEDIUM)
|
|
99
|
+
|
|
100
|
+
- `js-batch-dom-css` - Group CSS changes via classes or cssText
|
|
101
|
+
- `js-index-maps` - Build Map for repeated lookups
|
|
102
|
+
- `js-cache-property-access` - Cache object properties in loops
|
|
103
|
+
- `js-cache-function-results` - Cache function results in module-level Map
|
|
104
|
+
- `js-cache-storage` - Cache localStorage/sessionStorage reads
|
|
105
|
+
- `js-combine-iterations` - Combine multiple filter/map into one loop
|
|
106
|
+
- `js-length-check-first` - Check array length before expensive comparison
|
|
107
|
+
- `js-early-exit` - Return early from functions
|
|
108
|
+
- `js-hoist-regexp` - Hoist RegExp creation outside loops
|
|
109
|
+
- `js-min-max-loop` - Use loop for min/max instead of sort
|
|
110
|
+
- `js-set-map-lookups` - Use Set/Map for O(1) lookups
|
|
111
|
+
- `js-tosorted-immutable` - Use toSorted() for immutability
|
|
112
|
+
|
|
113
|
+
### 8. Advanced Patterns (LOW)
|
|
114
|
+
|
|
115
|
+
- `advanced-event-handler-refs` - Store event handlers in refs
|
|
116
|
+
- `advanced-init-once` - Initialize app once per app load
|
|
117
|
+
- `advanced-use-latest` - useLatest for stable callback refs
|
|
118
|
+
|
|
119
|
+
## How to Use
|
|
120
|
+
|
|
121
|
+
Read individual rule files for detailed explanations and code examples:
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
rules/async-parallel.md
|
|
125
|
+
rules/bundle-barrel-imports.md
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Each rule file contains:
|
|
129
|
+
- Brief explanation of why it matters
|
|
130
|
+
- Incorrect code example with explanation
|
|
131
|
+
- Correct code example with explanation
|
|
132
|
+
- Additional context and references
|
|
133
|
+
|
|
134
|
+
## Full Compiled Document
|
|
135
|
+
|
|
136
|
+
For the complete guide with all rules expanded: `AGENTS.md`
|
|
@@ -0,0 +1,55 @@
|
|
|
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.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Initialize App Once, Not Per Mount
|
|
3
|
+
impact: LOW-MEDIUM
|
|
4
|
+
impactDescription: avoids duplicate init in development
|
|
5
|
+
tags: initialization, useEffect, app-startup, side-effects
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Initialize App Once, Not Per Mount
|
|
9
|
+
|
|
10
|
+
Do not put app-wide initialization that must run once per app load inside `useEffect([])` of a component. Components can remount and effects will re-run. Use a module-level guard or top-level init in the entry module instead.
|
|
11
|
+
|
|
12
|
+
**Incorrect (runs twice in dev, re-runs on remount):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function Comp() {
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
loadFromStorage()
|
|
18
|
+
checkAuthToken()
|
|
19
|
+
}, [])
|
|
20
|
+
|
|
21
|
+
// ...
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (once per app load):**
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
let didInit = false
|
|
29
|
+
|
|
30
|
+
function Comp() {
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (didInit) return
|
|
33
|
+
didInit = true
|
|
34
|
+
loadFromStorage()
|
|
35
|
+
checkAuthToken()
|
|
36
|
+
}, [])
|
|
37
|
+
|
|
38
|
+
// ...
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Reference: [Initializing the application](https://react.dev/learn/you-might-not-need-an-effect#initializing-the-application)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: useEffectEvent for Stable Callback Refs
|
|
3
|
+
impact: LOW
|
|
4
|
+
impactDescription: prevents effect re-runs
|
|
5
|
+
tags: advanced, hooks, useEffectEvent, refs, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## useEffectEvent 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
|
+
**Incorrect (effect re-runs on every callback change):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
|
|
16
|
+
const [query, setQuery] = useState('')
|
|
17
|
+
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const timeout = setTimeout(() => onSearch(query), 300)
|
|
20
|
+
return () => clearTimeout(timeout)
|
|
21
|
+
}, [query, onSearch])
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (using React's useEffectEvent):**
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { useEffectEvent } from 'react';
|
|
29
|
+
|
|
30
|
+
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
|
|
31
|
+
const [query, setQuery] = useState('')
|
|
32
|
+
const onSearchEvent = useEffectEvent(onSearch)
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
const timeout = setTimeout(() => onSearchEvent(query), 300)
|
|
36
|
+
return () => clearTimeout(timeout)
|
|
37
|
+
}, [query])
|
|
38
|
+
}
|
|
39
|
+
```
|
|
@@ -0,0 +1,38 @@
|
|
|
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).
|
|
@@ -0,0 +1,80 @@
|
|
|
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.
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
**Alternative without extra dependencies:**
|
|
37
|
+
|
|
38
|
+
We can also create all the promises first, and do `Promise.all()` at the end.
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
const userPromise = fetchUser()
|
|
42
|
+
const profilePromise = userPromise.then(user => fetchProfile(user.id))
|
|
43
|
+
|
|
44
|
+
const [user, config, profile] = await Promise.all([
|
|
45
|
+
userPromise,
|
|
46
|
+
fetchConfig(),
|
|
47
|
+
profilePromise
|
|
48
|
+
])
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Reference: [https://github.com/shuding/better-all](https://github.com/shuding/better-all)
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
```
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Strategic Suspense Boundaries
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: faster initial paint
|
|
5
|
+
tags: async, suspense, streaming, layout-shift
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Strategic Suspense Boundaries
|
|
9
|
+
|
|
10
|
+
Instead of awaiting data in async components before returning JSX, use Suspense boundaries to show the wrapper UI faster while data loads.
|
|
11
|
+
|
|
12
|
+
**Incorrect (wrapper blocked by data fetching):**
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
async function Page() {
|
|
16
|
+
const data = await fetchData() // Blocks entire page
|
|
17
|
+
|
|
18
|
+
return (
|
|
19
|
+
<div>
|
|
20
|
+
<div>Sidebar</div>
|
|
21
|
+
<div>Header</div>
|
|
22
|
+
<div>
|
|
23
|
+
<DataDisplay data={data} />
|
|
24
|
+
</div>
|
|
25
|
+
<div>Footer</div>
|
|
26
|
+
</div>
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The entire layout waits for data even though only the middle section needs it.
|
|
32
|
+
|
|
33
|
+
**Correct (wrapper shows immediately, data streams in):**
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
function Page() {
|
|
37
|
+
return (
|
|
38
|
+
<div>
|
|
39
|
+
<div>Sidebar</div>
|
|
40
|
+
<div>Header</div>
|
|
41
|
+
<div>
|
|
42
|
+
<Suspense fallback={<Skeleton />}>
|
|
43
|
+
<DataDisplay />
|
|
44
|
+
</Suspense>
|
|
45
|
+
</div>
|
|
46
|
+
<div>Footer</div>
|
|
47
|
+
</div>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function DataDisplay() {
|
|
52
|
+
const data = await fetchData() // Only blocks this component
|
|
53
|
+
return <div>{data.content}</div>
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Sidebar, Header, and Footer render immediately. Only DataDisplay waits for data.
|
|
58
|
+
|
|
59
|
+
**Alternative (share promise across components):**
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
function Page() {
|
|
63
|
+
// Start fetch immediately, but don't await
|
|
64
|
+
const dataPromise = fetchData()
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<div>
|
|
68
|
+
<div>Sidebar</div>
|
|
69
|
+
<div>Header</div>
|
|
70
|
+
<Suspense fallback={<Skeleton />}>
|
|
71
|
+
<DataDisplay dataPromise={dataPromise} />
|
|
72
|
+
<DataSummary dataPromise={dataPromise} />
|
|
73
|
+
</Suspense>
|
|
74
|
+
<div>Footer</div>
|
|
75
|
+
</div>
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function DataDisplay({ dataPromise }: { dataPromise: Promise<Data> }) {
|
|
80
|
+
const data = use(dataPromise) // Unwraps the promise
|
|
81
|
+
return <div>{data.content}</div>
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function DataSummary({ dataPromise }: { dataPromise: Promise<Data> }) {
|
|
85
|
+
const data = use(dataPromise) // Reuses the same promise
|
|
86
|
+
return <div>{data.summary}</div>
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Both components share the same promise, so only one fetch occurs. Layout renders immediately while both components wait together.
|
|
91
|
+
|
|
92
|
+
**When NOT to use this pattern:**
|
|
93
|
+
|
|
94
|
+
- Critical data needed for layout decisions (affects positioning)
|
|
95
|
+
- SEO-critical content above the fold
|
|
96
|
+
- Small, fast queries where suspense overhead isn't worth it
|
|
97
|
+
- When you want to avoid layout shift (loading → content jump)
|
|
98
|
+
|
|
99
|
+
**Trade-off:** Faster initial paint vs potential layout shift. Choose based on your UX priorities.
|