πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Caching Strategies

Next.js Performance🟒 Free Lesson

Advertisement

Caching Strategies

Data cache, full route cache, router cache, and cache invalidation.

Overview

Next.js has multiple caching layers for optimal performance.

Key Concepts

  • Data Cache β€” Cache fetched data across requests
  • Full Route Cache β€” Cache entire routes on server
  • Router Cache β€” Client-side route prefetching
  • Revalidation β€” Time-based or on-demand invalidation
  • Cache Tags β€” Fine-grained cache control

Code Examples

// Data Cache with revalidation
async function getProducts() {
  const res = await fetch('https://api.example.com/products', {
    next: { revalidate: 3600 } // Cache for 1 hour
  });
  return res.json();
}

// Cache with tags for selective invalidation
async function getUser(id) {
  const res = await fetch(`https://api.example.com/users/${id}`, {
    next: { tags: [`user-${id}`, 'users'] }
  });
  return res.json();
}

// app/api/revalidate/route.js
import { revalidateTag } from 'next/cache';

export async function POST(request) {
  const { tag } = await request.json();
  revalidateTag(tag);
  return Response.json({ revalidated: true });
}

// Force dynamic route (no cache)
async function DynamicPage() {
  const data = await fetch('https://api.example.com/data', {
    cache: 'no-store' // Always fresh
  });
  return <div>{data}</div>;
}

// Opt out of full route cache
export const dynamic = 'force-dynamic';

Practice

Implement cache invalidation for a product catalog with different strategies.

⭐

Premium Content

Caching Strategies

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert Next.js Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement