This commit is contained in:
parent
6e945c49d8
commit
cbce57c173
|
|
@ -4,29 +4,43 @@ import { NextResponse, type NextRequest } from "next/server"
|
|||
import i18nConfig from "./i18nConfig"
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
// ✅ 从 Cookie 中获取语言
|
||||
const preferredLang = request.cookies.get("preferred-language")?.value
|
||||
const { pathname } = request.nextUrl
|
||||
const preferredLanguage = request.cookies.get("preferred-language")?.value
|
||||
|
||||
// ✅ 如果没有语言前缀,且 Cookie 中有语言偏好,则重定向一次
|
||||
const pathname = request.nextUrl.pathname
|
||||
const firstSegment = pathname.split("/")[1]
|
||||
const hasLocalePrefix = i18nConfig.locales.includes(firstSegment as any)
|
||||
console.log("[middleware] ⏩ Incoming request")
|
||||
console.log("[middleware] → pathname:", pathname)
|
||||
console.log("[middleware] → preferred-language from cookie:", preferredLanguage)
|
||||
|
||||
if (!hasLocalePrefix && preferredLang && i18nConfig.locales.includes(preferredLang)) {
|
||||
const newUrl = new URL(`/${preferredLang}${pathname}`, request.url)
|
||||
return NextResponse.redirect(newUrl)
|
||||
// 判断是否已包含语言前缀
|
||||
const hasLocalePrefix = i18nConfig.locales.some(
|
||||
(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)
|
||||
if (i18nResult) return i18nResult
|
||||
if (i18nResult) {
|
||||
console.log("[middleware] ✅ i18nRouter redirect applied")
|
||||
return i18nResult
|
||||
}
|
||||
|
||||
try {
|
||||
const { supabase, response } = createClient(request)
|
||||
|
||||
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) {
|
||||
const { data: homeWorkspace, error } = await supabase
|
||||
|
|
@ -37,24 +51,27 @@ export async function middleware(request: NextRequest) {
|
|||
.single()
|
||||
|
||||
if (!homeWorkspace) {
|
||||
console.error("[middleware] ❌ Home workspace not found:", error?.message)
|
||||
throw new Error(error?.message)
|
||||
}
|
||||
|
||||
return NextResponse.redirect(
|
||||
new URL(`/${homeWorkspace.id}/chat`, request.url)
|
||||
)
|
||||
const target = `/${homeWorkspace.id}/chat`
|
||||
console.log("[middleware] 🏠 Redirecting to home workspace:", target)
|
||||
|
||||
return NextResponse.redirect(new URL(target, request.url))
|
||||
}
|
||||
|
||||
return response
|
||||
} catch (e) {
|
||||
console.error("[middleware] 💥 Exception:", e)
|
||||
return NextResponse.next({
|
||||
request: {
|
||||
headers: request.headers
|
||||
}
|
||||
headers: request.headers,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: "/((?!api|static|.*\\..*|_next|auth).*)"
|
||||
matcher: "/((?!api|static|.*\\..*|_next|auth).*)",
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue