Download next.js/examples/cms-wordpress/src/app/api/revalidate/route.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/cms-wordpress/src/app/api/revalidate/route.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/examples/cms-wordpress/src/app/api/revalidate/route.ts
-
curl -L -o route.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/cms-wordpress/src/app/api/revalidate/route.ts
1.18 kB
| import { NextRequest, NextResponse } from "next/server"; | |
| import { revalidatePath, revalidateTag } from "next/cache"; | |
| export async function PUT(request: NextRequest) { | |
| const requestBody = await request.text(); | |
| const { paths, tags } = requestBody | |
| ? JSON.parse(requestBody) | |
| : { paths: [], tags: [] }; | |
| let revalidated = false; | |
| if ( | |
| request.headers.get("X-Headless-Secret-Key") !== process.env.HEADLESS_SECRET | |
| ) { | |
| return NextResponse.json({ message: "Invalid secret" }, { status: 401 }); | |
| } | |
| try { | |
| if (paths && Array.isArray(paths) && paths.length > 0) { | |
| Promise.all(paths.map((path) => revalidatePath(path))); | |
| console.log("Revalidated paths:", paths); | |
| revalidated = true; | |
| } | |
| if (tags && Array.isArray(tags) && tags.length > 0) { | |
| Promise.all(tags.map((tag) => revalidateTag(tag))); | |
| console.log("Revalidated tags:", tags); | |
| revalidated = true; | |
| } | |
| return NextResponse.json({ | |
| revalidated, | |
| now: Date.now(), | |
| paths, | |
| tags: tags, | |
| }); | |
| } catch (error) { | |
| return NextResponse.json( | |
| { message: "Error revalidating paths or tags" }, | |
| { status: 500 }, | |
| ); | |
| } | |
| } | |