🔎 Add canonical url (#800)

This commit is contained in:
Luke Vella 2023-08-03 12:33:20 +01:00 committed by GitHub
parent d08ed6d0f7
commit a9cee458a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,6 +11,7 @@ import { NextPage } from "next";
import { AppProps } from "next/app"; import { AppProps } from "next/app";
import { Inter } from "next/font/google"; import { Inter } from "next/font/google";
import Head from "next/head"; import Head from "next/head";
import { useRouter } from "next/router";
import { appWithTranslation } from "next-i18next"; import { appWithTranslation } from "next-i18next";
import { DefaultSeo } from "next-seo"; import { DefaultSeo } from "next-seo";
import React from "react"; import React from "react";
@ -34,6 +35,7 @@ type AppPropsWithLayout = AppProps<PageProps> & {
}; };
const MyApp: NextPage<AppPropsWithLayout> = ({ Component, pageProps }) => { const MyApp: NextPage<AppPropsWithLayout> = ({ Component, pageProps }) => {
const router = useRouter();
React.useEffect(() => { React.useEffect(() => {
if (process.env.NEXT_PUBLIC_ENABLE_ANALYTICS) { if (process.env.NEXT_PUBLIC_ENABLE_ANALYTICS) {
// calling inject directly to avoid having this run for self-hosted instances // calling inject directly to avoid having this run for self-hosted instances
@ -41,15 +43,25 @@ const MyApp: NextPage<AppPropsWithLayout> = ({ Component, pageProps }) => {
} }
}, []); }, []);
const canonicalUrl = React.useMemo(() => {
const path = router.pathname === "/" ? "" : router.pathname;
if (router.locale === router.defaultLocale) {
return absoluteUrl(path);
} else {
return absoluteUrl(`/${router.locale}${path}`);
}
}, [router.defaultLocale, router.locale, router.pathname]);
const getLayout = Component.getLayout ?? ((page) => page); const getLayout = Component.getLayout ?? ((page) => page);
return ( return (
<LazyMotion features={domMax}> <LazyMotion features={domMax}>
<DefaultSeo <DefaultSeo
canonical={canonicalUrl}
openGraph={{ openGraph={{
siteName: "Rallly", siteName: "Rallly",
type: "website", type: "website",
url: absoluteUrl(), url: canonicalUrl,
images: [ images: [
{ {
url: absoluteUrl("/og-image-1200.png"), url: absoluteUrl("/og-image-1200.png"),