From 4a3347c55cbd21fa90b0c3b852fdf3872c001048 Mon Sep 17 00:00:00 2001 From: hailin Date: Thu, 17 Apr 2025 12:02:32 +0800 Subject: [PATCH] . --- components/utility/home-redirector.tsx | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/components/utility/home-redirector.tsx b/components/utility/home-redirector.tsx index 6d8e660..82b16f8 100644 --- a/components/utility/home-redirector.tsx +++ b/components/utility/home-redirector.tsx @@ -4,13 +4,6 @@ import { useEffect } from 'react' import { useRouter } from 'next/navigation' import i18nConfig from '@/i18nConfig' - - -const isValidLocale = (locale: string): locale is (typeof i18nConfig.locales)[number] => { - return (i18nConfig.locales as readonly string[]).includes(locale) -} - - export default function HomeRedirector() { const router = useRouter() @@ -18,15 +11,25 @@ export default function HomeRedirector() { const preferred = localStorage.getItem('preferred-language') const currentPath = window.location.pathname + console.log('[HomeRedirector] currentPath:', currentPath) + console.log('[HomeRedirector] preferred-language from storage:', preferred) + + const isValidLocale = (lang: string) => + (i18nConfig.locales as string[]).includes(lang) + + // 若路径中已经有 locale 前缀,或 preferred 为空 / 非法,跳过 if ( preferred && isValidLocale(preferred) && - !currentPath.startsWith(`/${preferred}/`) + !currentPath.startsWith(`/${preferred}/`) && + !i18nConfig.locales.some(locale => currentPath.startsWith(`/${locale}/`)) ) { const newPath = `/${preferred}${currentPath}` + console.log('[HomeRedirector] Redirecting to:', newPath) router.replace(newPath) + } else { + console.log('[HomeRedirector] No redirection triggered') } - }, []) return null