Skip to content

Commit

Permalink
Merge branch 'pr/qixing-jk/3113'
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Jan 5, 2025
2 parents c13a102 + 704a4eb commit 5b115b7
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 3 deletions.
345 changes: 345 additions & 0 deletions blog.config.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const siteConfig = (key, defaultVal = null, extendConfig = {}) => {
case 'AI_SUMMARY_KEY':
case 'AI_SUMMARY_CACHE_TIME':
case 'AI_SUMMARY_WORD_LIMIT':
case 'UUID_REDIRECT':
// LINK比较特殊,
if (key === 'LINK') {
if (!extendConfig || Object.keys(extendConfig).length === 0) {
Expand Down
15 changes: 15 additions & 0 deletions lib/redirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import fs from 'fs'

export function generateRedirectJson({ allPages }) {
let uuidSlugMap = {}
allPages.forEach(page => {
if (page.type === 'Post' && page.status === 'Published') {
uuidSlugMap[page.id] = page.slug
}
})
try {
fs.writeFileSync('./public/redirect.json', JSON.stringify(uuidSlugMap))
} catch (error) {
console.warn('无法写入文件', error)
}
}
30 changes: 28 additions & 2 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
import { NextResponse } from 'next/server'
import { NextRequest, NextResponse } from 'next/server'
import { checkStrIsNotionId, getLastPartOfUrl } from '@/lib/utils'
import { idToUuid } from 'notion-utils'
import BLOG from './blog.config'

/**
* Clerk 身份验证中间件
Expand Down Expand Up @@ -30,8 +33,31 @@ const isTenantAdminRoute = createRouteMatcher([
* @returns
*/
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
const noAuthMiddleware = async (req: any, ev: any) => {
const noAuthMiddleware = async (req: NextRequest, ev: any) => {
// 如果没有配置 Clerk 相关环境变量,返回一个默认响应或者继续处理请求
if (BLOG['UUID_REDIRECT']) {
let redirectJson: Record<string, string> = {}
try {
const response = await fetch(`${req.nextUrl.origin}/redirect.json`)
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, 308)
}
}
return NextResponse.next()
}
/**
Expand Down
5 changes: 5 additions & 0 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { generateRobotsTxt } from '@/lib/robots.txt'
import { generateRss } from '@/lib/rss'
import { generateSitemapXml } from '@/lib/sitemap.xml'
import { DynamicLayout } from '@/themes/theme'
import { generateRedirectJson } from '@/lib/redirect'

/**
* 首页布局
Expand Down Expand Up @@ -60,6 +61,10 @@ export async function getStaticProps(req) {
generateRss(props)
// 生成
generateSitemapXml(props)
if (siteConfig('UUID_REDIRECT', false, props?.NOTION_CONFIG)) {
// 生成重定向 JSON
generateRedirectJson(props)
}

// 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build'

Expand Down
2 changes: 1 addition & 1 deletion themes/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useRouter } from 'next/router'
import { getQueryParam, getQueryVariable, isBrowser } from '../lib/utils'

// 在next.config.js中扫描所有主题
export const { THEMES = [] } = getConfig().publicRuntimeConfig
export const { THEMES = [] } = getConfig()?.publicRuntimeConfig || {}

/**
* 获取主题配置
Expand Down

0 comments on commit 5b115b7

Please sign in to comment.