This commit is contained in:
parent
cbce57c173
commit
a6eb3006a1
|
|
@ -11,15 +11,25 @@ export async function middleware(request: NextRequest) {
|
||||||
console.log("[middleware] → pathname:", pathname)
|
console.log("[middleware] → pathname:", pathname)
|
||||||
console.log("[middleware] → preferred-language from cookie:", preferredLanguage)
|
console.log("[middleware] → preferred-language from cookie:", preferredLanguage)
|
||||||
|
|
||||||
// 判断是否已包含语言前缀
|
// ✅ 1. i18nRouter 优先处理
|
||||||
|
const i18nResult = i18nRouter(request, i18nConfig)
|
||||||
|
if (i18nResult) {
|
||||||
|
console.log("[middleware] ✅ i18nRouter handled redirect")
|
||||||
|
return i18nResult
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ 2. 仅当不是 /[locale] 和 /[locale]/ 路径时才进行 cookie-based redirect
|
||||||
|
const isOnlyLocale = i18nConfig.locales.some(locale => pathname === `/${locale}`)
|
||||||
|
|
||||||
const hasLocalePrefix = i18nConfig.locales.some(
|
const hasLocalePrefix = i18nConfig.locales.some(
|
||||||
(locale) => pathname === `/${locale}` || pathname.startsWith(`/${locale}/`)
|
locale => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
|
||||||
)
|
)
|
||||||
console.log("[middleware] → hasLocalePrefix:", hasLocalePrefix)
|
console.log("[middleware] → hasLocalePrefix:", hasLocalePrefix)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
preferredLanguage &&
|
preferredLanguage &&
|
||||||
!hasLocalePrefix &&
|
!hasLocalePrefix &&
|
||||||
|
!isOnlyLocale &&
|
||||||
(i18nConfig.locales as readonly string[]).includes(preferredLanguage)
|
(i18nConfig.locales as readonly string[]).includes(preferredLanguage)
|
||||||
) {
|
) {
|
||||||
const url = request.nextUrl.clone()
|
const url = request.nextUrl.clone()
|
||||||
|
|
@ -28,18 +38,10 @@ export async function middleware(request: NextRequest) {
|
||||||
return NextResponse.redirect(url)
|
return NextResponse.redirect(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
const i18nResult = i18nRouter(request, i18nConfig)
|
|
||||||
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()
|
||||||
|
|
||||||
console.log("[middleware] 🔐 Supabase session:", session?.data?.session)
|
|
||||||
|
|
||||||
const redirectToChat = session && pathname === "/"
|
const redirectToChat = session && pathname === "/"
|
||||||
|
|
||||||
if (redirectToChat) {
|
if (redirectToChat) {
|
||||||
|
|
@ -50,15 +52,11 @@ export async function middleware(request: NextRequest) {
|
||||||
.eq("is_home", true)
|
.eq("is_home", true)
|
||||||
.single()
|
.single()
|
||||||
|
|
||||||
if (!homeWorkspace) {
|
if (!homeWorkspace) throw new Error(error?.message)
|
||||||
console.error("[middleware] ❌ Home workspace not found:", error?.message)
|
|
||||||
throw new Error(error?.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
const target = `/${homeWorkspace.id}/chat`
|
return NextResponse.redirect(
|
||||||
console.log("[middleware] 🏠 Redirecting to home workspace:", target)
|
new URL(`/${homeWorkspace.id}/chat`, request.url)
|
||||||
|
)
|
||||||
return NextResponse.redirect(new URL(target, request.url))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
@ -66,12 +64,12 @@ export async function middleware(request: NextRequest) {
|
||||||
console.error("[middleware] 💥 Exception:", 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).*)"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue