You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Impact: HIGH (avoids accidental broad bundles and file traces)**
585
+
586
+
Build tools work best when import and file-system paths are obvious at build time. If you hide the real path inside a variable or compose it too dynamically, the tool either has to include a broad set of possible files, warn that it cannot analyze the import, or widen file tracing to stay safe.
587
+
588
+
Prefer explicit maps or literal paths so the set of reachable files stays narrow and predictable. This is the same rule whether you are choosing modules with `import()` or reading files in server/build code.
589
+
590
+
When analysis becomes too broad, the cost is real:
591
+
592
+
- Larger server bundles
593
+
594
+
- Slower builds
595
+
596
+
- Worse cold starts
597
+
598
+
- More memory use
599
+
600
+
**Incorrect: the bundler cannot tell what may be imported**
601
+
602
+
```ts
603
+
const PAGE_MODULES = {
604
+
home: './pages/home',
605
+
settings: './pages/settings',
606
+
} asconst
607
+
608
+
const Page =awaitimport(PAGE_MODULES[pageName])
609
+
```
610
+
611
+
**Correct: use an explicit map of allowed modules**
612
+
613
+
```ts
614
+
const PAGE_MODULES = {
615
+
home: () =>import('./pages/home'),
616
+
settings: () =>import('./pages/settings'),
617
+
} asconst
618
+
619
+
const Page =awaitPAGE_MODULES[pageName]()
620
+
```
621
+
622
+
**Incorrect: a 2-value enum still hides the final path from static analysis**
**Correct: make each final path literal at the callsite**
634
+
635
+
```ts
636
+
const baseDir =
637
+
kind===ContentKind.Blog
638
+
?path.join(process.cwd(), 'content/blog')
639
+
:path.join(process.cwd(), 'content/docs')
640
+
```
641
+
642
+
In Next.js server code, this matters for output file tracing too. `path.join(process.cwd(), someVar)` can widen the traced file set because Next.js statically analyze `import`, `require`, and `fs` usage.
Copy file name to clipboardExpand all lines: skills/react-best-practices/SKILL.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ metadata:
9
9
10
10
# Vercel React Best Practices
11
11
12
-
Comprehensive performance optimization guide for React and Next.js applications, maintained by Vercel. Contains 69 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
12
+
Comprehensive performance optimization guide for React and Next.js applications, maintained by Vercel. Contains 70 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
13
13
14
14
## When to Apply
15
15
@@ -47,6 +47,7 @@ Reference these guidelines when:
Build tools work best when import and file-system paths are obvious at build time. If you hide the real path inside a variable or compose it too dynamically, the tool either has to include a broad set of possible files, warn that it cannot analyze the import, or widen file tracing to stay safe.
11
+
12
+
Prefer explicit maps or literal paths so the set of reachable files stays narrow and predictable. This is the same rule whether you are choosing modules with `import()` or reading files in server/build code.
13
+
14
+
When analysis becomes too broad, the cost is real:
15
+
- Larger server bundles
16
+
- Slower builds
17
+
- Worse cold starts
18
+
- More memory use
19
+
20
+
### Import Paths
21
+
22
+
**Incorrect (the bundler cannot tell what may be imported):**
23
+
24
+
```ts
25
+
const PAGE_MODULES = {
26
+
home: './pages/home',
27
+
settings: './pages/settings',
28
+
} asconst
29
+
30
+
const Page =awaitimport(PAGE_MODULES[pageName])
31
+
```
32
+
33
+
**Correct (use an explicit map of allowed modules):**
34
+
35
+
```ts
36
+
const PAGE_MODULES = {
37
+
home: () =>import('./pages/home'),
38
+
settings: () =>import('./pages/settings'),
39
+
} asconst
40
+
41
+
const Page =awaitPAGE_MODULES[pageName]()
42
+
```
43
+
44
+
### File-System Paths
45
+
46
+
**Incorrect (a 2-value enum still hides the final path from static analysis):**
**Correct (make each final path literal at the callsite):**
58
+
59
+
```ts
60
+
const baseDir =
61
+
kind===ContentKind.Blog
62
+
?path.join(process.cwd(), 'content/blog')
63
+
:path.join(process.cwd(), 'content/docs')
64
+
```
65
+
66
+
In Next.js server code, this matters for output file tracing too. `path.join(process.cwd(), someVar)` can widen the traced file set because Next.js statically analyze `import`, `require`, and `fs` usage.
0 commit comments