[Prerendered](/docs/app/glossary#prerendering) pages load fast, but the data behind them can change and become stale. For example, a product's price changes, a description gets updated, new inventory arrives. You need a way to refresh cached content without rebuilding and redeploying your entire app.
Thisguidecoverstwo[revalidation](/docs/app/glossary#revalidation) strategies: **periodic revalidation** to refresh data on a time-based schedule, and **on-demand revalidation** to expire specific cache entries the moment data changes.
## Example
We'll take an existing product page that already uses [`'usecache'`](/docs/app/api-reference/directives/use-cache) and [`generateStaticParams`](/docs/app/api-reference/functions/generate-static-params), and add a cache lifetime so product data refreshes daily. Then we'lladdcachetagssoindividualproductscanberevalidatedinstantlythrougha[RouteHandler](/docs/app/getting-started/route-handlers).
[`cacheLife`](/docs/app/api-reference/functions/cacheLife)acceptsanamedprofilethatconfigureshowlongcacheddatastaysfreshbeforerevalidatinginthebackground.Ifyoudon't specify a profile, Next.js uses `default`. The built-in profiles are:
| Profile | Stale | Revalidate | Expire |
| ----------- | ----- | ---------- | ------ |
| `default` | 5m | 15m | 1y |
| `'seconds'` | 30s | 1s | 1m |
| `'minutes'` | 5m | 1m | 1h |
| `'hours'` | 5m | 1h | 1d |
| `'days'` | 5m | 1d | 1w |
| `'weeks'` | 5m | 1w | 30d |
| `'max'` | 5m | 30d | 1y |
With `cacheLife('days')`, the data is served from cache immediately, [revalidated](/docs/app/glossary#revalidation) in the background after a day, and expires after a week. This means the first visitor after the revalidation window gets a stale response while a fresh one is generated, subsequent visitors get the updated data.
If we run [`next build`](/docs/app/api-reference/cli/next#next-build-options), the route table reflects the new lifetime:
```bash filename="Terminal"
Route (app) Revalidate Expire
├ ◐ /products/[slug] 1d 1w
└ ◐ /_not-found
◐ (Partial Prerender) prerendered as static HTML with dynamic server-streamed content
```
### Step 2: Tag and revalidate on demand
Periodic revalidation works for data that changes on a predictable schedule. But sometimes you want data to be refreshed immediately, for example, when a product'spriceisupdatedinyourCMS,youdon't want to wait up to a day for the change to appear.
#### Cache tags
Import [`cacheTag`](/docs/app/api-reference/functions/cacheTag) and call it inside the cached function to label the cache entry:
```tsx filename="app/products/data.ts"
import { cacheLife, cacheTag } from 'next/cache'
export async function getProduct(slug: string): Promise<Product> {
`cacheTag` attaches one or more string labels to a cache entry. These tags don'taffectcachingbehaviorontheirown.Theyexistsoyoucantargetspecificentriesforinvalidationlater.
export async function POST(request: NextRequest) {
const { tag } = await request.json()
revalidateTag(tag, 'days')
return Response.json({ revalidated: true, tag })
}
```
WhenthisendpointreceivesaPOSTrequest,itmarksallcacheentriesmatchingthegiventagasexpired.Thesecondargumentto`revalidateTag`setsthe[cachelifetimeprofile](#cache-lifetime-profiles) for the refreshed entry. The next visitor to that page triggers a fresh render with updated data.
Youcantestthislocallywith`curl`:
```bashfilename="Terminal"
curl-XPOSThttp://localhost:3000/api/revalidate\
-H'Content-Type: application/json'\
-d'{"tag": "product-shorts"}'
```
This returns:
```json
{"revalidated":true,"tag":"product-shorts"}
```
The next request to `/products/shorts` will return fresh data, you'll see the `updatedAt` timestamp on the product page change.
In production, a CMS would call this endpoint via a webhook when content changes.
### Step 3: Secure the revalidation endpoint
The Route Handler is currently public. Anyone who knows the URL can invalidate your cache.
To prevent this, check for a secret token before processing the request. For example, read the `Authorization` header and compare it against a server-side environment variable. If the token doesn't match, return a `401` error: