This commit is contained in:
hailin 2025-04-17 13:07:23 +08:00
parent f2130d7010
commit d6a7c26849
1 changed files with 19 additions and 7 deletions

View File

@ -4,12 +4,28 @@ import { NextResponse, type NextRequest } from "next/server"
import i18nConfig from "./i18nConfig"
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// ✅ 读取 cookie 中用户偏好的语言
const preferredLanguage = request.cookies.get('preferred-language')?.value
// ✅ 判断是否需要重定向到带有语言前缀的 URL
const isMissingLocalePrefix = !i18nConfig.locales.some(locale =>
pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
)
if (preferredLanguage && isMissingLocalePrefix) {
const url = request.nextUrl.clone()
url.pathname = `/${preferredLanguage}${pathname}`
return NextResponse.redirect(url)
}
// ✅ 原始的 i18n 路由逻辑(保留)
const i18nResult = i18nRouter(request, i18nConfig)
if (i18nResult) return i18nResult
try {
const { supabase, response } = createClient(request)
const session = await supabase.auth.getSession()
const redirectToChat = session && request.nextUrl.pathname === "/"
@ -22,9 +38,7 @@ export async function middleware(request: NextRequest) {
.eq("is_home", true)
.single()
if (!homeWorkspace) {
throw new Error(error?.message)
}
if (!homeWorkspace) throw new Error(error?.message)
return NextResponse.redirect(
new URL(`/${homeWorkspace.id}/chat`, request.url)
@ -34,9 +48,7 @@ export async function middleware(request: NextRequest) {
return response
} catch (e) {
return NextResponse.next({
request: {
headers: request.headers
}
request: { headers: request.headers }
})
}
}