mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-06-12 07:26:47 +00:00
feat(初步实现UUID及其去除-形式重定向到slug):
This commit is contained in:
15
lib/redirect.js
Normal file
15
lib/redirect.js
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
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'
|
||||
|
||||
/**
|
||||
* Clerk 身份验证中间件
|
||||
@@ -30,8 +32,29 @@ 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 相关环境变量,返回一个默认响应或者继续处理请求
|
||||
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)
|
||||
}
|
||||
return NextResponse.next()
|
||||
}
|
||||
/**
|
||||
|
||||
@@ -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'
|
||||
|
||||
/**
|
||||
* 首页布局
|
||||
@@ -60,6 +61,8 @@ export async function getStaticProps(req) {
|
||||
generateRss(props)
|
||||
// 生成
|
||||
generateSitemapXml(props)
|
||||
// 生成重定向 JSON
|
||||
generateRedirectJson(props)
|
||||
|
||||
// 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user