docs: fix revalidate cache guide (#12243)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -63,8 +63,7 @@ Then, create in the Next.js Starter Storefront the custom endpoint that listens
|
||||
|
||||
```ts title="src/app/api/revalidate/route.ts"
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { revalidateTag } from "next/cache"
|
||||
import { getCacheTag } from "../../../lib/data/cookies"
|
||||
import { revalidatePath } from "next/cache"
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const searchParams = req.nextUrl.searchParams
|
||||
@@ -77,12 +76,27 @@ export async function GET(req: NextRequest) {
|
||||
const tagsArray = tags.split(",")
|
||||
await Promise.all(
|
||||
tagsArray.map(async (tag) => {
|
||||
const cacheTag = await getCacheTag(tag)
|
||||
// revalidate cache for the tag
|
||||
revalidateTag(cacheTag)
|
||||
switch (tag) {
|
||||
case "products":
|
||||
revalidatePath("/[countryCode]/(main)/store", "page")
|
||||
revalidatePath("/[countryCode]/(main)/products/[handle]", "page")
|
||||
// TODO add for other tags
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return NextResponse.json({ message: "Revalidated" }, { status: 200 })
|
||||
}
|
||||
```
|
||||
|
||||
In this example, you create a custom endpoint `/api/revalidate` that revalidates the cache for paths based on the tags passed as query parameters.
|
||||
|
||||
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.
|
||||
|
||||
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).
|
||||
|
||||
### Test it Out
|
||||
|
||||
To test out this mechanism, run the Medusa application and the Next.js Starter Storefront.
|
||||
|
||||
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.
|
||||
@@ -6055,7 +6055,7 @@ export const generatedEditDates = {
|
||||
"references/modules/file_service/page.mdx": "2025-03-17T15:24:03.025Z",
|
||||
"references/modules/notification_service/page.mdx": "2025-03-17T15:24:05.164Z",
|
||||
"references/notification_service/interfaces/notification_service.INotificationModuleService/page.mdx": "2025-04-11T09:04:49.209Z",
|
||||
"app/nextjs-starter/guides/revalidate-cache/page.mdx": "2025-03-18T08:47:59.628Z",
|
||||
"app/nextjs-starter/guides/revalidate-cache/page.mdx": "2025-04-18T15:24:15.919Z",
|
||||
"app/storefront-development/cart/totals/page.mdx": "2025-03-27T14:47:14.252Z",
|
||||
"app/storefront-development/checkout/order-confirmation/page.mdx": "2025-03-27T14:29:45.669Z",
|
||||
"app/how-to-tutorials/tutorials/product-reviews/page.mdx": "2025-04-17T08:48:08.716Z",
|
||||
|
||||
Reference in New Issue
Block a user