# Caching and revalidation

Source: https://snizu.dev/docs/storefronts/caching-and-revalidation

## Two kinds of read [#two-kinds-of-read]

<Fields>
<Field name="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.
</Field>

<Field name="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.
</Field>
</Fields>

## Tag your reads [#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:

```ts title="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() });
}
```

```ts title="app/[locale]/products/[handle]/page.tsx"
const product = await catalog(locale, ["products", `product:${handle}`]).catalog.product(handle);
```

## Refresh when the store changes [#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:

```ts title="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 });
}
```

<Fields>
<Field name="product.*">
`products`, and `product:<handle>` for its handle and its previous one.
</Field>

<Field name="collection.*">
`collections`, and `collection:<handle>` the same way.
</Field>

<Field name="inventory_level.updated">
`stock`.
</Field>
</Fields>

The [Next.js starter](/docs/storefronts/nextjs-starter) has this route, `verifySignature` and `tagsFor` written out, with tests. [Verify webhook signatures](/docs/connections/webhooks/verify-signatures) explains the signature.

<Callout title="Without a webhook">
Reads still refresh on their own: the starter keeps the catalog for an hour, search for five minutes and stock for 30 seconds.
</Callout>

## Changes in server components [#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.