From 2d06bf064d4cfe143f8471449c177ca352574288 Mon Sep 17 00:00:00 2001 From: GoldElysium Date: Wed, 13 Mar 2024 16:03:43 +0100 Subject: [PATCH] fix: use revalidatePath --- src/app/api/revalidate/route.ts | 19 +++++++++++++++++++ src/pages/api/revalidate.ts | 11 ----------- 2 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 src/app/api/revalidate/route.ts delete mode 100644 src/pages/api/revalidate.ts diff --git a/src/app/api/revalidate/route.ts b/src/app/api/revalidate/route.ts new file mode 100644 index 00000000..0a7b9727 --- /dev/null +++ b/src/app/api/revalidate/route.ts @@ -0,0 +1,19 @@ +import { NextRequest, NextResponse } from 'next/server'; +import { revalidatePath } from 'next/cache'; + +// eslint-disable-next-line import/prefer-default-export +export async function POST(req: NextRequest) { + if (req.headers.get('Authorization') !== process.env.REVALIDATE_SECRET) { + return new NextResponse(undefined, { status: 401 }); + } + + const body = await req.json(); + + if (!body.path) { + return new NextResponse(undefined, { status: 400 }); + } + + revalidatePath(body.path, 'layout'); + + return new NextResponse(); +} diff --git a/src/pages/api/revalidate.ts b/src/pages/api/revalidate.ts deleted file mode 100644 index af0a4d59..00000000 --- a/src/pages/api/revalidate.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { NextApiRequest, NextApiResponse } from 'next'; - -export default async (req: NextApiRequest, res: NextApiResponse) => { - if (req.headers.authorization !== process.env.REVALIDATE_SECRET) return res.status(401).end(); - if (req.method !== 'POST') return res.status(404).end(); - - if (!req.body.path) return res.status(400).end(); - - await res.revalidate(req.body.path); - return res.status(200).end(); -};