This commit is contained in:
parent
b22645415b
commit
76dff6eac8
|
|
@ -5,38 +5,38 @@ import i18nConfig from "./i18nConfig"
|
|||
|
||||
export async function middleware(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl
|
||||
const preferredLanguage = request.cookies.get("preferred-language")?.value
|
||||
//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)
|
||||
//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
|
||||
}
|
||||
// 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 isOnlyLocale = i18nConfig.locales.some(locale => pathname === `/${locale}`)
|
||||
|
||||
const hasLocalePrefix = i18nConfig.locales.some(
|
||||
locale => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
|
||||
)
|
||||
console.log("[middleware] → hasLocalePrefix:", hasLocalePrefix)
|
||||
// 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)
|
||||
}
|
||||
// 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)
|
||||
// }
|
||||
|
||||
try {
|
||||
const { supabase, response } = createClient(request)
|
||||
|
|
|
|||
Loading…
Reference in New Issue