Skip to content

lootlog/monorepo / .agents/skills/vercel-react-best-practices

vercel-react-best-practices

Verified

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.

FileKindSize
AGENTS.mdmarkdown81.7 KB
README.mdmarkdown3.3 KB
SKILL.mdskill md6.3 KB
rules/advanced-event-handler-refs.mdmarkdown1.5 KB
rules/advanced-init-once.mdmarkdown967 B
rules/advanced-use-latest.mdmarkdown1.1 KB
rules/async-api-routes.mdmarkdown1.1 KB
rules/async-defer-await.mdmarkdown2.0 KB
rules/async-dependencies.mdmarkdown1.3 KB
rules/async-parallel.mdmarkdown659 B
rules/async-suspense-boundaries.mdmarkdown2.5 KB
rules/bundle-barrel-imports.mdmarkdown2.3 KB
rules/bundle-conditional.mdmarkdown966 B
rules/bundle-defer-third-party.mdmarkdown928 B
rules/bundle-dynamic-imports.mdmarkdown799 B
rules/bundle-preload.mdmarkdown1.1 KB
rules/client-event-listeners.mdmarkdown2.0 KB
rules/client-localstorage-schema.mdmarkdown2.0 KB
rules/client-passive-event-listeners.mdmarkdown1.6 KB
rules/client-swr-dedup.mdmarkdown1.1 KB
rules/js-batch-dom-css.mdmarkdown3.2 KB
rules/js-cache-function-results.mdmarkdown1.9 KB
rules/js-cache-property-access.mdmarkdown536 B
rules/js-cache-storage.mdmarkdown1.6 KB
rules/js-combine-iterations.mdmarkdown768 B
rules/js-early-exit.mdmarkdown1.1 KB
rules/js-hoist-regexp.mdmarkdown1.0 KB
rules/js-index-maps.mdmarkdown850 B
rules/js-length-check-first.mdmarkdown1.7 KB
rules/js-min-max-loop.mdmarkdown2.2 KB
rules/js-set-map-lookups.mdmarkdown532 B
rules/js-tosorted-immutable.mdmarkdown1.7 KB
rules/rendering-activity.mdmarkdown566 B
rules/rendering-animate-svg-wrapper.mdmarkdown1.1 KB
rules/rendering-conditional-render.mdmarkdown938 B
rules/rendering-content-visibility.mdmarkdown818 B
rules/rendering-hoist-jsx.mdmarkdown993 B
rules/rendering-hydration-no-flicker.mdmarkdown2.2 KB
rules/rendering-hydration-suppress-warning.mdmarkdown852 B
rules/rendering-svg-precision.mdmarkdown588 B
rules/rendering-usetransition-loading.mdmarkdown2.0 KB
rules/rerender-defer-reads.mdmarkdown983 B
rules/rerender-dependencies.mdmarkdown833 B
rules/rerender-derived-state-no-effect.mdmarkdown1.2 KB
rules/rerender-derived-state.mdmarkdown732 B
rules/rerender-functional-setstate.mdmarkdown2.9 KB
rules/rerender-lazy-state-init.mdmarkdown2.0 KB
rules/rerender-memo-with-default-value.mdmarkdown1.1 KB
rules/rerender-memo.mdmarkdown1.1 KB
rules/rerender-move-effect-to-event.mdmarkdown1.2 KB
rules/rerender-simple-expression-in-memo.mdmarkdown1023 B
rules/rerender-transitions.mdmarkdown1.0 KB
rules/rerender-use-ref-transient-values.mdmarkdown1.7 KB
rules/server-after-nonblocking.mdmarkdown2.0 KB
rules/server-auth-actions.mdmarkdown2.6 KB
rules/server-cache-lru.mdmarkdown1.3 KB
rules/server-cache-react.mdmarkdown2.2 KB
rules/server-dedup-props.mdmarkdown2.0 KB
rules/server-hoist-static-io.mdmarkdown4.3 KB
rules/server-parallel-fetching.mdmarkdown1.5 KB
rules/server-serialization.mdmarkdown1006 B

rules/server-hoist-static-io.md

View on GitHub
---
title: Hoist Static I/O to Module Level
impact: HIGH
impactDescription: avoids repeated file/network I/O per request
tags: server, io, performance, next.js, route-handlers, og-image
---

## 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**

```typescript
// 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**

```typescript
// 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**

```typescript
// 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**

```typescript
// 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](https://vercel.com/docs/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.
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall