mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
feat: support custom canonical URLs (#83)
Adding an optional attribute in the posts' frontmatter that allows the user to set a custom canonical URL
This commit is contained in:
committed by
Sat Naing
parent
08383093d1
commit
4687bd516b
@@ -11,6 +11,7 @@ export const blogSchema = z
|
|||||||
tags: z.array(z.string()).default(["others"]),
|
tags: z.array(z.string()).default(["others"]),
|
||||||
ogImage: z.string().optional(),
|
ogImage: z.string().optional(),
|
||||||
description: z.string(),
|
description: z.string(),
|
||||||
|
canonicalURL: z.string().optional(),
|
||||||
})
|
})
|
||||||
.strict();
|
.strict();
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ Here is the list of frontmatter property for each post.
|
|||||||
| **_draft_** | Mark this post 'unpublished'. | default = false |
|
| **_draft_** | Mark this post 'unpublished'. | default = false |
|
||||||
| **_tags_** | Related keywords for this post. Written in array yaml format. | default = others |
|
| **_tags_** | Related keywords for this post. Written in array yaml format. | default = others |
|
||||||
| **_ogImage_** | OG image of the post. Useful for social media sharing and SEO. | default = SITE.ogImage or generated SVG image |
|
| **_ogImage_** | OG image of the post. Useful for social media sharing and SEO. | default = SITE.ogImage or generated SVG image |
|
||||||
| **_canonicalUrl_** | Canonical URL (absolute), in case the article already exists on other source. | default = `Astro.site` + `Astro.url.pathname` |
|
| **_canonicalURL_** | Canonical URL (absolute), in case the article already exists on other source. | default = `Astro.site` + `Astro.url.pathname` |
|
||||||
|
|
||||||
Only `title`, `description` and `pubDatetime` fields in frontmatter must be specified.
|
Only `title`, `description` and `pubDatetime` fields in frontmatter must be specified.
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ tags:
|
|||||||
- tags
|
- tags
|
||||||
ogImage: ""
|
ogImage: ""
|
||||||
description: This is the example description of the example post.
|
description: This is the example description of the example post.
|
||||||
canonicalUrl: https://example.org/my-article-was-already-posted-here
|
canonicalURL: https://example.org/my-article-was-already-posted-here
|
||||||
---
|
---
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ export interface Props {
|
|||||||
author?: string;
|
author?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
ogImage?: string;
|
ogImage?: string;
|
||||||
|
canonicalURL?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -16,10 +17,9 @@ const {
|
|||||||
author = SITE.author,
|
author = SITE.author,
|
||||||
description = SITE.desc,
|
description = SITE.desc,
|
||||||
ogImage = SITE.ogImage,
|
ogImage = SITE.ogImage,
|
||||||
|
canonicalURL = new URL(Astro.url.pathname, Astro.site).href,
|
||||||
} = Astro.props;
|
} = Astro.props;
|
||||||
|
|
||||||
const canonicalURL = new URL(Astro.url.pathname, Astro.site);
|
|
||||||
|
|
||||||
const socialImageURL = new URL(
|
const socialImageURL = new URL(
|
||||||
ogImage ? ogImage : SITE.ogImage,
|
ogImage ? ogImage : SITE.ogImage,
|
||||||
Astro.url.origin
|
Astro.url.origin
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export interface Props {
|
|||||||
|
|
||||||
const { post } = Astro.props;
|
const { post } = Astro.props;
|
||||||
|
|
||||||
const { title, author, description, ogImage, pubDatetime, tags } = post.data;
|
const { title, author, description, ogImage, canonicalURL, pubDatetime, tags } = post.data;
|
||||||
|
|
||||||
const { Content } = await post.render();
|
const { Content } = await post.render();
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ const ogUrl = new URL(ogImage ? ogImage : `${title}.png`, Astro.url.origin)
|
|||||||
.href;
|
.href;
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout title={title} author={author} description={description} ogImage={ogUrl}>
|
<Layout title={title} author={author} description={description} ogImage={ogUrl} canonicalURL={canonicalURL}>
|
||||||
<Header />
|
<Header />
|
||||||
<div class="mx-auto flex w-full max-w-3xl justify-start px-2">
|
<div class="mx-auto flex w-full max-w-3xl justify-start px-2">
|
||||||
<button
|
<button
|
||||||
|
|||||||
Reference in New Issue
Block a user