Skip to content

Commit

Permalink
Revert "fix: drop fallback title support"
Browse files Browse the repository at this point in the history
This reverts commit 185b8c8.
  • Loading branch information
harlan-zw committed Sep 6, 2023
1 parent 0585cee commit 6d00cdd
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions module/src/runtime/plugin/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import {
useSiteConfig,
} from '#imports'

function titleCase(s: string) {
return s
.replaceAll('-', ' ')
.replace(/\w\S*/g, w => w.charAt(0).toUpperCase() + w.substr(1).toLowerCase())
}

export default defineNuxtPlugin({
name: 'nuxtseo:defaults',
setup() {
Expand All @@ -25,7 +31,19 @@ export default defineNuxtPlugin({
const resolveUrl = createSitePathResolver({ withBase: true, absolute: true })
const canonicalUrl = computed(() => resolveUrl(route.path || '/').value)

const title = computed(() => {
if (typeof route.meta?.title === 'string')
return route.meta?.title

// if no title has been set then we should use the last segment of the URL path and title case it
const path = route.path || '/'
const lastSegment = path.split('/').pop()
return lastSegment ? titleCase(lastSegment) : null
})

useHead({
// fallback title
title,
link: [{ rel: 'canonical', href: canonicalUrl }],
})

Expand Down

0 comments on commit 6d00cdd

Please sign in to comment.