Skip to main content

Vercel React Best Practices — Repo Guidance

Source: .agents/references/coding-standard/vercel-react-best-practices/REPO-GUIDANCE.md

Content

Vercel React Best Practices — Repo Guidance

Performance optimization guidance for React and Next.js in this repository, adapted from Vercel Engineering best practices (MIT). The pack contains 70 rules across 8 categories, prioritized by impact.

Entry point: read this file first, then open detailed rule files under rules/ as needed.

Default application

Load this guidance together with .agents/rules/*.mdc whenever agent work touches:

  • src/**/*.tsx or React-related src/**/*.ts
  • Next.js Pages Router (src/pages/), getStaticProps / getServerSideProps, and pages/api
  • Hooks, client/server data fetching, or service callers from UI
  • Imports, dynamic imports, third-party libraries, or bundle-size changes
  • Rendering, re-render, hydration, or JavaScript hot-path review

The always-applied rule .agents/rules/vercel-react-performance.mdc points here. Non-frontend tasks (docs-only, infra-only, etc.) do not need a deep read of this pack.

Repo precedence

If Vercel guidance conflicts with repo rules, repo rules win. In particular:

  • Follow .agents/rules/clean-react.mdc for unnecessary useEffect, unnecessary state, and memoization discipline.
  • Follow .agents/rules/no-unnecessary-usecallback.mdc; do not add useCallback only because a generic performance checklist mentions stable callbacks.
  • Follow .agents/rules/no-reexport-index-files.mdc; avoid barrel files in app code.
  • Use this repo's existing data-fetching and service patterns ({ data, error }, TanStack Query, etc.) before introducing new libraries.

Pages Router (this repo)

This codebase uses Next.js Pages Router only (src/pages/, index.page.tsx). There is no src/app/, no Server Actions ("use server"), and no React Server Components in application code.

Do not introduce App Router routes, Server Actions, or RSC patterns unless the user explicitly requests a migration.

Data-fetching defaults:

  • Page data: getStaticProps / getServerSideProps (often via getDefaultStaticProps in src/utility/getDefaultProps.js)
  • Client data: TanStack Query and @/services/ with { data, error } (see .agents/rules/clean-react.mdc, no-try-catch-service-api.mdc)
  • Mutations and utilities: src/pages/api/ handlers (NextApiRequest / NextApiResponse)

Interpret server- rules through Pages Router (props serialized into the page payload), not RSC trees.

Rule applicability

StatusRules
Apply as-isasync-cheap-condition-before-await, async-defer-await, async-parallel, async-dependencies, async-api-routes (Pages), all bundle-*, client-*, rerender-*, rendering-*, js-*, advanced-*
Adapt (Pages)server-serialization, server-dedup-props, server-parallel-fetching, server-parallel-nested-fetching, server-no-shared-module-state, server-hoist-static-io, server-cache-lru, server-cache-react (limited), async-suspense-boundaries (client Suspense only)
App Router only (skip here)server-auth-actions when documented as "use server" (use API-route auth instead), server-after-nonblocking (after()), RSC-style async Server Component streaming in async-suspense-boundaries

How to use this pack

  1. Skim the category table and quick reference below for the concern you are implementing or reviewing.
  2. Open the matching file: rules/<rule-id>.md (e.g. rules/async-parallel.md).
  3. Each rule file includes why it matters, incorrect/correct examples, and extra context.

Rule categories by priority

PriorityCategoryImpactPrefix
1Eliminating WaterfallsCRITICALasync-
2Bundle Size OptimizationCRITICALbundle-
3Server-Side PerformanceHIGHserver-
4Client-Side Data FetchingMEDIUM-HIGHclient-
5Re-render OptimizationMEDIUMrerender-
6Rendering PerformanceMEDIUMrendering-
7JavaScript PerformanceLOW-MEDIUMjs-
8Advanced PatternsLOWadvanced-

Quick reference

1. Eliminating Waterfalls (CRITICAL)

  • async-cheap-condition-before-await - Check cheap sync conditions before awaiting flags or remote values
  • async-defer-await - Move await into branches where actually used
  • async-parallel - Use Promise.all() for independent operations
  • async-dependencies - Use better-all for partial dependencies
  • async-api-routes (Pages) - Start promises early, await late in pages/api handlers
  • async-suspense-boundaries (Pages: client Suspense; App-only: async RSC streaming) - Defer slow UI with client <Suspense>, next/dynamic, or client queries

