docs: update recommendation for canonical urls to use transformPageData instead

pull/3488/head
Divyansh Singh 10 months ago
parent c882fa1469
commit ba3c6441be

@ -645,19 +645,20 @@ export default {
}
```
#### Example: Adding a canonical URL `<link>`
#### Example: Adding a canonical URL `<link>`
```ts
export default {
transformHead({ page }) {
// Skip the 404 page
if (page !== '404.md') {
const canonicalUrl = `https://example.com/${page}`
.replace(/index\.md$/, '')
.replace(/\.md$/, '.html')
return [['link', { rel: 'canonical', href: canonicalUrl }]]
}
transformPageData(pageData) {
const canonicalUrl = `https://example.com/${pageData.relativePath}`
.replace(/index\.md$/, '')
.replace(/\.md$/, '.html')
pageData.frontmatter.head ??= []
pageData.frontmatter.head.push([
'link',
{ rel: 'canonical', href: canonicalUrl }
])
}
}
```

Loading…
Cancel
Save