fix(i18n): use hard navigation for language switch
Switch locales via window.location.assign to guarantee the URL and messages update even if client-side router navigation is blocked. Co-authored-by: dennis <dennis@konkol.net>
This commit is contained in:
@@ -49,10 +49,14 @@ const Header = () => {
|
||||
const pathWithoutLocale = pathname.replace(new RegExp(`^/${locale}`), "") || "";
|
||||
const hash = typeof window !== "undefined" ? window.location.hash : "";
|
||||
// Rely on middleware to persist NEXT_LOCALE cookie.
|
||||
// Avoid setting cookies from the client here; in some environments this can throw
|
||||
// and prevent navigation.
|
||||
router.push(`/${nextLocale}${pathWithoutLocale}${hash}`);
|
||||
router.refresh();
|
||||
// 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user