All skills
vercel-labs avatar

/vercel-react-best-practices

@805687f
by Vercel Labsvercel-labs/openreview1.7k stars
118

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.

Use this Skill: https://skilld.dev/gh/vercel-labs/openreview/vercel-react-best-practices

This session only. Nothing lands on disk.

rulesserver-hoist-static-io.md

≈1.1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Hoist Static I/O to Module Level

Impact: HIGH (avoids repeated file/network I/O per request)

When loading static assets (fonts, logos, images, config files) in route handlers or server functions, hoist the I/O operation to module level. Module-level code runs once when the module is first imported, not on every request. This eliminates redundant file system reads or network fetches that would otherwise run on every invocation.

Incorrect: reads font file on every request

// app/api/og/route.tsx
import { ImageResponse } from 'next/og'

export async function GET(request: Request) {
  // Runs on EVERY request - expensive!
  const fontData = await fetch(
    new URL('./fonts/Inter.ttf', import.meta.url)
  ).then(res => res.arrayBuffer())

  const logoData = await fetch(
    new URL('./images/logo.png', import.meta.url)
  ).then(res => res.arrayBuffer())

  return new ImageResponse(
    <div style={{ fontFamily: 'Inter' }}>
      <img src={logoData} />
      Hello World
    </div>,
    { fonts: [{ name: 'Inter', data: fontData }] }
  )
}

Correct: loads once at module initialization

// app/api/og/route.tsx
import { ImageResponse } from 'next/og'

// Module-level: runs ONCE when module is first imported
const fontData = fetch(
  new URL('./fonts/Inter.ttf', import.meta.url)
).then(res => res.arrayBuffer())

const logoData = fetch(
  new URL('./images/logo.png', import.meta.url)
).then(res => res.arrayBuffer())

export async function GET(request: Request) {
  // Await the already-started promises
  const [font, logo] = await Promise.all([fontData, logoData])

  return new ImageResponse(
    <div style={{ fontFamily: 'Inter' }}>
      <img src={logo} />
      Hello World
    </div>,
    { fonts: [{ name: 'Inter', data: font }] }
  )
}

Alternative: synchronous file reads with Node.js fs

// app/api/og/route.tsx
import { ImageResponse } from 'next/og'
import { readFileSync } from 'fs'
import { join } from 'path'

// Synchronous read at module level - blocks only during module init
const fontData = readFileSync(
  join(process.cwd(), 'public/fonts/Inter.ttf')
)

const logoData = readFileSync(
  join(process.cwd(), 'public/images/logo.png')
)

export async function GET(request: Request) {
  return new ImageResponse(
    <div style={{ fontFamily: 'Inter' }}>
      <img src={logoData} />
      Hello World
    </div>,
    { fonts: [{ name: 'Inter', data: fontData }] }
  )
}

General Node.js example: loading config or templates

// Incorrect: reads config on every call
export async function processRequest(data: Data) {
  const config = JSON.parse(await fs.readFile("./config.json", "utf-8"));
  const template = await fs.readFile("./template.html", "utf-8");

  return render(template, data, config);
}

// Correct: loads once at module level
const configPromise = fs.readFile("./config.json", "utf-8").then(JSON.parse);
const templatePromise = fs.readFile("./template.html", "utf-8");

export async function processRequest(data: Data) {
  const [config, template] = await Promise.all([
    configPromise,
    templatePromise,
  ]);

  return render(template, data, config);
}

When to use this pattern:

  • Loading fonts for OG image generation
  • Loading static logos, icons, or watermarks
  • Reading configuration files that don't change at runtime
  • Loading email templates or other static templates
  • Any static asset that's the same across all requests

When NOT to use this pattern:

  • Assets that vary per request or user
  • Files that may change during runtime (use caching with TTL instead)
  • Large files that would consume too much memory if kept loaded
  • Sensitive data that shouldn't persist in memory

With Vercel's Fluid Compute: Module-level caching is especially effective because multiple concurrent requests share the same function instance. The static assets stay loaded in memory across requests without cold start penalties.

In traditional serverless: Each cold start re-executes module-level code, but subsequent warm invocations reuse the loaded assets until the instance is recycled.

Source: SKILL.md on GitHub

No alerts17d4 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill provides rules, tools, and scripts intended for compiling and managing React and Next.js performance optimization guidelines. No high-severity security considerations or anomalous patterns were discovered.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    1/61 files flagged

Signed by skilld at 805687f. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 7 months ago
metadata
{
  "author": "vercel",
  "version": "1.0.0"
}
  • React
  • Performance
  • next.js
  • optimization
  • bundle-size
  • server-components
  • data-fetching
  • rendering
  • javascript

README badge

README badge for vercel-labs/openreview/vercel-react-best-practices

Provides 58 prioritized React and Next.js performance rules from Vercel Engineering, organized across waterfalls, bundle size, server/client data fetching, re-renders, and rendering patterns. Use when writing or refactoring React components and Next.js pages to apply production-grade optimization practices.

Generated from the current SKILL.md.

Does this skill apply to both React and Next.js?
Yes. The skill covers React components, Next.js pages, server components, server actions, and data fetching patterns for both frameworks.
Can I use this skill to review existing code for performance issues?
Yes. The skill is designed for reviewing and refactoring React/Next.js code against 58 prioritized rules organized by impact (critical, high, medium, low).
Does this skill cover bundle size optimization?
Yes. Bundle size optimization is marked as a critical priority category with 5 rules, including barrel imports, dynamic imports, and third-party script deferral.
What about server-side performance and data fetching?
The skill covers both. It includes rules for server components, React.cache(), LRU caching, parallel fetching, and client-side patterns like SWR deduplication and event listener optimization.

Generated from the current SKILL.md. These answers refresh after source changes.