This commit is contained in:
parent
76dff6eac8
commit
2b5bbd570a
|
|
@ -4,45 +4,15 @@ import { NextResponse, type NextRequest } from "next/server"
|
|||
import i18nConfig from "./i18nConfig"
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl
|
||||
//const preferredLanguage = request.cookies.get("preferred-language")?.value
|
||||
|
||||
console.log("[middleware] ⏩ Incoming request")
|
||||
console.log("[middleware] → pathname:", pathname)
|
||||
//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(
|
||||
// locale => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
|
||||
// )
|
||||
// console.log("[middleware] → hasLocalePrefix:", hasLocalePrefix)
|
||||
|
||||
// if (
|
||||
// preferredLanguage &&
|
||||
// !hasLocalePrefix &&
|
||||
// !isOnlyLocale &&
|
||||
// (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)
|
||||
// }
|
||||
const i18nResult = i18nRouter(request, i18nConfig)
|
||||
if (i18nResult) return i18nResult
|
||||
|
||||
try {
|
||||
const { supabase, response } = createClient(request)
|
||||
|
||||
const session = await supabase.auth.getSession()
|
||||
|
||||
const redirectToChat = session && pathname === "/"
|
||||
const redirectToChat = session && request.nextUrl.pathname === "/"
|
||||
|
||||
if (redirectToChat) {
|
||||
const { data: homeWorkspace, error } = await supabase
|
||||
|
|
@ -52,7 +22,9 @@ 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)
|
||||
|
|
@ -61,7 +33,6 @@ export async function middleware(request: NextRequest) {
|
|||
|
||||
return response
|
||||
} catch (e) {
|
||||
console.error("[middleware] 💥 Exception:", e)
|
||||
return NextResponse.next({
|
||||
request: {
|
||||
headers: request.headers
|
||||
|
|
|
|||
Loading…
Reference in New Issue