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 pathWithoutLocale = pathname.replace(new RegExp(`^/${locale}`), "") || "";
|
||||||
const hash = typeof window !== "undefined" ? window.location.hash : "";
|
const hash = typeof window !== "undefined" ? window.location.hash : "";
|
||||||
// Rely on middleware to persist NEXT_LOCALE cookie.
|
// Rely on middleware to persist NEXT_LOCALE cookie.
|
||||||
// Avoid setting cookies from the client here; in some environments this can throw
|
// Use a hard navigation for maximum reliability (also fixes cases where
|
||||||
// and prevent navigation.
|
// client-side router navigation can be prevented by runtime errors).
|
||||||
router.push(`/${nextLocale}${pathWithoutLocale}${hash}`);
|
const target = `/${nextLocale}${pathWithoutLocale}${hash}`;
|
||||||
router.refresh();
|
if (typeof window !== "undefined") {
|
||||||
|
window.location.assign(target);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
router.push(target);
|
||||||
} catch {
|
} catch {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user