できない.dev

Next.js の revalidatePath / revalidateTag が効かず古いデータが残る

revalidatePath / revalidateTag が効かないときは、fetch にタグを付け 'use server' 関数から呼べば直る。
クライアント側の Router Cache は router.refresh() で更新する。

公開: 更新:

症状できない
curl -s http://localhost:3000/tagged | grep -o '[a-z]*: version [0-9]*' | head -n 1
tagged: version 1

2026-10-20 に node:20(Next.js 16.4.2 / Node.js 20.20.2) で実際に打って取った出力。検証環境の詳細

要約

revalidatePath / revalidateTag を呼んだのにページが更新されない場合、原因は App Router のキャッシュ階層を 1 つだけ叩いているケースが多い。
fetch にタグが付いているか、Server Action から呼ばれているか、クライアント Router Cache をどう更新するかの 3 点を順に確認する。

実行例

node:20(Next.js 16.4.2)で上記の手順を動かすと、データ元を更新しても next build 済みのページは古い値のままだった。
revalidateTag("posts", "max") で更新されるのはタグを付けた /tagged だけで、呼んだ直後のアクセスは古い値、次のアクセスから新しい値になった。
タグの無い /untagged は revalidatePath で更新された。
Pages Router の API ルートから revalidatePath を呼ぶと 500 で落ち、res.revalidate に替えると更新された。

$ curl -s http://localhost:3000/tagged | grep -o '[a-z]*: version [0-9]*' | head -n 1
tagged: version 1
$ curl -s http://localhost:3000/untagged | grep -o '[a-z]*: version [0-9]*' | head -n 1
untagged: version 1
$ curl -s http://localhost:3000/legacy-page | grep -o '[a-z]*: version [0-9]*' | head -n 1
legacy: version 1
$ curl -s -X POST http://localhost:4000/bump
{"version":2}
$ curl -s http://localhost:3000/tagged | grep -o '[a-z]*: version [0-9]*' | head -n 1
tagged: version 1
$ curl -s http://localhost:3000/untagged | grep -o '[a-z]*: version [0-9]*' | head -n 1
untagged: version 1
$ curl -s http://localhost:3000/legacy-page | grep -o '[a-z]*: version [0-9]*' | head -n 1
legacy: version 1
$ curl -s -X POST http://localhost:3000/api/revalidate
{"revalidated":true}
$ curl -s http://localhost:3000/untagged | grep -o '[a-z]*: version [0-9]*' | head -n 1
untagged: version 1
$ curl -s http://localhost:3000/tagged | grep -o '[a-z]*: version [0-9]*' | head -n 1
tagged: version 1
$ curl -s http://localhost:3000/tagged | grep -o '[a-z]*: version [0-9]*' | head -n 1
tagged: version 2
$ curl -s -X POST http://localhost:3000/api/revalidate-old
{"revalidated":true}
$ grep -F "without the second argument" start.log
"revalidateTag" without the second argument is now deprecated, add second argument of "max" or use "updateTag". See more info here: https://nextjs.org/docs/messages/revalidate-tag-single-arg
$ curl -s -X POST http://localhost:3000/api/revalidate-path
{"revalidated":true}
$ curl -s http://localhost:3000/untagged | grep -o '[a-z]*: version [0-9]*' | head -n 1
untagged: version 2
$ curl -s -w '\n%{http_code}\n' -X POST http://localhost:3000/api/legacy
Internal Server Error
500
$ grep -F Invariant start.log
Error: Invariant: static generation store missing in revalidatePath("/legacy-page")
$ curl -s -X POST http://localhost:3000/api/legacy-isr
{"revalidated":true}
$ curl -s http://localhost:3000/legacy-page | grep -o '[a-z]*: version [0-9]*' | head -n 1
legacy: version 2

— 2026-10-20 時点の出力

検証環境Node.js 20.20.2 / npm 10.8.2 / Python 3.11.2 / 2026-10-20 検証(ほか 2 件)
検証日
実行環境
node:20 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 20.20.2
  • npm 10.8.2
  • Python 3.11.2
  • Git 2.39.5
  • Next.js 16.4.2

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. fetch にタグを付ける

// app/lib/posts.ts
export async function getPosts() {
  const res = await fetch("https://api.example.com/posts", {
    next: { tags: ["posts"] },
  });
  return res.json();
}

その上で Server Action 側で呼ぶ。

"use server";
import { revalidateTag } from "next/cache";
 
export async function createPost(formData: FormData) {
  // ... 書き込み
  revalidateTag("posts", "max");
}

revalidateTag 公式リファレンス(新しいタブで開く) のとおり、タグが一致した fetch のデータだけが古い(stale)扱いになる。
推奨の "max" は stale-while-revalidate なので、呼んだ直後の最初のリクエストは古い値を返して裏で作り直し、その後のリクエストから新しい値になる(Next.js 16.4.2 で確認)。
Next.js 16 からは第 2 引数でプロファイル(推奨は "max")を渡す形になり、1 引数の revalidateTag("posts") は非推奨で TypeScript エラーになる(15 までは 1 引数の形)。
書き込んだ本人の画面にすぐ反映したい Server Action では、代わりに updateTag("posts") を使う。
updateTag を使えない Route Handler(Webhook など)ですぐに消したいときは revalidateTag("posts", { expire: 0 }) を渡す。

2. Server Action / Route Handler 内で呼ぶ

'use client' のコンポーネントから直接 revalidatePath を import しても動かない。
必ず 'use server' 関数の中で呼び、フォームの action などから起動する。

3. Pages Router は別 API

// pages/api/revalidate.ts
export default async function handler(req, res) {
  await res.revalidate("/blog/" + req.query.slug);
  res.json({ revalidated: true });
}

App Router 用の API は pages/ 配下では使えない。

4. クライアント側の即時反映

"use client";
import { useRouter } from "next/navigation";
 
export function RefreshButton() {
  const router = useRouter();
  return <button onClick={() => router.refresh()}>更新</button>;
}

router.refresh() は現在のルートを再 fetch して Router Cache を更新する(useRouter 公式リファレンス(新しいタブで開く))。
サーバ側のキャッシュは破棄しないので、データ自体の更新には revalidatePath / revalidateTag が要る。
Cache Components を使わない場合のキャッシュと再検証の全体像は Caching and Revalidating (Previous Model)(新しいタブで開く) にまとまっている。

よくある原因

  1. fetch にタグを指定していない。
    fetch(url) のように next: { tags: [...] } を渡していないと、revalidateTag('posts') を呼んでも対応する fetch は破棄されない。
  2. Client Component から呼んでいる。
    両 API は Server Action / Route Handler 限定で、Client Component から直接 import しても実行できない。
  3. Pages Router で使っている。
    revalidatePath は App Router 専用で、Pages Router の ISR では res.revalidate('/path') を使う。
  4. クライアントの Router Cache に古いデータが残っている。
    サーバ側のキャッシュは破棄できても、クライアントが保持する Router Cache に古いデータが残ると、ナビゲーションでは更新されないことがある。