mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-15 15:09:25 +00:00
feat(初步实现UUID及其去除-形式重定向到slug):
This commit is contained in:
@@ -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()
|
||||
}
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user