Skip to content

Commit b36ab92

Browse files
Alex KrawiecAlex Krawiec
authored andcommitted
Get custom canonical tags functioning using frontmatter
1 parent 7289691 commit b36ab92

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

app/[[...path]]/page.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ type MetadataProps = {
132132
};
133133
};
134134

135+
// Helper function to clean up canonical tags missing leading or trailing slash
136+
function checkCanonicalTagFormat(tag: string) {
137+
if (tag.charAt(0) !== '/') {
138+
tag = '/' + tag;
139+
}
140+
if (tag.charAt(tag.length - 1) !== '/') {
141+
tag = tag + '/';
142+
}
143+
return tag;
144+
}
145+
135146
export async function generateMetadata({params}: MetadataProps): Promise<Metadata> {
136147
const domain = isDeveloperDocs
137148
? 'https://develop.sentry.dev'
@@ -142,6 +153,7 @@ export async function generateMetadata({params}: MetadataProps): Promise<Metadat
142153
: domain;
143154
let title =
144155
'Sentry Docs | Application Performance Monitoring &amp; Error Tracking Software';
156+
let customCanonicalTag;
145157
let description =
146158
'Self-hosted and cloud-based application performance monitoring &amp; error tracking that helps software teams see clearer, solve quicker, &amp; learn continuously.';
147159
const images = [{url: `${previewDomain ?? domain}/meta.jpg`, width: 1200, height: 822}];
@@ -160,11 +172,17 @@ export async function generateMetadata({params}: MetadataProps): Promise<Metadat
160172
pageNode.frontmatter.title +
161173
(guideOrPlatform ? ` | Sentry for ${guideOrPlatform.title}` : '');
162174
description = pageNode.frontmatter.description ?? '';
175+
176+
if (pageNode.frontmatter.customCanonicalTag) {
177+
customCanonicalTag = pageNode.frontmatter.customCanonicalTag;
178+
}
163179
}
164180
}
165181

166182
let canonical = domain;
167-
if (params.path) {
183+
if (customCanonicalTag) {
184+
canonical = domain + checkCanonicalTagFormat(customCanonicalTag);
185+
} else if (params.path) {
168186
canonical = `${domain}/${params.path.join('/')}/`;
169187
}
170188

docs/platforms/javascript/common/configuration/options.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Basic Options
33
description: "Learn more about how to configure the SDK. These options are set when the SDK is first initialized, passed to the init function as an object."
4+
customCanonicalTag: "platforms/javascript/guides/configuration/options/"
45
sidebar_order: 1
56
---
67

src/types/frontmatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface FrontMatter {
1414
/**
1515
* A description to use in the <meta> header, as well as in auto generated page grids.
1616
*/
17+
customCanonicalTag?: string;
18+
/** Add this if you want to add a canonical tag (without this it will default to the page url). Should be a relative path without the domain (e.g. `/platforms/react/options/`) */
1719
description?: string;
1820
/**
1921
* Set this to true to mark this page as a draft, and hide it from various other components (such as the PageGrid).

0 commit comments

Comments
 (0)