Skip to content

Commit cc4b1af

Browse files
committed
fix: support for @vueuse/head v1
1 parent 3f0b739 commit cc4b1af

File tree

5 files changed

+93
-53
lines changed

5 files changed

+93
-53
lines changed

packages/nuxt/src/runtime/plugin.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,26 @@ export default defineNuxtPlugin(async (nuxtApp) => {
1818
async meta() {
1919
const head = nuxtApp.vueApp._context.provides.usehead
2020

21+
let tags: { tag: string; props: any; children?: string }[] = []
22+
if (typeof head.resolveTags === 'function')
23+
tags = await head.resolveTags()
24+
else if (typeof head.headTags === 'object')
25+
tags = head.headTags
26+
else if (typeof head.headTags === 'function')
27+
tags = await head.headTags()
28+
29+
tags = tags.reverse()
30+
2131
const inferredMeta = {} as ResolvedMeta
22-
const headTag = head.headTags.reverse().filter(t => t.tag === 'title' && (!!t.props.children || !!t.children))
23-
if (headTag.length)
24-
inferredMeta.title = headTag[0].props.children || headTag[0].children
25-
const descTag = head.headTags.reverse().filter(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
26-
if (descTag.length)
27-
inferredMeta.description = descTag[0].props.content
28-
const imageTag = head.headTags.reverse().filter(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
29-
if (imageTag.length)
30-
inferredMeta.image = imageTag[0].props.content
32+
const titleTag = tags.find(t => t.tag === 'title' && (!!t.props.children || !!t.children))
33+
if (titleTag)
34+
inferredMeta.title = titleTag.props.children || titleTag.children
35+
const descTag = tags.find(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
36+
if (descTag)
37+
inferredMeta.description = descTag.props.content
38+
const imageTag = tags.find(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
39+
if (imageTag)
40+
inferredMeta.image = imageTag.props.content
3141
const schemaOrgMeta = {
3242
path: nuxtApp._route.path,
3343
...inferredMeta,

packages/vite/src/iles-app.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,31 @@ export function installSchemaOrg(ctx: EnhanceAppContext, config: UserConfig) {
1414
if (typeof document !== 'undefined')
1515
ctx.head.updateDOM()
1616
},
17-
meta() {
17+
async meta() {
1818
const inferredMeta: Record<string, any> = {}
1919

20-
const tags = ctx.head.headTags?.reverse()
21-
if (tags) {
22-
const headTag = tags.filter(t => t.tag === 'title' && !!t.props.children)
23-
if (headTag.length)
24-
inferredMeta.title = headTag[0].props.children
25-
const descTag = tags.filter(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
26-
if (descTag.length)
27-
inferredMeta.description = descTag[0].props.content
28-
const imageTag = tags.filter(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
29-
if (imageTag.length)
30-
inferredMeta.image = imageTag[0].props.content
31-
}
20+
let tags: { tag: string; props: any; children?: string }[] = []
21+
// @ts-expect-error version mismatch
22+
if (typeof ctx.head.resolveTags === 'function')
23+
// @ts-expect-error version mismatch
24+
tags = await ctx.head.resolveTags()
25+
else if (typeof ctx.head.headTags === 'object')
26+
tags = ctx.head.headTags
27+
else if (typeof ctx.head.headTags === 'function')
28+
// @ts-expect-error version mismatch
29+
tags = await ctx.head.headTags()
30+
31+
tags = tags.reverse()
32+
33+
const titleTag = tags.find(t => t.tag === 'title' && (!!t.props.children || !!t.children))
34+
if (titleTag)
35+
inferredMeta.title = titleTag.props.children || titleTag.children
36+
const descTag = tags.find(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
37+
if (descTag)
38+
inferredMeta.description = descTag.props.content
39+
const imageTag = tags.find(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
40+
if (imageTag)
41+
inferredMeta.image = imageTag.props.content
3242

3343
return {
3444
path: ctx.router?.currentRoute.value.path || '/',

packages/vite/src/vite.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,31 @@ export function installSchemaOrg(ctx: { app: App; router?: Router }, config: Use
2020
if (typeof document !== 'undefined')
2121
head.updateDOM()
2222
},
23-
meta() {
23+
async meta() {
2424
const inferredMeta: Record<string, any> = {}
2525

26-
const tags = head.headTags?.reverse()
27-
if (tags) {
28-
// @ts-expect-error latest @vueuse/head
29-
const headTag = tags.filter(t => t.tag === 'title' && (!!t.props.children || !!t.children))
30-
if (headTag.length) {
31-
// @ts-expect-error latest @vueuse/head
32-
inferredMeta.title = headTag[0].props.children || headTag[0].children
33-
}
34-
const descTag = tags.filter(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
35-
if (descTag.length)
36-
inferredMeta.description = descTag[0].props.content
37-
const imageTag = tags.filter(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
38-
if (imageTag.length)
39-
inferredMeta.image = imageTag[0].props.content
40-
}
26+
let tags: { tag: string; props: any; children?: string }[] = []
27+
// @ts-expect-error version mismatch
28+
if (typeof head.resolveTags === 'function')
29+
// @ts-expect-error version mismatch
30+
tags = await head.resolveTags()
31+
else if (typeof head.headTags === 'object')
32+
tags = head.headTags
33+
else if (typeof head.headTags === 'function')
34+
// @ts-expect-error version mismatch
35+
tags = await head.headTags()
36+
37+
tags = tags.reverse()
38+
39+
const titleTag = tags.find(t => t.tag === 'title' && (!!t.props.children || !!t.children))
40+
if (titleTag)
41+
inferredMeta.title = titleTag.props.children || titleTag.children
42+
const descTag = tags.find(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
43+
if (descTag)
44+
inferredMeta.description = descTag.props.content
45+
const imageTag = tags.find(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
46+
if (imageTag)
47+
inferredMeta.image = imageTag.props.content
4148

4249
return {
4350
path: ctx.router?.currentRoute.value.path || '/',

packages/vite/src/vitesse.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,34 @@ export function installSchemaOrg(ctx: ViteSSGContext, config: UserConfig) {
1313
if (typeof document !== 'undefined')
1414
ctx.head?.updateDOM()
1515
},
16-
meta() {
16+
async meta() {
1717
const inferredMeta: Record<string, any> = {}
1818

19-
const tags = ctx.head?.headTags?.reverse()
20-
if (tags) {
21-
const headTag = tags.filter(t => t.tag === 'title' && !!t.props.children)
22-
if (headTag.length)
23-
inferredMeta.title = headTag[0].props.children
24-
const descTag = tags.filter(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
25-
if (descTag.length)
26-
inferredMeta.description = descTag[0].props.content
27-
const imageTag = tags.filter(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
28-
if (imageTag.length)
29-
inferredMeta.image = imageTag[0].props.content
30-
}
19+
let tags: { tag: string; props: any; children?: string }[] = []
20+
// @ts-expect-error version mismatch
21+
if (typeof ctx.head.resolveTags === 'function')
22+
// @ts-expect-error version mismatch
23+
tags = await ctx.head.resolveTags()
24+
// @ts-expect-error version mismatch
25+
else if (typeof ctx.head.headTags === 'object')
26+
// @ts-expect-error version mismatch
27+
tags = ctx.head.headTags
28+
// @ts-expect-error version mismatch
29+
else if (typeof ctx.head.headTags === 'function')
30+
// @ts-expect-error version mismatch
31+
tags = await ctx.head.headTags()
32+
33+
tags = tags.reverse()
34+
35+
const titleTag = tags.find(t => t.tag === 'title' && (!!t.props.children || !!t.children))
36+
if (titleTag)
37+
inferredMeta.title = titleTag.props.children || titleTag.children
38+
const descTag = tags.find(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
39+
if (descTag)
40+
inferredMeta.description = descTag.props.content
41+
const imageTag = tags.find(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
42+
if (imageTag)
43+
inferredMeta.image = imageTag.props.content
3144

3245
return {
3346
path: ctx.router.currentRoute.value.path,

playgrounds/nuxt3/nuxt.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export default defineNuxtConfig({
1111
'@vueuse/schema-org-vite': resolve(__dirname, '../../packages/vite/src/index.ts'),
1212
},
1313
modules: [
14-
// 'nuxt-windicss',
15-
// 'nuxt-schema-org',
14+
'nuxt-windicss',
15+
'nuxt-schema-org',
1616
],
1717
schemaOrg: {
1818
canonicalHost: 'https://example.com',

0 commit comments

Comments
 (0)