This commit is contained in:
hailin 2025-04-17 13:23:01 +08:00
parent 6e945c49d8
commit cbce57c173
1 changed files with 36 additions and 19 deletions

View File

@ -4,29 +4,43 @@ import { NextResponse, type NextRequest } from "next/server"
import i18nConfig from "./i18nConfig" import i18nConfig from "./i18nConfig"
export async function middleware(request: NextRequest) { export async function middleware(request: NextRequest) {
// ✅ 从 Cookie 中获取语言 const { pathname } = request.nextUrl
const preferredLang = request.cookies.get("preferred-language")?.value const preferredLanguage = request.cookies.get("preferred-language")?.value
// ✅ 如果没有语言前缀,且 Cookie 中有语言偏好,则重定向一次 console.log("[middleware] ⏩ Incoming request")
const pathname = request.nextUrl.pathname console.log("[middleware] → pathname:", pathname)
const firstSegment = pathname.split("/")[1] console.log("[middleware] → preferred-language from cookie:", preferredLanguage)
const hasLocalePrefix = i18nConfig.locales.includes(firstSegment as any)
if (!hasLocalePrefix && preferredLang && i18nConfig.locales.includes(preferredLang)) { // 判断是否已包含语言前缀
const newUrl = new URL(`/${preferredLang}${pathname}`, request.url) const hasLocalePrefix = i18nConfig.locales.some(
return NextResponse.redirect(newUrl) (locale) => pathname === `/${locale}` || pathname.startsWith(`/${locale}/`)
)
console.log("[middleware] → hasLocalePrefix:", hasLocalePrefix)
if (
preferredLanguage &&
!hasLocalePrefix &&
(i18nConfig.locales as readonly string[]).includes(preferredLanguage)
) {
const url = request.nextUrl.clone()
url.pathname = `/${preferredLanguage}${pathname}`
console.log("[middleware] 🚀 Redirecting to preferred language:", url.pathname)
return NextResponse.redirect(url)
} }
// 👇 这个要留着,用于 next-i18n-router 自动 locale 路由处理
const i18nResult = i18nRouter(request, i18nConfig) const i18nResult = i18nRouter(request, i18nConfig)
if (i18nResult) return i18nResult if (i18nResult) {
console.log("[middleware] ✅ i18nRouter redirect applied")
return i18nResult
}
try { try {
const { supabase, response } = createClient(request) const { supabase, response } = createClient(request)
const session = await supabase.auth.getSession() const session = await supabase.auth.getSession()
const redirectToChat = session && request.nextUrl.pathname === "/" console.log("[middleware] 🔐 Supabase session:", session?.data?.session)
const redirectToChat = session && pathname === "/"
if (redirectToChat) { if (redirectToChat) {
const { data: homeWorkspace, error } = await supabase const { data: homeWorkspace, error } = await supabase
@ -37,24 +51,27 @@ export async function middleware(request: NextRequest) {
.single() .single()
if (!homeWorkspace) { if (!homeWorkspace) {
console.error("[middleware] ❌ Home workspace not found:", error?.message)
throw new Error(error?.message) throw new Error(error?.message)
} }
return NextResponse.redirect( const target = `/${homeWorkspace.id}/chat`
new URL(`/${homeWorkspace.id}/chat`, request.url) console.log("[middleware] 🏠 Redirecting to home workspace:", target)
)
return NextResponse.redirect(new URL(target, request.url))
} }
return response return response
} catch (e) { } catch (e) {
console.error("[middleware] 💥 Exception:", e)
return NextResponse.next({ return NextResponse.next({
request: { request: {
headers: request.headers headers: request.headers,
} },
}) })
} }
} }
export const config = { export const config = {
matcher: "/((?!api|static|.*\\..*|_next|auth).*)" matcher: "/((?!api|static|.*\\..*|_next|auth).*)",
} }