Skip to content

Commit b4afda6

Browse files
authored
Merge pull request #344 from onurgenes/master
added canonicalUrl support
2 parents fe08ac5 + f46cebe commit b4afda6

File tree

7 files changed

+31
-4
lines changed

7 files changed

+31
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ summary (optional)
178178
images (optional, if none provided defaults to socialBanner in siteMetadata config)
179179
authors (optional list which should correspond to the file names in `data/authors`. Uses `default` if none is specified)
180180
layout (optional list which should correspond to the file names in `data/layouts`)
181+
canonicalUrl (optional, canonical url for the post for SEO)
181182
```
182183

183184
Here's an example of a post's frontmatter:
@@ -193,6 +194,7 @@ summary: 'Looking for a performant, out of the box template, with all the best i
193194
images: ['/static/images/canada/mountains.jpg', '/static/images/canada/toronto.jpg']
194195
authors: ['default', 'sparrowhawk']
195196
layout: PostLayout
197+
canonicalUrl: https://tailwind-nextjs-starter-blog.vercel.app/blog/introducing-tailwind-nextjs-starter-blog
196198
---
197199
```
198200

components/SEO.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Head from 'next/head'
22
import { useRouter } from 'next/router'
33
import siteMetadata from '@/data/siteMetadata'
44

5-
const CommonSEO = ({ title, description, ogType, ogImage, twImage }) => {
5+
const CommonSEO = ({ title, description, ogType, ogImage, twImage, canonicalUrl }) => {
66
const router = useRouter()
77
return (
88
<Head>
@@ -24,6 +24,10 @@ const CommonSEO = ({ title, description, ogType, ogImage, twImage }) => {
2424
<meta name="twitter:title" content={title} />
2525
<meta name="twitter:description" content={description} />
2626
<meta name="twitter:image" content={twImage} />
27+
<link
28+
rel="canonical"
29+
href={canonicalUrl ? canonicalUrl : `${siteMetadata.siteUrl}${router.asPath}`}
30+
/>
2731
</Head>
2832
)
2933
}
@@ -67,7 +71,16 @@ export const TagSEO = ({ title, description }) => {
6771
)
6872
}
6973

70-
export const BlogSEO = ({ authorDetails, title, summary, date, lastmod, url, images = [] }) => {
74+
export const BlogSEO = ({
75+
authorDetails,
76+
title,
77+
summary,
78+
date,
79+
lastmod,
80+
url,
81+
images = [],
82+
canonicalUrl,
83+
}) => {
7184
const router = useRouter()
7285
const publishedAt = new Date(date).toISOString()
7386
const modifiedAt = new Date(lastmod || date).toISOString()
@@ -133,11 +146,11 @@ export const BlogSEO = ({ authorDetails, title, summary, date, lastmod, url, ima
133146
ogType="article"
134147
ogImage={featuredImages}
135148
twImage={twImageUrl}
149+
canonicalUrl={canonicalUrl}
136150
/>
137151
<Head>
138152
{date && <meta property="article:published_time" content={publishedAt} />}
139153
{lastmod && <meta property="article:modified_time" content={modifiedAt} />}
140-
<link rel="canonical" href={`${siteMetadata.siteUrl}${router.asPath}`} />
141154
<script
142155
type="application/ld+json"
143156
dangerouslySetInnerHTML={{

data/blog/introducing-tailwind-nextjs-starter-blog.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ summary (optional)
149149
images (optional, if none provided defaults to socialBanner in siteMetadata config)
150150
authors (optional list which should correspond to the file names in `data/authors`. Uses `default` if none is specified)
151151
layout (optional list which should correspond to the file names in `data/layouts`)
152+
canonicalUrl (optional, canonical url for the post for SEO)
152153
```
153154

154155
Here's an example of a post's frontmatter:
@@ -164,6 +165,7 @@ summary: 'Looking for a performant, out of the box template, with all the best i
164165
images: ['/static/images/canada/mountains.jpg', '/static/images/canada/toronto.jpg']
165166
authors: ['default', 'sparrowhawk']
166167
layout: PostLayout
168+
canonicalUrl: https://tailwind-nextjs-starter-blog.vercel.app/blog/introducing-tailwind-nextjs-starter-blog
167169
---
168170
```
169171

data/blog/new-features-in-v1.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ draft: false
77
summary: 'An overview of the new features released in v1 - code block copy, multiple authors, frontmatter layout and more'
88
layout: PostSimple
99
bibliography: references-data.bib
10+
canonicalUrl: https://tailwind-nextjs-starter-blog.vercel.app/blog/new-features-in-v1/
1011
---
1112

1213
## Overview

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/compose.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const genFrontMatter = (answers) => {
4040
summary: ${answers.summary ? answers.summary : ' '}
4141
images: []
4242
layout: ${answers.layout}
43+
canonicalUrl: ${answers.canonicalUrl}
4344
`
4445

4546
if (answers.authors.length > 0) {
@@ -92,6 +93,11 @@ inquirer
9293
type: 'list',
9394
choices: getLayouts,
9495
},
96+
{
97+
name: 'canonicalUrl',
98+
message: 'Enter canonical url:',
99+
type: 'input',
100+
},
95101
])
96102
.then((answers) => {
97103
// Remove special characters and replace space with -

scripts/generate-sitemap.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const siteMetadata = require('../data/siteMetadata')
2929
if (fm.data.draft) {
3030
return
3131
}
32+
if (fm.data.canonicalUrl) {
33+
return
34+
}
3235
}
3336
const path = page
3437
.replace('pages/', '/')

0 commit comments

Comments
 (0)