feat(支持配置UUID及其去除-形式重定向到slug功能):

This commit is contained in:
anime
2025-01-03 00:48:53 +08:00
parent e405ab9dd7
commit daa47e5203
3 changed files with 31 additions and 28 deletions

View File

@@ -271,16 +271,11 @@ const BLOG = {
STARRY_SKY: process.env.NEXT_PUBLIC_STARRY_SKY || false, // 开关 STARRY_SKY: process.env.NEXT_PUBLIC_STARRY_SKY || false, // 开关
// AI 文章摘要生成 // AI 文章摘要生成
AI_SUMMARY_API: AI_SUMMARY_API: process.env.AI_SUMMARY_API || '',
process.env.AI_SUMMARY_API|| AI_SUMMARY_KEY: process.env.AI_SUMMARY_KEY || '',
'',
AI_SUMMARY_KEY:
process.env.AI_SUMMARY_KEY ||
'',
AI_SUMMARY_CACHE_TIME: process.env.AI_SUMMARY_CACHE_TIME || 1800, // 缓存时间,单位秒 AI_SUMMARY_CACHE_TIME: process.env.AI_SUMMARY_CACHE_TIME || 1800, // 缓存时间,单位秒
AI_SUMMARY_WORD_LIMIT: process.env.AI_SUMMARY_WORD_LIMIT || 1000, AI_SUMMARY_WORD_LIMIT: process.env.AI_SUMMARY_WORD_LIMIT || 1000,
// ********挂件组件相关******** // ********挂件组件相关********
// AI 文章摘要生成 @see https://docs_s.tianli0.top/ // AI 文章摘要生成 @see https://docs_s.tianli0.top/
TianliGPT_CSS: TianliGPT_CSS:
@@ -532,6 +527,9 @@ const BLOG = {
MAILCHIMP_LIST_ID: process.env.MAILCHIMP_LIST_ID || null, // 开启mailichimp邮件订阅 客户列表ID ,具体使用方法参阅文档 MAILCHIMP_LIST_ID: process.env.MAILCHIMP_LIST_ID || null, // 开启mailichimp邮件订阅 客户列表ID ,具体使用方法参阅文档
MAILCHIMP_API_KEY: process.env.MAILCHIMP_API_KEY || null, // 开启mailichimp邮件订阅 APIkey MAILCHIMP_API_KEY: process.env.MAILCHIMP_API_KEY || null, // 开启mailichimp邮件订阅 APIkey
// uuid重定向至 slug
UUID_REDIRECT: process.env.UUID_REDIRECT || false,
// ANIMATE.css 动画 // ANIMATE.css 动画
ANIMATE_CSS_URL: ANIMATE_CSS_URL:
process.env.NEXT_PUBLIC_ANIMATE_CSS_URL || process.env.NEXT_PUBLIC_ANIMATE_CSS_URL ||

View File

@@ -2,6 +2,7 @@ import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
import { checkStrIsNotionId, getLastPartOfUrl } from '@/lib/utils' import { checkStrIsNotionId, getLastPartOfUrl } from '@/lib/utils'
import { idToUuid } from 'notion-utils' import { idToUuid } from 'notion-utils'
import { siteConfig } from '@/lib/config'
/** /**
* Clerk 身份验证中间件 * 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 // 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) => { const noAuthMiddleware = async (req: NextRequest, ev: any) => {
// 如果没有配置 Clerk 相关环境变量,返回一个默认响应或者继续处理请求 // 如果没有配置 Clerk 相关环境变量,返回一个默认响应或者继续处理请求
let redirectJson: Record<string, string> = {} if (siteConfig('UUID_REDIRECT')) {
try { let redirectJson: Record<string, string> = {}
const response = await fetch(`${req.nextUrl.origin}/redirect.json`) try {
if (response.ok) { const response = await fetch(`${req.nextUrl.origin}/redirect.json`)
redirectJson = (await response.json()) as Record<string, string> if (response.ok) {
redirectJson = (await response.json()) as Record<string, string>
}
} 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() return NextResponse.next()
} }

View File

@@ -61,8 +61,10 @@ export async function getStaticProps(req) {
generateRss(props) generateRss(props)
// 生成 // 生成
generateSitemapXml(props) generateSitemapXml(props)
// 生成重定向 JSON if (siteConfig('UUID_REDIRECT', false, props?.NOTION_CONFIG)) {
generateRedirectJson(props) // 生成重定向 JSON
generateRedirectJson(props)
}
// 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build' // 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build'