2. Bundle Size Optimization (CRITICAL)

  • bundle-barrel-imports - Import directly, avoid barrel files
  • bundle-analyzable-paths - Prefer statically analyzable import and file-system paths to avoid broad bundles and traces
  • bundle-dynamic-imports - Use next/dynamic for heavy components
  • bundle-defer-third-party - Load analytics/logging after hydration
  • bundle-conditional - Load modules only when feature is activated
  • bundle-preload - Preload on hover/focus for perceived speed

3. Server-Side Performance (HIGH)

  • server-auth-actions (Pages: API routes; App-only: "use server") - Authenticate pages/api handlers; Server Actions N/A in this repo
  • server-cache-react (Pages: limited) - Prefer Promise.all in data functions; React.cache() is App Router/RSC
  • server-cache-lru (Pages) - LRU cache for cross-request caching
  • server-dedup-props (Pages) - Avoid duplicate fields in getStaticProps / getServerSideProps return values
  • server-hoist-static-io (Pages) - Hoist static I/O (fonts, logos) to module level
  • server-no-shared-module-state (Pages) - No mutable module scope for per-request data during GSSP/SSR
  • server-serialization (Pages) - Minimize props returned from page data functions
  • server-parallel-fetching (Pages) - Promise.all in getStaticProps / getServerSideProps
  • server-parallel-nested-fetching (Pages) - Per-item promise chains inside Promise.all
  • server-after-nonblocking (App-only) - after(); not used in Pages Router here

4. Client-Side Data Fetching (MEDIUM-HIGH)

  • client-swr-dedup - Use SWR for automatic request deduplication
  • client-event-listeners - Deduplicate global event listeners
  • client-passive-event-listeners - Use passive listeners for scroll
  • client-localstorage-schema - Version and minimize localStorage data

5. Re-render Optimization (MEDIUM)

  • rerender-defer-reads - Don't subscribe to state only used in callbacks
  • rerender-memo - Extract expensive work into memoized components
  • rerender-memo-with-default-value - Hoist default non-primitive props
  • rerender-dependencies - Use primitive dependencies in effects
  • rerender-derived-state - Subscribe to derived booleans, not raw values
  • rerender-derived-state-no-effect - Derive state during render, not effects
  • rerender-functional-setstate - Use functional setState for stable callbacks
  • rerender-lazy-state-init - Pass function to useState for expensive values
  • rerender-simple-expression-in-memo - Avoid memo for simple primitives
  • rerender-split-combined-hooks - Split hooks with independent dependencies
  • rerender-move-effect-to-event - Put interaction logic in event handlers
  • rerender-transitions - Use startTransition for non-urgent updates
  • rerender-use-deferred-value - Defer expensive renders to keep input responsive
  • rerender-use-ref-transient-values - Use refs for transient frequent values
  • rerender-no-inline-components - Don't define components inside components

6. Rendering Performance (MEDIUM)

  • rendering-animate-svg-wrapper - Animate div wrapper, not SVG element
  • rendering-content-visibility - Use content-visibility for long lists
  • rendering-hoist-jsx - Extract static JSX outside components
  • rendering-svg-precision - Reduce SVG coordinate precision
  • rendering-hydration-no-flicker - Use inline script for client-only data
  • rendering-hydration-suppress-warning - Suppress expected mismatches
  • rendering-activity - Use Activity component for show/hide
  • rendering-conditional-render - Use ternary, not && for conditionals
  • rendering-usetransition-loading - Prefer useTransition for loading state
  • rendering-resource-hints - Use React DOM resource hints for preloading
  • rendering-script-defer-async - Use defer or async on script tags

7. JavaScript Performance (LOW-MEDIUM)

  • js-batch-dom-css - Group CSS changes via classes or cssText
  • js-index-maps - Build Map for repeated lookups
  • js-cache-property-access - Cache object properties in loops
  • js-cache-function-results - Cache function results in module-level Map
  • js-cache-storage - Cache localStorage/sessionStorage reads
  • js-combine-iterations - Combine multiple filter/map into one loop
  • js-length-check-first - Check array length before expensive comparison
  • js-early-exit - Return early from functions
  • js-hoist-regexp - Hoist RegExp creation outside loops
  • js-min-max-loop - Use loop for min/max instead of sort
  • js-set-map-lookups - Use Set/Map for O(1) lookups
  • js-tosorted-immutable - Use toSorted() for immutability
  • js-flatmap-filter - Use flatMap to map and filter in one pass
  • js-request-idle-callback - Defer non-critical work to browser idle time

8. Advanced Patterns (LOW)

  • advanced-effect-event-deps - Don't put useEffectEvent results in effect deps
  • advanced-event-handler-refs - Store event handlers in refs
  • advanced-init-once - Initialize app once per app load
  • advanced-use-latest - useLatest for stable callback refs

Attribution

Upstream: Vercel Engineering React/Next.js performance best practices. License: MIT.