From dd890e413e05896472c263880be8fe2191297baa Mon Sep 17 00:00:00 2001 From: Vincenzo Marcovecchio <55591572+VincenzoMarcovecchio@users.noreply.github.com> Date: Sun, 6 Dec 2020 21:27:19 +0100 Subject: [PATCH] Index page to have a locale path Hey man how's going I noticed my home page (on the very first load ) was not accessible to language switching, I thought to make a fix for that, probably not the best way to do it but it does the job over here. Cheers. --- src/pages/[locale]/[slug].tsx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/pages/[locale]/[slug].tsx b/src/pages/[locale]/[slug].tsx index 9394e47..d83cfe2 100644 --- a/src/pages/[locale]/[slug].tsx +++ b/src/pages/[locale]/[slug].tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { NextPage, GetStaticProps } from 'next'; import Head from 'next/head'; import { Locale } from 'types/app'; @@ -8,6 +8,7 @@ import { localisedStaticPathsGetter } from 'utils/page'; import { getGeneralSettings } from 'models/settings'; import { getPage } from 'models/pages'; import pageComponents from 'page-components/index'; +import { useRouter } from 'next/router'; interface Props { locale: Locale; @@ -20,7 +21,15 @@ const Page: NextPage = ({ slug, locale }) => { const { basePageTitle } = getGeneralSettings(); const { title } = attributes; const PageComponent = pageComponents[slug] || pageComponents.default; + const router = useRouter(); + + useEffect(() => { + if (router.pathname === '/') { + router.push(`${locale}`); + } + }, []); + return ( <>