diff --git a/app/components/Header.tsx b/app/components/Header.tsx index 441230f..62e1ea7 100644 --- a/app/components/Header.tsx +++ b/app/components/Header.tsx @@ -6,14 +6,14 @@ import { Menu, X, Mail } from "lucide-react"; import { SiGithub, SiLinkedin } from "react-icons/si"; import Link from "next/link"; import { useLocale, useTranslations } from "next-intl"; -import { usePathname, useRouter } from "next/navigation"; +import { usePathname, useSearchParams } from "next/navigation"; const Header = () => { const [isOpen, setIsOpen] = useState(false); const [scrolled, setScrolled] = useState(false); const locale = useLocale(); const pathname = usePathname(); - const router = useRouter(); + const searchParams = useSearchParams(); const t = useTranslations("nav"); const isHome = pathname === `/${locale}` || pathname === `/${locale}/`; @@ -44,23 +44,11 @@ const Header = () => { { icon: Mail, href: "mailto:contact@dk0.dev", label: "Email" }, ]; - const switchLocale = (nextLocale: string) => { - try { - const pathWithoutLocale = pathname.replace(new RegExp(`^/${locale}`), "") || ""; - const hash = typeof window !== "undefined" ? window.location.hash : ""; - // Rely on middleware to persist NEXT_LOCALE cookie. - // Use a hard navigation for maximum reliability (also fixes cases where - // client-side router navigation can be prevented by runtime errors). - const target = `/${nextLocale}${pathWithoutLocale}${hash}`; - if (typeof window !== "undefined") { - window.location.assign(target); - return; - } - router.push(target); - } catch { - // ignore - } - }; + const pathWithoutLocale = pathname.replace(new RegExp(`^/${locale}`), "") || ""; + const qs = searchParams.toString(); + const query = qs ? `?${qs}` : ""; + const enHref = `/en${pathWithoutLocale}${query}`; + const deHref = `/de${pathWithoutLocale}${query}`; // Always render to prevent flash, but use opacity transition @@ -144,9 +132,8 @@ const Header = () => {