|
1
|
export const metadata = { |
|
2
|
title: `Revalidate Cache in Next.js Starter Storefront`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this guide, you'll learn about the general approach to revalidating cache in the Next.js Starter Storefront when data is updated in the Medusa application. |
|
8
|
|
|
9
|
## Approach Overview |
|
10
|
|
|
11
|
By default, the data that the Next.js Starter Storefront retrieves from the Medusa application is cached in the browser. This cache is used to improve the performance and speed of the storefront. |
|
12
|
|
|
13
|
In some cases, you may need to revalidate the cache in the storefront when data is updated in the Medusa application. For example, when a product variant's price is updated in the Medusa application, you may want to revalidate the cache in the storefront to reflect the updated price. |
|
14
|
|
|
15
|
You're free to choose the approach that works for your use case, custom requirements, and tech stack. The approach that Medusa recommends is: |
|
16
|
|
|
17
|
1. Create a [subscriber](!docs!/learn/fundamentals/events-and-subscribers) in the Medusa application that listens for the event that triggers the data update. For example, you can listen to the `product.updated` event. |
|
18
|
2. In the subscriber, send a request to a custom endpoint in the Next.js Starter Storefront to trigger the cache revalidation. |
|
19
|
3. Create the custom endpoint in the Next.js Starter Storefront that listens for the request from the subscriber and revalidates the cache. |
|
20
|
|
|
21
|
<Note title="Tip"> |
|
22
|
|
|
23
|
Refer to the [Events Reference](/references/events) for a full list of events that the Medusa application emits. |
|
24
|
|
|
25
|
</Note> |
|
26
|
|
|
27
|
--- |
|
28
|
|
|
29
|
## Example: Revalidating Cache for Product Update |
|
30
|
|
|
31
|
Consider you want to revalidate the cache in the Next.js Starter Storefront whenever a product is updated. |
|
32
|
|
|
33
|
Start by creating the following subscriber in the Medusa application: |
|
34
|
|
|
35
|
```ts |
|
36
|
import type { |
|
37
|
SubscriberArgs, |
|
38
|
SubscriberConfig, |
|
39
|
} from "@medusajs/framework" |
|
40
|
|
|
41
|
export default async function productUpdatedHandler({ |
|
42
|
event: { data }, |
|
43
|
container, |
|
44
|
}: SubscriberArgs<{ id: string }>) { |
|
45
|
// send request to next.js storefront to revalidate cache |
|
46
|
await fetch(`${process.env.STOREFRONT_URL}/api/revalidate?tags=products`) |
|
47
|
} |
|
48
|
|
|
49
|
export const config: SubscriberConfig = { |
|
50
|
event: "product.updated", |
|
51
|
} |
|
52
|
``` |
|
53
|
|
|
54
|
In the subscriber, you send a request to the custom endpoint `/api/revalidate` in the Next.js Starter Storefront. The request includes the query parameter `tags=product-${data.id}` to specify the cache that needs to be revalidated. |
|
55
|
|
|
56
|
<Note> |
|
57
|
|
|
58
|
Make sure to set the `STOREFRONT_URL` environment variable in the Medusa application to the URL of the Next.js Starter Storefront. |
|
59
|
|
|
60
|
</Note> |
|
61
|
|
|
62
|
Then, create in the Next.js Starter Storefront the custom endpoint that listens for the request and revalidates the cache: |
|
63
|
|
|
64
|
```ts title="src/app/api/revalidate/route.ts" |
|
65
|
import { NextRequest, NextResponse } from "next/server" |
|
66
|
import { revalidatePath } from "next/cache" |
|
67
|
|
|
68
|
export async function GET(req: NextRequest) { |
|
69
|
const searchParams = req.nextUrl.searchParams |
|
70
|
const tags = searchParams.get("tags") as string |
|
71
|
|
|
72
|
if (!tags) { |
|
73
|
return NextResponse.json({ error: "No tags provided" }, { status: 400 }) |
|
74
|
} |
|
75
|
|
|
76
|
const tagsArray = tags.split(",") |
|
77
|
await Promise.all( |
|
78
|
tagsArray.map(async (tag) => { |
|
79
|
switch (tag) { |
|
80
|
case "products": |
|
81
|
revalidatePath("/[countryCode]/(main)/store", "page") |
|
82
|
revalidatePath("/[countryCode]/(main)/products/[handle]", "page") |
|
83
|
// TODO add for other tags |
|
84
|
} |
|
85
|
}) |
|
86
|
) |
|
87
|
|
|
88
|
return NextResponse.json({ message: "Revalidated" }, { status: 200 }) |
|
89
|
} |
|
90
|
``` |
|
91
|
|
|
92
|
In this example, you create a custom endpoint `/api/revalidate` that revalidates the cache for paths based on the tags passed as query parameters. |
|
93
|
|
|
94
|
You only handle the case of the `products` tag in this example, but you can extend the switch statement to handle other tags as needed. |
|
95
|
|
|
96
|
To revalidate the cache, you use Next.js's `revalidatePath` function. Learn more about in the [Next.js documentation](https://nextjs.org/docs/app/api-reference/functions/revalidatePath). |
|
97
|
|
|
98
|
### Test it Out |
|
99
|
|
|
100
|
To test out this mechanism, run the Medusa application and the Next.js Starter Storefront. |
|
101
|
|
|
102
|
Then, update a product in the Medusa application. You can see in the Next.js Starter Storefront's terminal that a request was sent to the `/api/revalidate` endpoint, meaning that the cache was revalidated successfully. |