Skip to content

Commit 299caa8

Browse files
committed
feat(seo): add canonical URL meta field
1 parent 6107af5 commit 299caa8

File tree

4 files changed

+10
-1
lines changed

4 files changed

+10
-1
lines changed

src/components/SEOMeta.astro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ interface Props {
44
description?: string
55
favicon?: string
66
faviconMimeType?: string
7+
canonicalURL?: string | undefined
78
keywords?: string
89
author?: string
910
robots?: string
@@ -14,16 +15,20 @@ const {
1415
description,
1516
favicon,
1617
faviconMimeType,
18+
canonicalURL,
1719
keywords,
1820
author,
1921
robots,
2022
} = Astro.props
23+
24+
const _canonicalURL = canonicalURL || Astro.url.href
2125
---
2226

2327
<!-- Global Metadata -->
2428
<meta charset="utf-8" />
2529
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
2630
<link rel="icon" type={faviconMimeType} href={favicon} />
31+
{_canonicalURL && <link rel="canonical" href={_canonicalURL} />}
2732

2833
<!-- Primary Meta Tags -->
2934
<title>{title}</title>

src/content/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const leetcodeSolutionCollection = defineCollection({
88
pubDate: z.string(),
99
difficulty: z.string(),
1010
tags: z.array(z.string()),
11+
canonicalURL: z.string().optional(),
1112
}),
1213
})
1314

src/layouts/AppLayout.astro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import siteConfig from '@/configs/site'
99
export interface Props {
1010
title: string
1111
description: string
12+
canonicalURL?: string | undefined
1213
author: string
1314
headerCssClasses?: string
1415
keywords?: string[]
1516
}
1617
17-
const { title, description = '', author, headerCssClasses = '' } = Astro.props
18+
const { title, description = '', canonicalURL, author, headerCssClasses = '' } = Astro.props
1819
const { favicon, faviconMimeType } = siteConfig
1920
const pageDescription =
2021
description.length > 0 ? description : `${title} - ${siteConfig.description}`
@@ -26,6 +27,7 @@ const pageDescription =
2627
<SEOMeta
2728
title={title}
2829
description={pageDescription}
30+
canonicalURL={canonicalURL}
2931
favicon={favicon}
3032
faviconMimeType={faviconMimeType}
3133
author={author}

src/layouts/PostLayout.astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const { author } = siteConfig
1818
headerCssClasses="max-w-5xl"
1919
title={frontmatter.title}
2020
description={frontmatter.title}
21+
canonicalURL={frontmatter.canonicalURL}
2122
keywords={[
2223
...((frontmatter.keywords as string[]) || []),
2324
...((frontmatter.tags as string[]) || []),

0 commit comments

Comments
 (0)