読み方のヒント

矢印ボタン(または ← → キー)でページを移動できます。

第3節 変えたときに捨てる

3.1 updateTag で捨てる

商品を変える Server Actions の中で、名札の付いた結果を捨てます。

// src/app/admin/products/actions.ts
"use server";

import { updateTag } from "next/cache";
import { requireStaff } from "@/lib/auth";

export async function updateProductPrice(productId: string, price: number) {
  await requireStaff();
  await db.products.update(productId, { price });
  updateTag(`product-${productId}`);
  updateTag("products");
}

updateTag は、その名札の付いた結果をすぐに捨て、同じ操作の中で新しい結果を読ませます。価格を変えた晴さんが、そのまま商品のページを見ると、新しい価格が表示されます。

名札付いている関数捨てると新しくなる画面
product-p002getProduct("p002")クロワッサンのページ
productsgetProducts()・getProduct(...)商品一覧、すべての商品のページ

11 / 17