Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/bundle-analyzable-paths.md
1---2title: Prefer Statically Analyzable Paths3impact: HIGH4impactDescription: avoids accidental broad bundles and file traces5tags: bundle, nextjs, vite, webpack, rollup, esbuild, path6---78## Prefer Statically Analyzable Paths910Build 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.1112Prefer 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.1314When analysis becomes too broad, the cost is real:15- Larger server bundles16- Slower builds17- Worse cold starts18- More memory use1920### Import Paths2122**Incorrect (the bundler cannot tell what may be imported):**2324```ts25const PAGE_MODULES = {26home: './pages/home',27settings: './pages/settings',28} as const2930const Page = await import(PAGE_MODULES[pageName])31```3233**Correct (use an explicit map of allowed modules):**3435```ts36const PAGE_MODULES = {37home: () => import('./pages/home'),38settings: () => import('./pages/settings'),39} as const4041const Page = await PAGE_MODULES[pageName]()42```4344### File-System Paths4546**Incorrect (a 2-value enum still hides the final path from static analysis):**4748```ts49const baseDir = path.join(process.cwd(), 'content/' + contentKind)50```5152**Correct (make each final path literal at the callsite):**5354```ts55const baseDir =56kind === ContentKind.Blog57? path.join(process.cwd(), 'content/blog')58: path.join(process.cwd(), 'content/docs')59```6061In 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.6263Reference: [Next.js output](https://nextjs.org/docs/app/api-reference/config/next-config-js/output), [Next.js dynamic imports](https://nextjs.org/learn/seo/dynamic-imports), [Vite features](https://vite.dev/guide/features.html), [esbuild API](https://esbuild.github.io/api/), [Rollup dynamic import vars](https://www.npmjs.com/package/@rollup/plugin-dynamic-import-vars), [Webpack dependency management](https://webpack.js.org/guides/dependency-management/)64