Skip to content

Commit ba3c644

Browse files
committed
docs: update recommendation for canonical urls to use transformPageData instead
1 parent c882fa1 commit ba3c644

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

docs/reference/site-config.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -645,19 +645,20 @@ export default {
645645
}
646646
```
647647

648-
#### Example: Adding a canonical URL `<link>`
648+
#### Example: Adding a canonical URL `<link>`
649649

650650
```ts
651651
export default {
652-
transformHead({ page }) {
653-
// Skip the 404 page
654-
if (page !== '404.md') {
655-
const canonicalUrl = `https://example.com/${page}`
656-
.replace(/index\.md$/, '')
657-
.replace(/\.md$/, '.html')
658-
659-
return [['link', { rel: 'canonical', href: canonicalUrl }]]
660-
}
652+
transformPageData(pageData) {
653+
const canonicalUrl = `https://example.com/${pageData.relativePath}`
654+
.replace(/index\.md$/, '')
655+
.replace(/\.md$/, '.html')
656+
657+
pageData.frontmatter.head ??= []
658+
pageData.frontmatter.head.push([
659+
'link',
660+
{ rel: 'canonical', href: canonicalUrl }
661+
])
661662
}
662663
}
663664
```

0 commit comments

Comments
 (0)