Skip to content

Revalidate Next.js blog posts after a publish: time-based, on-demand and cache tags

How to revalidate Next.js blog posts stored in your own database: time-based ISR, on-demand revalidatePath, cache tags, the Pages Router and common mistakes.

Developer and content writer checking a newly published blog page on a laptop and phone in a small studio
In this article

If your Next.js blog reads posts from its own database, a new or edited post only appears once the cached page is rebuilt. To revalidate Next.js blog posts reliably, give the post page and the blog index a short time-based revalidate, allow new slugs with dynamicParams, and add on-demand revalidation with revalidatePath or cache tags if you need posts live within seconds.

This guide is for developers whose Next.js site stores posts in Postgres, MySQL, SQLite or MongoDB, and for the content teams who keep asking "why isn't my post showing?". It covers the App Router and the Pages Router, the three caching setups that work, and the mistakes that leave old content on the page.

Why a published post doesn't appear

When a post lands in your database, Next.js doesn't know. If the blog page was rendered statically at build time, or cached with a long revalidation window, visitors keep seeing the old version until something tells Next.js to render it again.

Three things usually cause the gap:

  • Static pages with no revalidation. The page was built once and never again.
  • New slugs that return 404. generateStaticParams listed the slugs at build time, and dynamicParams was set to false.
  • Database queries cached forever. A query wrapped in a cache helper with no tag or time limit.

The fix depends on how fresh posts need to be, and how much traffic hits your database.

Three ways to revalidate Next.js blog posts

Approach How fresh Database load Setup
Dynamic rendering Instant Every request queries the database One line
Time-based revalidation Within the window you set, such as 60 seconds One query per page per window One line per route
On-demand revalidation Seconds after the publish Lowest A route handler or a call after the write

For most company blogs, time-based revalidation at 60 to 300 seconds is enough. Writers publish, refresh a minute later and see the post. Add on-demand revalidation when a team schedules launches to the minute, or when the blog index has to update as soon as a post goes live.

Time-based revalidation in the App Router

Set a revalidation window on the route segment. For a typical blog with a post page at app/blog/[slug]/page.tsx:

// app/blog/[slug]/page.tsx
export const revalidate = 120;      // rebuild at most every 2 minutes
export const dynamicParams = true;  // render slugs that weren't known at build time

export async function generateStaticParams() {
  const posts = await db.post.findMany({
    where: { status: "published" },
    select: { slug: true },
  });
  return posts.map((p) => ({ slug: p.slug }));
}

export default async function PostPage({ params }) {
  const { slug } = await params;
  const post = await db.post.findUnique({ where: { slug } });
  if (!post || post.status !== "published") notFound();
  return <Article post={post} />;
}

Do the same on app/blog/page.tsx so the index picks up new posts, and on any RSS or sitemap route. dynamicParams = true is the default, but set it explicitly. A teammate who flips it to false for "performance" will stop every new post from rendering.

The Next.js guide to incremental static regeneration explains how the window behaves: the first visitor after it expires gets the cached page while Next.js renders a fresh one in the background.

On-demand revalidation after a publish

On-demand revalidation rebuilds a page the moment you ask. In the App Router, call revalidatePath from a route handler or server action:

// app/api/revalidate/route.ts
import { revalidatePath } from "next/cache";

export async function POST(req: Request) {
  if (req.headers.get("x-revalidate-secret") !== process.env.REVALIDATE_SECRET) {
    return new Response("Unauthorised", { status: 401 });
  }
  const { slug } = await req.json();
  revalidatePath(`/blog/${slug}`);
  revalidatePath("/blog");
  return Response.json({ revalidated: true });
}

Test it with curl:

curl -X POST https://example.com/api/revalidate \
  -H "x-revalidate-secret: $REVALIDATE_SECRET" \
  -H "Content-Type: application/json" \
  -d '{"slug":"my-new-post"}'

Keep the secret in an environment variable and never in client code. If anyone can call the route, anyone can make your server rebuild pages on a loop.

Using cache tags for database queries

