Skip to content

Commit

Permalink
fix: remove Schema.org defaults, moved to nuxt-schema-org
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Dec 21, 2023
1 parent cc7c53d commit 3851490
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 67 deletions.
2 changes: 1 addition & 1 deletion docs/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useModuleList } from '~/utils/data'
definePageMeta({
breadcrumb: {
icon: 'heroicons-solid:home',
icon: 'i-heroicons-home',
ariaLabel: 'Home',
},
})
Expand Down
10 changes: 8 additions & 2 deletions module/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ export default defineNuxtModule<ModuleOptions>({

await installNuxtSiteConfig()

// TODO disable modules if SSR is disabled
// if (!nuxt.options.ssr) {
// nuxt.options.schemaOrg = defu({ enabled: false}, nuxt.options.schemaOrg)
// nuxt.options.ogImage = defu({ enabled: false}, nuxt.options.ogImage)
// logger.warn('SSR is required for Nuxt OG Image and Nuxt Schema.org, disabling them.')
// }

for (const module of Modules)
await installModule(await resolvePath(module))

Expand Down Expand Up @@ -121,7 +128,6 @@ export default defineNuxtModule<ModuleOptions>({

// if user disables certain modules we need to pollyfill the imports
const polyfills: Record<string, string[]> = {
robots: ['defineRobotMeta'],
schemaOrg: ['useSchemaOrg', 'defineWebSite', 'defineWebPage'],
}
for (const [module, composables] of Object.entries(polyfills)) {
Expand Down Expand Up @@ -157,7 +163,7 @@ export default defineNuxtModule<ModuleOptions>({
if (!upToDate)
logger.log(`${chalk.gray(' ├─ ')}🎉 New version available!${chalk.gray(` Run ${chalk.underline(`npm i nuxt-seo-kit@${latestTag}`)} to update.`)}`)

logger.log(chalk.dim(' └─ 🧪 Nuxt SEO is in beta. Shape the future of it: https://github.com/harlan-zw/nuxt-seo-kit/discussions/108'))
logger.log(chalk.dim(' └─ 🧪 Help get Nuxt SEO stable by providing feedback https://github.com/harlan-zw/nuxt-seo-kit/discussions/108'))
logger.log('')
}
},
Expand Down
68 changes: 4 additions & 64 deletions module/src/runtime/plugin/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,31 @@
import type { SiteConfig } from 'nuxt-site-config-kit'
import { defineNuxtPlugin } from 'nuxt/app'
import type { UseHeadOptions, UseSeoMetaInput } from '@unhead/vue'
import type { Organization, Person } from '@unhead/schema-org'
import {
computed,
createSitePathResolver,
defineOrganization,
definePerson,
defineRobotMeta,
defineWebPage,
defineWebSite,
useHead,
useRoute,
useSchemaOrg,
useSeoMeta,
useServerHead,
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',
name: 'nuxt-seo:defaults',
setup() {
// get the head instance
const siteConfig = { ...useSiteConfig() } as Omit<SiteConfig, '_context'>
delete siteConfig._context
const route = useRoute()
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
})
const canonicalUrl = computed<string>(() => resolveUrl(route.path || '/').value || route.path)

const minimalPriority: UseHeadOptions = {
// give nuxt.config values higher priority
tagPriority: 101,
}

useHead({
// fallback title
title,
}, minimalPriority)

// needs higher priority
useHead({
link: [{ rel: 'canonical', href: canonicalUrl }],
Expand All @@ -67,8 +39,8 @@ export default defineNuxtPlugin({

// TODO support SPA
useHead({
templateParams: { site: siteConfig, siteName: siteConfig.name },
titleTemplate: '%s %separator %site.name',
templateParams: { site: siteConfig, siteName: siteConfig.name || '' },
titleTemplate: '%s %separator %siteName',
}, minimalPriority)

const seoMeta: UseSeoMetaInput = {
Expand All @@ -87,37 +59,5 @@ export default defineNuxtPlugin({
seoMeta.twitterSite = id
}
useSeoMeta(seoMeta, minimalPriority)

// init vendors
defineRobotMeta()
useSchemaOrg([
defineWebSite({
name: () => siteConfig?.name || '',
// TODO integrate with nuxt/i18n
inLanguage: () => siteConfig?.currentLocale || '',
description: () => siteConfig?.description || '',
}),
defineWebPage(),
])
if (siteConfig.identity) {
const identityPayload: Person | Organization = {
name: siteConfig.identity.name || siteConfig.name,
url: siteConfig.url,
}
if (siteConfig.twitter) {
// without the @
const id = siteConfig.twitter.startsWith('@')
? siteConfig.twitter.slice(1)
: siteConfig.twitter
identityPayload.sameAs = [
`https://twitter.com/${id}`,
]
}
useSchemaOrg([
siteConfig.identity.type === 'Person'
? definePerson(identityPayload)
: defineOrganization(identityPayload),
])
}
},
})

0 comments on commit 3851490

Please sign in to comment.