Hirefullstack – Software Engineering & IT-Beratung aus Berlin
← Back to overview
Core 10 min read

Revalidation

After saving, refetch exactly what changed

In one sentence

Instead of switching caching off, you tell Next.js precisely when something is stale. The page stays fast and is still current.

The previous lesson ended with a dilemma: caching makes things fast, but data goes stale. The resolution is not to turn the cache off – it is to invalidate it at the right moment.

Two routes

routes.txt
By time            revalidate: 60
                   "at most a minute old" – without you doing
                   anything. Good for third-party content.

On demand          revalidatePath() / revalidateTag()
                   "stale as of right now" – after YOU changed
                   something. Good for your own data.

The difference in one sentence: by time you are guessing how quickly something goes stale. On demand you know.

By time

time.tsx
// For the whole page
export const revalidate = 3600;   // hourly

// Or per request
const rates = await fetch(url, { next: { revalidate: 300 } });
Good to know The first request after expiry still gets the old one

Next.js serves the cached version and rebuilds in the background. Only the next visitor sees the fresh data. That is deliberate: nobody has to wait for the rebuild. The term for it is ISRA static page with an expiry date: after the configured time Next.js rebuilds it in the background.Short for Incremental Static Regeneration. Configured through `revalidate`..

On demand

app/actions.ts
"use server";

import { revalidatePath, revalidateTag } from "next/cache";

export async function savePost(id: string, data: FormData) {
  await db.post.update({ where: { id }, data: … });

  // Option 1: one specific address
  revalidatePath("/blog/" + id);
  revalidatePath("/blog");            // the overview as well

  // Option 2: everything carrying this tag
  revalidateTag("posts");
}
tags.tsx
// Attach a tag when fetching …
await fetch(url, { next: { tags: ["posts"] } });

// … and invalidate everything with it later.
revalidateTag("posts");
Tags are handy when the same data appears on many pages and you do not want to list them all.
Pitfall The overview is the one people forget

After editing a post, not only /blog/my-post is stale but also /blog and perhaps the home page. Clear only the single case and you will wonder why the list still shows the old title. Tags solve exactly that.

Which one when

Reach for it when …
  • +

    By time for data you do not control: exchange rates, weather, a third-party feed

  • +

    On demand for everything you change yourself – from inside a server action

  • +

    Both together: on demand as the rule, time as a safety net

Skip it when …
  • no-store everywhere just to dodge the topic

  • Very short intervals (revalidate = 1) – that is dynamic with extra steps

  • Doing nothing after saving and hoping

Tip For editors: the route handler detour

If the content lives in an external CMS, that system can call an address on your site after publishing (a webhook). Inside it you call revalidateTag(...). The site then becomes current the moment somebody publishes – with no interval to guess.

In more depth: what revalidate does not touch optional

revalidatePath clears the server-side cache. The Router cacheThe store in the browser: navigating back shows the page instantly from memory. in the user's browser is untouched at first – which is why you sometimes still see the old state after saving.

browser.tsx
"use client";
import { useRouter } from "next/navigation";

const router = useRouter();
router.refresh();     // refetches the current page from the server
Good to know Inside a server action it happens automatically

If you call revalidatePath within a server action, Next.js refreshes the current view along with it. You only need router.refresh() when the change happened outside React's knowledge.

Does it stick?

4 questions on this lesson. Wrong answers show up in your stats.

Hirefullstack

Need React firepower on your team?

We have been building React and Next.js applications for clients across Germany for years – as a single expert, as reinforcement for an existing team, or as a complete Scrum team.

Talk about your project →