Skip to content

Caching and revalidation

Cache what's the same for every shopper, never what belongs to one, and let the store's webhooks refresh a page the moment a merchant changes it.

Next.jsStorefront API2 min read

Two kinds of read

  • The same for everyone
    The catalog, search, the store's content and its forms. Safe to cache, and the options you pass (publicReads, next, with()) only ever reach these.
  • About one shopper
    Carts, accounts, orders and bookings. The server client always sends them no-store, whatever you pass, so a cache can never show one shopper's to another.

Tag your reads

On a server, make one client per kind. Next.js keeps the catalog's answers under the tags you give, for as long as you say:

lib/storefront.ts
import { createServerStorefront } from "@snizu/storefront/server";
import { cookies } from "next/headers";

export function catalog(locale: string, tags: string[], revalidate = 3600) {
  return createServerStorefront({ publishableKey, locale, next: { revalidate, tags } });
}

export async function shopper(locale: string) {
  return createServerStorefront({ publishableKey, locale, cookies: await cookies() });
}
app/[locale]/products/[handle]/page.tsx
const product = await catalog(locale, ["products", `product:${handle}`]).catalog.product(handle);

Refresh when the store changes

Point one of the store's webhooks at a route of yours, subscribed to product.*, collection.* and inventory_level.updated. Check its signature on the raw body, then refresh only the tags the event names:

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

export async function POST(request: Request) {
  const body = await request.text();
  if (!verifySignature(body, request.headers.get("shop-signature"), process.env.SNIZU_WEBHOOK_SECRET!)) {
    return Response.json({ error: "Bad signature." }, { status: 401 });
  }
  const event = JSON.parse(body);
  const tags = tagsFor(event); // product.updated → ["products", "product:oud-noir"]
  for (const tag of tags) revalidateTag(tag, { expire: 0 });
  return Response.json({ revalidated: tags });
}
  • product.*
    products, and product:<handle> for its handle and its previous one.
  • collection.*
    collections, and collection:<handle> the same way.
  • inventory_level.updated
    stock.

The Next.js starter has this route, verifySignature and tagsFor written out, with tests. Verify webhook signatures explains the signature.

Changes in server components

Cookies can't change while a server component renders, so a cart change made there lasts for that request only. Make changes in server actions or route handlers, where the client writes the new cookie.

Was this page helpful?

Updated 25 Nov 2026