Skip to content

Commit 9391138

Browse files
authored
Merge pull request #874 from IABTechLab/ibg-CORPWEB-3542-canonical-dedupe
ibg-CORPWEB-3542-canonical-dedupe
2 parents 2258e2b + 0ac7407 commit 9391138

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/theme/SiteMetadata/index.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from "react";
2+
import SiteMetadata from "@theme-original/SiteMetadata";
3+
import type SiteMetadataType from "@theme/SiteMetadata";
4+
import type { WrapperProps } from "@docusaurus/types";
5+
import Head from "@docusaurus/Head";
6+
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
7+
import { useLocation } from "react-router";
8+
9+
type Props = WrapperProps<typeof SiteMetadataType>;
10+
11+
export default function SiteMetadataWrapper(props: Props): JSX.Element {
12+
const location = useLocation();
13+
const { siteConfig } = useDocusaurusContext();
14+
15+
let canonical = new URL(location.pathname, siteConfig.url).href;
16+
// sidebar.js generates alternate pages that end in -1, -2, .etc
17+
// this canonical href fix will prevent search engines from indexing those pages as unique and instead dedupe
18+
if (canonical.substring(canonical.length - 2).match(/-[0-9]/)) {
19+
canonical = canonical.substring(0, canonical.length - 2);
20+
}
21+
22+
return (
23+
<>
24+
<SiteMetadata {...props} />
25+
<Head>
26+
<link rel="canonical" href={canonical}></link>
27+
</Head>
28+
</>
29+
);
30+
}

0 commit comments

Comments
 (0)