Skip to content

Commit d5a50ad

Browse files
committed
feat(nuxt): add schema-org:meta hook
1 parent 40540c9 commit d5a50ad

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

packages/nuxt/src/module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@ import type { NuxtModule } from '@nuxt/schema'
1010
import { dirname } from 'pathe'
1111
import type { UserConfig } from '@vueuse/schema-org'
1212
import { AliasRuntimePluginVite, AliasRuntimePluginWebpack } from '@vueuse/schema-org-vite'
13+
import type { MetaInput } from 'schema-org-graph-js'
1314

1415
export interface ModuleOptions extends UserConfig {}
1516

1617
export interface ModuleHooks {
1718

1819
}
1920

21+
declare module 'nuxt' {
22+
export interface RuntimeNuxtHooks {
23+
'schema-org:meta': (meta: MetaInput) => void
24+
}
25+
}
26+
2027
const Pkg = '@vueuse/schema-org'
2128

2229
export default defineNuxtModule<ModuleOptions>({

packages/nuxt/src/runtime/plugin.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { defineNuxtPlugin } from '#app'
33
import { unref, watch } from '#imports'
44
import config from '#build/nuxt-schema-org-config.mjs'
55

6-
export default defineNuxtPlugin((nuxtApp) => {
6+
export default defineNuxtPlugin(async (nuxtApp) => {
77
const ssr = !!nuxtApp.ssrContext?.url
88

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

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

25-
return {
25+
const schemaOrgMeta = {
2626
path: nuxtApp._route.path,
2727
...inferredMeta,
2828
...nuxtApp._route.meta,
2929
...config.meta || {},
3030
}
31+
await nuxtApp.hooks.callHook('schema-org:meta', schemaOrgMeta)
32+
return schemaOrgMeta
3133
},
3234
})
3335

3436
nuxtApp.vueApp.use(client)
3537

3638
if (ssr) {
37-
client.generateSchema()
38-
client.setupDOM()
39+
await client.forceRefresh()
3940
return
4041
}
4142

4243
watch(() => nuxtApp._route.path, () => {
43-
client.generateSchema()
44-
client.setupDOM()
44+
client.forceRefresh()
4545
})
4646
})

0 commit comments

Comments
 (0)