From 2e957dbd91cf5fcdd8e1ea80f0b04dda23925deb Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Mon, 17 Oct 2022 14:35:51 +0200 Subject: [PATCH] keep language prefix when doing client-side refresh (#31686) Co-authored-by: Robert Sese <734194+rsese@users.noreply.github.com> --- components/ClientSideRefresh.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/ClientSideRefresh.tsx b/components/ClientSideRefresh.tsx index 88699e48a2..156c68c827 100644 --- a/components/ClientSideRefresh.tsx +++ b/components/ClientSideRefresh.tsx @@ -11,7 +11,11 @@ export default function ClientSideRefresh() { useSWR( router.asPath, () => { - router.replace(router.asPath, undefined, { scroll: false }) + // Remember, in NextJS, the `router.locale` is never including the + // `router.asPath`. So we have to make sure it's always there + // otherwise, after this hook runs, you lose that `/en` prefix + // in the URL on the address bar. + router.replace(`/${router.locale}${router.asPath}`, undefined, { scroll: false }) }, { // Implied here is that `revalidateOnFocus: true` which the default