Skip to content

giuseppe-trisciuoglio/developer-kit / plugins/developer-kit-typescript/skills/nextjs-performance

nextjs-performance

Verified

Expert Next.js performance optimization skill covering Core Web Vitals, image/font optimization, caching strategies, streaming, bundle optimization, and Server Components best practices. Use when optimizing Next.js applications for Core Web Vitals (LCP, INP, CLS), implementing next/image and next/font, configuring caching with unstable_cache and revalidateTag, converting Client Components to Server Components, implementing Suspense streaming, or analyzing and reducing bundle size. Supports Next.js 16 + React 19 patterns.

references/streaming-suspense.md

View on GitHub
# Streaming e Suspense in Next.js

## Overview

Streaming permette di inviare parti della UI man mano che sono pronte, migliorando il Time to First Byte (TTFB) e permettendo all'utente di vedere contenuto più velocemente.

---

## Pattern Base

### Loading.tsx

```typescript
// app/blog/loading.tsx
export default function Loading() {
  return (
    <div className="loading-skeleton">
      <div className="h-8 w-48 bg-gray-200 rounded animate-pulse" />
      <div className="mt-4 space-y-2">
        {<>Array.from({ length: 5 }).map((_, i) => (
          <div key={i} className="h-4 bg-gray-200 rounded animate-pulse" />
        ))}</>
      </div>
    </div>
  )
}
```

### Suspense Boundaries

```typescript
// app/page.tsx
import { Suspense } from 'react'
import { ProductListSkeleton } from './components/ProductListSkeleton'
import { ProductList } from './components/ProductList'
import { ReviewsSkeleton } from './components/ReviewsSkeleton'
import { Reviews } from './components/Reviews'

export default function Page() {
  return (
    <div>
      {/* Questo è streamato immediatamente */}
      <header>
        <h1>Our Products</h1>
      </header>

      {/* Suspense boundary per ProductList */}
      <Suspense fallback={<ProductListSkeleton />}>
        <ProductList />
      </Suspense>

      {/* Reviews può caricare indipendentemente */}
      <Suspense fallback={<ReviewsSkeleton />}>
        <Reviews />
      </Suspense>
    </div>
  )
}
```

---

## Streaming Pattern

### Waterfall Ottimizzato

```typescript
// BEFORE - Waterfall sequenziale
export default async function Page() {
  const user = await fetchUser()      // 100ms
  const orders = await fetchOrders()   // 150ms (dipende da user)
  const recommendations = await fetchRecommendations() // 200ms

  return (
    <div>
      <UserProfile user={user} />
      <OrderList orders={orders} />
      <Recommendations items={recommendations} />
    </div>
  )
}

// AFTER - Parallel fetching con Suspense
export default function Page() {
  return (
    <div>
      <Suspense fallback={<UserSkeleton />}>
        <UserProfile />
      </Suspense>

      <Suspense fallback={<OrdersSkeleton />}>
        <OrderList />
      </Suspense>

      <Suspense fallback={<RecsSkeleton />}>
        <Recommendations />
      </Suspense>
    </div>
  )
}

// Ogni componente fetcha i propri dati
async function UserProfile() {
  const user = await fetchUser()
  return <div>{user.name}</div>
}

async function OrderList() {
  const orders = await fetchOrders()
  return <ul>{orders.map(o => <li key={o.id}>{o.total}</li>)}</ul>
}
```

### Nested Suspense

```typescript
// app/dashboard/page.tsx
export default function DashboardPage() {
  return (
    <Suspense fallback={<DashboardSkeleton />}>
      <Dashboard />
    </Suspense>
  )
}

// app/dashboard/Dashboard.tsx
async function Dashboard() {
  return (
    <div>
      <Sidebar />

      <main>
        <Suspense fallback={<ChartSkeleton />}>
          <RevenueChart />
        </Suspense>

        <Suspense fallback={<TableSkeleton />}>
          <RecentOrders />
        </Suspense>
      </main>
    </div>
  )
}
```

---

## use() Hook (React 19)

```typescript
'use client'

import { use, Suspense } from 'react'

// Promise creata fuori dal componente
const messagePromise = fetchMessage()

function Message() {
  // Suspende finché la promise non resolve
  const message = use(messagePromise)
  return <p>{message}</p>
}

export default function Page() {
  return (
    <Suspense fallback={<Loading />}>
      <Message />
    </Suspense>
  )
}
```

### Con Props

```typescript
'use client'

import { use } from 'react'

function Comments({ commentsPromise }: { commentsPromise: Promise<Comment[]> }) {
  const comments = use(commentsPromise)

  return (
    <ul>
      {comments.map(c => <li key={c.id}>{c.text}</li>)}
    </ul>
  )
}

// Server Component che passa la promise
export default function Page() {
  const commentsPromise = fetchComments() // Non await!

  return (
    <Suspense fallback={<Loading />}>
      <Comments commentsPromise={commentsPromise} />
    </Suspense>
  )
}
```

---

## Error Boundaries

```typescript
// app/error.tsx
'use client'

export default function ErrorBoundary({
  error,
  reset,
}: {
  error: Error & { digest?: string }
  reset: () => void
}) {
  return (
    <div className="error-container">
      <h2>Something went wrong!</h2>
      <button onClick={reset}>Try again</button>
    </div>
  )
}
```

---

## Best Practices

```typescript
// ✅ SÌ: Place Suspense boundaries strategically
<Suspense fallback={<SpecificSkeleton />}>
  <ExpensiveComponent />
</Suspense>

// ❌ NON: Unico Suspense in alto livello
<Suspense fallback={<GenericLoading />}>
  <EntirePage />
</Suspense>

// ✅ SÌ: Skeletons specifici per ogni sezione
<Suspense fallback={<ProductGridSkeleton />}>
  <ProductGrid />
</Suspense>

<Suspense fallback={<ReviewListSkeleton />}>
  <ReviewList />
</Suspense>

// ✅ SÌ: Fetch nel componente che usa i dati
async function ProductList() {
  const products = await fetchProducts() // Fetch locale
  return <div>{...}</div>
}

// ❌ NON: Passare dati attraverso props
async function Page() {
  const products = await fetchProducts() // ❌ Blocca tutta la pagina
  return <ProductList products={products} />
}
```
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