Skip to content

Commit 2698cd1

Browse files
coolguyzoneAlex Krawiec
andauthored
feat: Custom canonical tags (#11658)
* Get custom canonical tags functioning using frontmatter * Add more canonical tags and refactored code logic * Small refactor of tag format helper function for readability --------- Co-authored-by: Alex Krawiec <[email protected]>
1 parent 95fdb39 commit 2698cd1

File tree

7 files changed

+28
-4
lines changed

7 files changed

+28
-4
lines changed

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

Lines changed: 21 additions & 4 deletions
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 formatCanonicalTag(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,13 +172,18 @@ 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 = formatCanonicalTag(pageNode.frontmatter.customCanonicalTag);
178+
}
163179
}
164180
}
165181

166-
let canonical = domain;
167-
if (params.path) {
168-
canonical = `${domain}/${params.path.join('/')}/`;
169-
}
182+
const canonical = customCanonicalTag
183+
? domain + customCanonicalTag
184+
: params.path
185+
? `${domain}/${params.path.join('/')}/`
186+
: domain;
170187

171188
return {
172189
title,

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/configuration/options/"
45
sidebar_order: 1
56
---
67

docs/platforms/javascript/common/enriching-events/level/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Event Level
33
description: "The level - similar to logging levels - is generally added by default based on the integration. You can also override it within an event."
4+
customCanonicalTag: "/platforms/javascript/enriching-events/level/"
45
---
56

67
The level - similar to logging levels - is generally added by default by the SDK. You can either provide a dedicated level directly in `captureMessage`, or configure a level on the scope in order to apply it to all events.

docs/platforms/javascript/common/session-replay/privacy.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ notSupported:
1818
- javascript.nestjs
1919
- javascript.cloudflare
2020
description: "Configuring Session Replay to maintain user and data privacy."
21+
customCanonicalTag: "/platforms/javascript/session-replay/privacy/"
2122
---
2223

2324
<Include name="session-replay-web-report-bug.mdx" />

docs/platforms/javascript/common/sourcemaps/uploading/vite.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Vite
33
description: "Upload your source maps with the Sentry Vite Plugin."
4+
customCanonicalTag: "/platforms/javascript/sourcemaps/uploading/vite/"
45
sidebar_order: 3
56
---
67

docs/platforms/javascript/common/sourcemaps/uploading/webpack.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Webpack
33
description: "Upload your source maps with our webpack plugin."
4+
customCanonicalTag: "/platforms/javascript/sourcemaps/uploading/webpack/"
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)