Skip to content

Commit

Permalink
Fix canonical URL for root website and index pages
Browse files Browse the repository at this point in the history
  • Loading branch information
meduzen committed Apr 28, 2024
1 parent a40afe1 commit f176a4f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions vitepress/.vitepress/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,21 @@ export default defineConfig({
],

// per page `<head>` entries
async transformHead(context) {
const canonicalUrl = `${APP_URL}/${context.pageData.filePath.replace('.md', '')}`
async transformHead({ pageData }) {
const keywordsMeta = metaName('keywords', pageData.frontmatter.tags) // @todo: add general website keyword

if (pageData.title == '404') { return keywordsMeta }

// remove trailing `index.md` or `.md`
const path = pageData.filePath.replace(/((index)?\.md)$/, '')

const canonicalUrl = `${APP_URL}/${path}`.replace(/\/$/, '') // remove trailing `/`

return [
metaName('keywords', context.pageData.frontmatter.tags), // @todo: add general website keyword
keywordsMeta,
['link', { rel: 'canonical', href: canonicalUrl }],
metaProperty('og:url', canonicalUrl),
...getOpenGraphTags(context.pageData),
...getOpenGraphTags(pageData),
]
},

Expand Down

0 comments on commit f176a4f

Please sign in to comment.