Skip to content

Commit

Permalink
Feat: ✨ configure canonical link
Browse files Browse the repository at this point in the history
All pages will have a self canonical (URL without trailing slash)

URLs with trailing slash are redirected permanently (301) to avoid duplicates in google index
  • Loading branch information
braanj committed Dec 17, 2024
1 parent ee21f48 commit 32497c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const ogTitle = 'Debbie codes and helps others learn Playwright, testing, React,
const twitterDescription = 'My website of where I play around with Nuxt, Playwright and more and showcase my blog, resources etc'
const twitterCard = 'https://debbie.codes/twitter-card.png'
const mySite = 'https://debbie.codes'
const { path } = useRoute()
const canonical = computed(()=> {
if (path === '/') return mySite
const { href: canonical } = new URL(path, mySite)
return canonical
})
useHead({
htmlAttrs: {
lang: 'en',
Expand Down Expand Up @@ -70,7 +78,7 @@ useHead({
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
{
rel: 'canonical',
href: mySite,
href: canonical.value,
},
],
})
Expand Down
7 changes: 7 additions & 0 deletions server/middleware/removeTrailingSlash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default defineEventHandler((event) => {
if (event.path !== '/' && event.path.endsWith('/')) {
const { path } = event
const nextPath = path.replace(/\/+$/, '') || '/'
return sendRedirect(event, nextPath, 301)
}
})

0 comments on commit 32497c3

Please sign in to comment.