Next.js

Cache Tags, Cache Life & Revalidation

28 min Lesson 43 of 80

Cache Tags, Cache Life & Revalidation

This lesson expands the Next.js path with an advanced topic from the official Next.js documentation. The goal is not only to memorize an option or file name, but to understand its impact on rendering, caching, security, and deployment.

After this lesson you should be able to apply the topic in a real project, choose the right boundary for it, and explain it as a reviewable engineering decision.

Core Concepts

  • cacheTag grouping
  • cacheLife profiles
  • revalidateTag
  • revalidatePath
  • editor publishing flows

Practical Example

// app/lib/posts.ts import { cacheLife, cacheTag } from 'next/cache' export async function getPost(slug: string) { 'use cache' cacheLife('days') cacheTag('posts') cacheTag('post-' + slug) return db.post.findUnique({ where: { slug } }) } // app/admin/posts/actions.ts 'use server' import { revalidateTag } from 'next/cache' export async function publishPost(slug: string) { await db.post.update({ where: { slug }, data: { published: true } }) revalidateTag('posts') revalidateTag('post-' + slug) }
This lesson is aligned with these official Next.js documentation areas: Revalidating, cacheTag, cacheLife, revalidateTag, and revalidatePath docs.

Why It Matters

In production applications, this topic affects page speed, data freshness, authorization clarity, and operational reliability after deployment.

Implementation Workflow

  • Decide whether the data is public or user-specific.
  • Choose the smallest part of the tree that needs this behavior.
  • Connect the example to a real route and add a small verification check.
  • Document the effect on caching and deployment.

Hands-on Practice

Create cache tags for posts, categories, and navigation, then invalidate them after admin mutations.

Time-based revalidation alone is weak for admin publishing workflows.

Summary

Judge the implementation by how clear the decision is, whether the behavior is correct after build, and how easily it can be traced in production.