Skip to content

Commit

Permalink
feat(nuxt): add schema-org:meta hook
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Aug 22, 2022
1 parent 40540c9 commit d5a50ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
7 changes: 7 additions & 0 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import type { NuxtModule } from '@nuxt/schema'
import { dirname } from 'pathe'
import type { UserConfig } from '@vueuse/schema-org'
import { AliasRuntimePluginVite, AliasRuntimePluginWebpack } from '@vueuse/schema-org-vite'
import type { MetaInput } from 'schema-org-graph-js'

export interface ModuleOptions extends UserConfig {}

export interface ModuleHooks {

}

declare module 'nuxt' {
export interface RuntimeNuxtHooks {
'schema-org:meta': (meta: MetaInput) => void
}
}

const Pkg = '@vueuse/schema-org'

export default defineNuxtModule<ModuleOptions>({
Expand Down
14 changes: 7 additions & 7 deletions packages/nuxt/src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineNuxtPlugin } from '#app'
import { unref, watch } from '#imports'
import config from '#build/nuxt-schema-org-config.mjs'

export default defineNuxtPlugin((nuxtApp) => {
export default defineNuxtPlugin(async (nuxtApp) => {
const ssr = !!nuxtApp.ssrContext?.url

const client = createSchemaOrg({
Expand All @@ -14,33 +14,33 @@ export default defineNuxtPlugin((nuxtApp) => {
// computed so only need to be done once
nuxtApp._useHead(unref(fn))
},
meta() {
async meta() {
const head = nuxtApp.vueApp._context.provides.usehead

const inferredMeta: Record<string, any> = {}
const headTag = head.headTags.reverse().filter(t => t.tag === 'title' && !!t.props.children)
if (headTag.length)
inferredMeta.title = headTag[0].props.children

return {
const schemaOrgMeta = {
path: nuxtApp._route.path,
...inferredMeta,
...nuxtApp._route.meta,
...config.meta || {},
}
await nuxtApp.hooks.callHook('schema-org:meta', schemaOrgMeta)
return schemaOrgMeta
},
})

nuxtApp.vueApp.use(client)

if (ssr) {
client.generateSchema()
client.setupDOM()
await client.forceRefresh()
return
}

watch(() => nuxtApp._route.path, () => {
client.generateSchema()
client.setupDOM()
client.forceRefresh()
})
})

0 comments on commit d5a50ad

Please sign in to comment.