Path revalidation is simple, but a post appears in several places: its own page, the index, category pages, the RSS feed. Tags let you clear all of them at once. Wrap your queries in a tagged cache and revalidate the tag:

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

export const getPublishedPosts = unstable_cache(
  () => db.post.findMany({ where: { status: "published" } }),
  ["published-posts"],
  { tags: ["posts"] }
);

// after a publish
revalidateTag("posts");

Newer Next.js versions add the use cache directive with cacheTag, and change the revalidateTag signature. Check the docs for the version you run before you copy code between projects.

The Pages Router version

On the Pages Router, set revalidate in getStaticProps and use fallback: "blocking" in getStaticPaths so new slugs render on first request:

export async function getStaticPaths() {
  return { paths: [], fallback: "blocking" };
}

export async function getStaticProps({ params }) {
  const post = await getPost(params.slug);
  if (!post) return { notFound: true, revalidate: 60 };
  return { props: { post }, revalidate: 120 };
}

For on-demand rebuilds, an API route calls await res.revalidate("/blog/" + slug). Note the revalidate: 60 on the not found branch. Without it, a request for a slug that doesn't exist yet can cache a 404 for longer than you expect.

Where Quillfleet fits

Quillfleet publishes to Next.js sites through a connector a developer installs, which writes posts into the site's own database. That means the caching rules above still apply: the post row is written, and your pages decide when to show it, so you still need to revalidate Next.js blog posts on your side. The setup is in our Next.js connection guide, and the reasoning behind keeping posts in your own database is in a Next.js blog without a headless CMS.

Scheduled posts behave the same way. When a post set with Schedule for later goes live at its time, a time-based window shows it within that window. If your launch must appear to the minute, pair the schedule with on-demand revalidation. Our guide to publishing and scheduling explains how per-site status works.

Mistakes that leave old content on the page

Most bug reports about posts that won't appear come down to one of these. Check them in order before you change how you revalidate Next.js blog posts.

  • Revalidating the post but not the index. The post works when you visit its URL, but the blog page still lists last week's posts.
  • Forgetting the sitemap and RSS routes. Search engines and feed readers see stale lists.
  • Changing a slug without a redirect. The old URL now returns 404. Add a redirect in next.config.js or your middleware.
  • Testing in development. next dev renders on every request, so caching bugs only show up in a production build. Run next build && next start before you decide it works.
  • A CDN in front with its own cache. If Cloudflare or another CDN caches HTML, revalidating in Next.js isn't enough. Respect the Cache-Control headers Next.js sends, or purge the CDN as well.
  • Unpublished posts still rendering. Check the post status in the page query, not just the list query, so a post moved back to draft stops showing.

Frequently asked questions

How often should a Next.js blog revalidate?

Between 60 and 300 seconds suits most company blogs. Shorter windows mean more database queries for little gain, since most posts are read hours or days after they go live. Use on-demand revalidation if a team needs posts visible within seconds.

Do I need on-demand revalidation if I use time-based revalidation?

No. Time-based revalidation on its own is enough for most blogs. On-demand revalidation is useful for launches, breaking updates, or when you want long cache windows to keep database load low.

Why do new blog posts return 404 in Next.js?

Usually dynamicParams is set to false in the App Router, or fallback is false in the Pages Router. Next.js then only serves the slugs it knew at build time. Set it to allow new slugs and the page will render on first request.

Does revalidation work on a self-hosted Next.js site?

Yes, time-based and on-demand revalidation both work with next start on your own server. If you run several instances behind a load balancer, they need a shared cache, otherwise each instance revalidates on its own schedule.

Will editing a published post update the page?

Only after the page revalidates. An edit is a database change like any other, so the same window or on-demand call applies to updates as to new posts.

If you want to see the full flow from writing to a live page, try the demo site or follow the Next.js setup guide to connect your own.

Write it once. Let the fleet carry it.

Try Quillfleet free for 30 days on up to 5 sites and publish your next post to all of them at once. No credit card, no plugin, no migration.