Skip to content

Commit 8665c61

Browse files
Add canonical tags for SEO (#8296)
* adding SEO canonical tag
1 parent 3a5ad7f commit 8665c61

File tree

9 files changed

+39
-6
lines changed

9 files changed

+39
-6
lines changed

src/components/basePage.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type PageContext = {
2121
};
2222

2323
type Props = {
24+
slug: string;
2425
children?: React.ReactNode;
2526
data?: {
2627
file?: {
@@ -46,6 +47,7 @@ export function BasePage({
4647
sidebar,
4748
children,
4849
prependToc,
50+
slug,
4951
}: Props) {
5052
const tx = getCurrentTransaction();
5153
if (tx) {
@@ -67,6 +69,7 @@ export function BasePage({
6769
title={seoTitle ?? title ?? 'Sentry Docs'}
6870
description={pageDescription}
6971
noindex={pageContext.noindex}
72+
slug={slug}
7073
/>
7174

7275
<div className="row">

src/components/seo.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const detailsQuery = graphql`
1616
`;
1717

1818
type Props = {
19+
slug: string;
1920
title: string;
2021
description?: string;
2122
keywords?: string[];
@@ -45,9 +46,16 @@ export function BaseSEO({
4546
keywords = [],
4647
title,
4748
noindex,
49+
slug,
4850
}: ChildProps) {
4951
const metaDescription = description || data.site.siteMetadata.description;
5052

53+
// slug === '' is the homepage and a valid value
54+
const canonical =
55+
data.site.siteMetadata.sitePath && (slug || slug === '')
56+
? `https://${data.site.siteMetadata.sitePath}/${slug}`
57+
: false;
58+
5159
return (
5260
<Helmet
5361
htmlAttributes={{
@@ -123,7 +131,9 @@ export function BaseSEO({
123131
: []
124132
)
125133
.concat(meta)}
126-
/>
134+
>
135+
{canonical && <link rel="canonical" href={canonical} />}
136+
</Helmet>
127137
);
128138
}
129139

src/pages/404.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function NotFoundPage() {
1616
<SEO
1717
title="Page Not Found"
1818
description="We couldn’t find the page you were looking for."
19+
slug="404/"
1920
/>
2021
<h1>Page Not Found</h1>
2122
<p>We couldn&apos;t find the page you were looking for.</p>

src/pages/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function IndexPage() {
6060

6161
return (
6262
<div className="index-wrapper">
63-
<SEO title="Sentry Documentation" />
63+
<SEO title="Sentry Documentation" slug="" />
6464
<div className="hero-section">
6565
<div className="index-container">
6666
<div className="index-navbar">

src/templates/apiDoc.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ export default function ApiDoc(props) {
1010
const {
1111
data: {allOpenApi, apiDescription},
1212
} = props;
13+
14+
// remove leading '/'
15+
const slug = props.location.pathname.replace(/^\//, '');
16+
1317
return (
14-
<BasePage sidebar={<ApiSidebar />} {...props}>
18+
<BasePage sidebar={<ApiSidebar />} slug={slug} {...props}>
1519
{apiDescription && <Content file={apiDescription} />}
1620
<ul data-noindex>
1721
{allOpenApi.edges.map(({node: {path}}) => (

src/templates/apiPage.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,14 @@ export default function ApiPage(props) {
172172
? ['RESPONSE', 'SCHEMA']
173173
: ['RESPONSE'];
174174

175+
// remove leading '/'
176+
const slug = props.location.pathname.replace(/^\//, '');
177+
175178
useEffect(() => {
176179
Prism.highlightAll();
177180
}, []);
178181
return (
179-
<BasePage sidebar={<ApiSidebar />} {...props}>
182+
<BasePage sidebar={<ApiSidebar />} slug={slug} {...props}>
180183
<div className="row">
181184
<div className="col-12">
182185
<div className="api-block">

src/templates/doc.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ export default function Doc(props: any) {
1313
} else if (props.path.startsWith('/contributing/')) {
1414
sidebar = <InternalDocsSidebar />;
1515
}
16+
17+
// remove leading '/'
18+
const slug = props.location.pathname.replace(/^\//, '');
19+
1620
return (
17-
<BasePage sidebar={sidebar} {...props}>
21+
<BasePage sidebar={sidebar} slug={slug} {...props}>
1822
<Content file={props.data.file} />
1923
</BasePage>
2024
);

src/templates/platform.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ type Props = {
1010
data: {
1111
file: any;
1212
};
13+
location: {
14+
pathname: string;
15+
};
1316
pageContext: {
1417
platform: {
1518
name: string;
@@ -36,9 +39,14 @@ export default function Platform(props: Props) {
3639
: `${props.pageContext.title} for ${
3740
(props.pageContext.guide || props.pageContext.platform).title
3841
}`;
42+
43+
// remove leading '/'
44+
const slug = props.location.pathname.replace(/^\//, '');
45+
3946
return (
4047
<BasePage
4148
{...props}
49+
slug={slug}
4250
seoTitle={seoTitle}
4351
prependToc={<PlatformSdkDetail />}
4452
sidebar={

src/templates/wizardDebugIndex.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default function WizardDebug({
1010
},
1111
}) {
1212
return (
13-
<BasePage pageContext={{title: 'Wizard Previews'}}>
13+
<BasePage pageContext={{title: 'Wizard Previews'}} slug="wizard-debug/">
1414
<ul>
1515
{nodes
1616
.sort((a, b) =>

0 commit comments

Comments
 (0)