From a24d269391dd15bb0dbf25af7306b74f7ed6b385 Mon Sep 17 00:00:00 2001 From: Neko Date: Sun, 1 May 2022 12:24:18 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=88=20perf(revalidate):=20remove=20rev?= =?UTF-8?q?alidate=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Neko --- lib/dtos/index.ts | 1 - lib/dtos/revalidate.ts | 14 ------- pages/api/revalidate.ts | 88 ----------------------------------------- 3 files changed, 103 deletions(-) delete mode 100644 lib/dtos/revalidate.ts delete mode 100644 pages/api/revalidate.ts diff --git a/lib/dtos/index.ts b/lib/dtos/index.ts index 30b17fc..86dcf26 100644 --- a/lib/dtos/index.ts +++ b/lib/dtos/index.ts @@ -1,4 +1,3 @@ import 'reflect-metadata'; export * from './getPostLists'; -export * from './revalidate'; diff --git a/lib/dtos/revalidate.ts b/lib/dtos/revalidate.ts deleted file mode 100644 index 6ca084b..0000000 --- a/lib/dtos/revalidate.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Type } from 'class-transformer'; -import { ArrayNotEmpty, IsNotEmpty } from 'class-validator'; - -export class RevalidateHeaderDto { - @IsNotEmpty() - authorization!: string; -} - -export class RevalidateBodyDto { - @IsNotEmpty() - @ArrayNotEmpty() - @Type(() => String) - paths!: string[]; -} diff --git a/pages/api/revalidate.ts b/pages/api/revalidate.ts deleted file mode 100644 index 8d439bf..0000000 --- a/pages/api/revalidate.ts +++ /dev/null @@ -1,88 +0,0 @@ -import type { NextApiHandler } from 'next'; -import { plainToInstance } from 'class-transformer'; -import { validateOrReject, ValidationError } from 'class-validator'; -import * as fs from 'fs'; -import * as path from 'path'; - -import { RevalidateHeaderDto, RevalidateBodyDto } from 'lib/dtos'; -import type { BaseResponse } from 'lib/interfaces'; -import { BlogPostPath, getPostContent } from 'lib'; - -// TODO: now using unstable api -const handler: NextApiHandler = async (req, res) => { - if (req.method === 'POST') { - const headers = plainToInstance(RevalidateHeaderDto, req.headers); - const body = plainToInstance(RevalidateBodyDto, req.body); - - try { - await validateOrReject(headers); - await validateOrReject(body); - - if (headers.authorization !== process.env.REVALIDATE_TOKEN) { - res.status(401).json({ - statusCode: 401, - message: 'Unauthorized', - }); - } else { - let postRevalidate = false; - - const revalidatePaths = body.paths.filter((item) => { - if (item === '/friend') return true; - if (item === '/') return true; - if (item === '/archive') return true; - if (item.endsWith('.md')) { - if (fs.existsSync(path.join(BlogPostPath, path.basename(item)))) { - postRevalidate = true; - return true; - } - } - return false; - }); - console.log(`Starting revalidate paths ${revalidatePaths.join(', ')}`); - await Promise.all( - revalidatePaths.map(async (item) => { - if (item.endsWith('.md')) { - await res.unstable_revalidate( - `/post/${path.basename(item.slice(0, -3))}` - ); - const post = getPostContent(item); - if (post?.categories) { - await Promise.all( - post.categories.map(async (category) => { - await res.unstable_revalidate(`/tag/${category}`); - }) - ); - } - } else { - await res.unstable_revalidate(`${item}`); - } - }) - ); - if (postRevalidate) { - await res.unstable_revalidate('/'); - await res.unstable_revalidate('/archive'); - } - console.log('Revalidate success'); - res.status(204).send(undefined); - } - } catch (err) { - console.error('Revalidate failed'); - console.error(err); - if (err instanceof ValidationError) { - res.status(400).json({ - statusCode: 400, - message: 'Bad Request', - }); - } else { - res.status(500).json({ - statusCode: 500, - message: 'Revalidate Failed', - }); - } - } - } else { - res.status(405).setHeader('Allow', 'POST').send(undefined); - } -}; - -export default handler;