diff --git a/pages/index.tsx b/pages/index.tsx index 5c47376..afc018c 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -5,6 +5,21 @@ import { getDefaultLocale, getLocales } from "../helpers/localization"; const Page: NextPage = () => null; export const getServerSideProps: GetServerSideProps = async ({ req }) => { + if (req.url?.includes("?")) { + const searchParams = new URL(req.url, process.env.NEXT_PUBLIC_SELF_ORIGIN) + .searchParams; + const requestedLocale = getLocales().find( + (locale) => locale === searchParams.get("hl")?.toLowerCase() + ); + + return { + redirect: { + permanent: true, + destination: `/${requestedLocale}`, + }, + }; + } + const negotiator = new Negotiator(req); const locale = negotiator.language(getLocales()) ?? getDefaultLocale(); diff --git a/pages/posts/[slug].tsx b/pages/posts/[slug].tsx index a594dae..f212c2f 100644 --- a/pages/posts/[slug].tsx +++ b/pages/posts/[slug].tsx @@ -8,6 +8,21 @@ export const getServerSideProps: GetServerSideProps< {}, { slug: string } > = async ({ req, params }) => { + if (req.url?.includes("?")) { + const searchParams = new URL(req.url, process.env.NEXT_PUBLIC_SELF_ORIGIN) + .searchParams; + const requestedLocale = getLocales().find( + (locale) => locale === searchParams.get("hl")?.toLowerCase() + ); + + return { + redirect: { + permanent: true, + destination: `/${requestedLocale}/posts/${params!.slug}`, + }, + }; + } + const negotiator = new Negotiator(req); const locale = negotiator.language(getLocales()) ?? getDefaultLocale();