From daa47e520321d1471ca445cc686bf73ee77bf0a7 Mon Sep 17 00:00:00 2001 From: anime Date: Fri, 3 Jan 2025 00:48:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=94=AF=E6=8C=81=E9=85=8D=E7=BD=AEUUID?= =?UTF-8?q?=E5=8F=8A=E5=85=B6=E5=8E=BB=E9=99=A4-=E5=BD=A2=E5=BC=8F?= =?UTF-8?q?=E9=87=8D=E5=AE=9A=E5=90=91=E5=88=B0slug=E5=8A=9F=E8=83=BD):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog.config.js | 12 +++++------- middleware.ts | 41 ++++++++++++++++++++++------------------- pages/index.js | 6 ++++-- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/blog.config.js b/blog.config.js index 24cf06ae..379e15d3 100644 --- a/blog.config.js +++ b/blog.config.js @@ -271,16 +271,11 @@ const BLOG = { STARRY_SKY: process.env.NEXT_PUBLIC_STARRY_SKY || false, // 开关 // AI 文章摘要生成 - AI_SUMMARY_API: - process.env.AI_SUMMARY_API|| - '', - AI_SUMMARY_KEY: - process.env.AI_SUMMARY_KEY || - '', + AI_SUMMARY_API: process.env.AI_SUMMARY_API || '', + AI_SUMMARY_KEY: process.env.AI_SUMMARY_KEY || '', AI_SUMMARY_CACHE_TIME: process.env.AI_SUMMARY_CACHE_TIME || 1800, // 缓存时间,单位秒 AI_SUMMARY_WORD_LIMIT: process.env.AI_SUMMARY_WORD_LIMIT || 1000, - // ********挂件组件相关******** // AI 文章摘要生成 @see https://docs_s.tianli0.top/ TianliGPT_CSS: @@ -532,6 +527,9 @@ const BLOG = { MAILCHIMP_LIST_ID: process.env.MAILCHIMP_LIST_ID || null, // 开启mailichimp邮件订阅 客户列表ID ,具体使用方法参阅文档 MAILCHIMP_API_KEY: process.env.MAILCHIMP_API_KEY || null, // 开启mailichimp邮件订阅 APIkey + // uuid重定向至 slug + UUID_REDIRECT: process.env.UUID_REDIRECT || false, + // ANIMATE.css 动画 ANIMATE_CSS_URL: process.env.NEXT_PUBLIC_ANIMATE_CSS_URL || diff --git a/middleware.ts b/middleware.ts index c60ee9cd..6c622069 100644 --- a/middleware.ts +++ b/middleware.ts @@ -2,6 +2,7 @@ import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server' import { NextRequest, NextResponse } from 'next/server' import { checkStrIsNotionId, getLastPartOfUrl } from '@/lib/utils' import { idToUuid } from 'notion-utils' +import { siteConfig } from '@/lib/config' /** * Clerk 身份验证中间件 @@ -34,26 +35,28 @@ const isTenantAdminRoute = createRouteMatcher([ // eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars const noAuthMiddleware = async (req: NextRequest, ev: any) => { // 如果没有配置 Clerk 相关环境变量,返回一个默认响应或者继续处理请求 - let redirectJson: Record = {} - try { - const response = await fetch(`${req.nextUrl.origin}/redirect.json`) - if (response.ok) { - redirectJson = (await response.json()) as Record + if (siteConfig('UUID_REDIRECT')) { + let redirectJson: Record = {} + try { + const response = await fetch(`${req.nextUrl.origin}/redirect.json`) + if (response.ok) { + redirectJson = (await response.json()) as Record + } + } catch (err) { + console.error('Error fetching static file:', err) + } + let lastPart = getLastPartOfUrl(req.nextUrl.pathname) as string + if (checkStrIsNotionId(lastPart)) { + lastPart = idToUuid(lastPart) + } + if (lastPart && redirectJson[lastPart]) { + const redirectToUrl = req.nextUrl.clone() + redirectToUrl.pathname = '/' + redirectJson[lastPart] + console.log( + `redirect from ${req.nextUrl.pathname} to ${redirectToUrl.pathname}` + ) + return NextResponse.redirect(redirectToUrl) } - } catch (err) { - console.error('Error fetching static file:', err) - } - let lastPart = getLastPartOfUrl(req.nextUrl.pathname) as string - if (checkStrIsNotionId(lastPart)) { - lastPart = idToUuid(lastPart) - } - if (lastPart && redirectJson[lastPart]) { - const redirectToUrl = req.nextUrl.clone() - redirectToUrl.pathname = '/' + redirectJson[lastPart] - console.log( - `redirect from ${req.nextUrl.pathname} to ${redirectToUrl.pathname}` - ) - return NextResponse.redirect(redirectToUrl) } return NextResponse.next() } diff --git a/pages/index.js b/pages/index.js index a83d42c8..b66b5610 100644 --- a/pages/index.js +++ b/pages/index.js @@ -61,8 +61,10 @@ export async function getStaticProps(req) { generateRss(props) // 生成 generateSitemapXml(props) - // 生成重定向 JSON - generateRedirectJson(props) + if (siteConfig('UUID_REDIRECT', false, props?.NOTION_CONFIG)) { + // 生成重定向 JSON + generateRedirectJson(props) + } // 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build'