From 7cb5ccec855399a60199696e8e9735823071e9f7 Mon Sep 17 00:00:00 2001 From: jingrow Date: Tue, 24 Jun 2025 02:30:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86revalidate=E5=86=97=E4=BD=99?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/revalidate/route.js | 25 +-------------------- components/common/DynamicListPage/index.jsx | 2 +- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/app/api/revalidate/route.js b/app/api/revalidate/route.js index 29fbb3c..2543c79 100644 --- a/app/api/revalidate/route.js +++ b/app/api/revalidate/route.js @@ -1,52 +1,29 @@ import { revalidatePath } from 'next/cache'; import { NextRequest, NextResponse } from 'next/server'; -/** - * 按需重新验证 API 路由 - * 当后端内容更新时,通过调用此 API 来手动触发特定页面的重新生成。 - * - * 调用方式: - * POST /api/revalidate - * Headers: - * Content-Type: application/json - * x-revalidate-secret: YOUR_SECRET_TOKEN - * Body: - * { - * "path": "/page-to-revalidate" - * } - */ export async function POST(request) { - // 1. 从请求头中获取密钥 const secret = request.headers.get('x-revalidate-secret'); // 2. 验证密钥 - // 为了安全,这个密钥应该存储在环境变量中,并且不能硬编码。 if (secret !== process.env.REVALIDATE_TOKEN) { - console.warn('Invalid revalidate token received.'); return NextResponse.json({ message: 'Invalid token' }, { status: 401 }); } - // 3. 从请求体中获取并记录需要重新验证的路径 + // 3. 从请求体中获取需要重新验证的路径 const body = await request.json(); const { path } = body; - console.log('Revalidation request body:', body); - if (!path) { - console.warn('Revalidation request received without a path.'); return NextResponse.json({ message: 'Path is required' }, { status: 400 }); } try { // 4. 调用 Next.js 的 revalidatePath 函数 - // 这将清除指定路径的缓存,并在下次请求时重新生成页面。 revalidatePath(path); - console.log(`Successfully revalidated path: ${path}`); return NextResponse.json({ revalidated: true, now: Date.now() }); } catch (error) { // 5. 如果发生错误,返回错误信息 - console.error(`Error revalidating path: ${path}`, error); return NextResponse.json( { message: 'Error revalidating', error: error.message }, { status: 500 } diff --git a/components/common/DynamicListPage/index.jsx b/components/common/DynamicListPage/index.jsx index 2a1325a..e4c386b 100644 --- a/components/common/DynamicListPage/index.jsx +++ b/components/common/DynamicListPage/index.jsx @@ -1,7 +1,7 @@ import DynamicListPageUI from './DynamicListPageUI'; import { getPageData } from '@/utils/data'; -export default async function DynamicListPage({ slugArr, basePath, columns, pageSize = 10, searchParams }) { +export default async function DynamicListPage({ slugArr, basePath, columns, pageSize = 12, searchParams }) { // 先 await searchParams const resolvedSearchParams = await searchParams; // 解析当前页码