Skip to content

Commit

Permalink
fix: support for @vueuse/head v1
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Nov 13, 2022
1 parent 3f0b739 commit cc4b1af
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 53 deletions.
28 changes: 19 additions & 9 deletions packages/nuxt/src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@ export default defineNuxtPlugin(async (nuxtApp) => {
async meta() {
const head = nuxtApp.vueApp._context.provides.usehead

let tags: { tag: string; props: any; children?: string }[] = []
if (typeof head.resolveTags === 'function')
tags = await head.resolveTags()
else if (typeof head.headTags === 'object')
tags = head.headTags
else if (typeof head.headTags === 'function')
tags = await head.headTags()

tags = tags.reverse()

const inferredMeta = {} as ResolvedMeta
const headTag = head.headTags.reverse().filter(t => t.tag === 'title' && (!!t.props.children || !!t.children))
if (headTag.length)
inferredMeta.title = headTag[0].props.children || headTag[0].children
const descTag = head.headTags.reverse().filter(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
if (descTag.length)
inferredMeta.description = descTag[0].props.content
const imageTag = head.headTags.reverse().filter(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
if (imageTag.length)
inferredMeta.image = imageTag[0].props.content
const titleTag = tags.find(t => t.tag === 'title' && (!!t.props.children || !!t.children))
if (titleTag)
inferredMeta.title = titleTag.props.children || titleTag.children
const descTag = tags.find(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
if (descTag)
inferredMeta.description = descTag.props.content
const imageTag = tags.find(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
if (imageTag)
inferredMeta.image = imageTag.props.content
const schemaOrgMeta = {
path: nuxtApp._route.path,
...inferredMeta,
Expand Down
36 changes: 23 additions & 13 deletions packages/vite/src/iles-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,31 @@ export function installSchemaOrg(ctx: EnhanceAppContext, config: UserConfig) {
if (typeof document !== 'undefined')
ctx.head.updateDOM()
},
meta() {
async meta() {
const inferredMeta: Record<string, any> = {}

const tags = ctx.head.headTags?.reverse()
if (tags) {
const headTag = tags.filter(t => t.tag === 'title' && !!t.props.children)
if (headTag.length)
inferredMeta.title = headTag[0].props.children
const descTag = tags.filter(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
if (descTag.length)
inferredMeta.description = descTag[0].props.content
const imageTag = tags.filter(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
if (imageTag.length)
inferredMeta.image = imageTag[0].props.content
}
let tags: { tag: string; props: any; children?: string }[] = []
// @ts-expect-error version mismatch
if (typeof ctx.head.resolveTags === 'function')
// @ts-expect-error version mismatch
tags = await ctx.head.resolveTags()
else if (typeof ctx.head.headTags === 'object')
tags = ctx.head.headTags
else if (typeof ctx.head.headTags === 'function')
// @ts-expect-error version mismatch
tags = await ctx.head.headTags()

tags = tags.reverse()

const titleTag = tags.find(t => t.tag === 'title' && (!!t.props.children || !!t.children))
if (titleTag)
inferredMeta.title = titleTag.props.children || titleTag.children
const descTag = tags.find(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
if (descTag)
inferredMeta.description = descTag.props.content
const imageTag = tags.find(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
if (imageTag)
inferredMeta.image = imageTag.props.content

return {
path: ctx.router?.currentRoute.value.path || '/',
Expand Down
39 changes: 23 additions & 16 deletions packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,31 @@ export function installSchemaOrg(ctx: { app: App; router?: Router }, config: Use
if (typeof document !== 'undefined')
head.updateDOM()
},
meta() {
async meta() {
const inferredMeta: Record<string, any> = {}

const tags = head.headTags?.reverse()
if (tags) {
// @ts-expect-error latest @vueuse/head
const headTag = tags.filter(t => t.tag === 'title' && (!!t.props.children || !!t.children))
if (headTag.length) {
// @ts-expect-error latest @vueuse/head
inferredMeta.title = headTag[0].props.children || headTag[0].children
}
const descTag = tags.filter(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
if (descTag.length)
inferredMeta.description = descTag[0].props.content
const imageTag = tags.filter(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
if (imageTag.length)
inferredMeta.image = imageTag[0].props.content
}
let tags: { tag: string; props: any; children?: string }[] = []
// @ts-expect-error version mismatch
if (typeof head.resolveTags === 'function')
// @ts-expect-error version mismatch
tags = await head.resolveTags()
else if (typeof head.headTags === 'object')
tags = head.headTags
else if (typeof head.headTags === 'function')
// @ts-expect-error version mismatch
tags = await head.headTags()

tags = tags.reverse()

const titleTag = tags.find(t => t.tag === 'title' && (!!t.props.children || !!t.children))
if (titleTag)
inferredMeta.title = titleTag.props.children || titleTag.children
const descTag = tags.find(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
if (descTag)
inferredMeta.description = descTag.props.content
const imageTag = tags.find(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
if (imageTag)
inferredMeta.image = imageTag.props.content

return {
path: ctx.router?.currentRoute.value.path || '/',
Expand Down
39 changes: 26 additions & 13 deletions packages/vite/src/vitesse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,34 @@ export function installSchemaOrg(ctx: ViteSSGContext, config: UserConfig) {
if (typeof document !== 'undefined')
ctx.head?.updateDOM()
},
meta() {
async meta() {
const inferredMeta: Record<string, any> = {}

const tags = ctx.head?.headTags?.reverse()
if (tags) {
const headTag = tags.filter(t => t.tag === 'title' && !!t.props.children)
if (headTag.length)
inferredMeta.title = headTag[0].props.children
const descTag = tags.filter(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
if (descTag.length)
inferredMeta.description = descTag[0].props.content
const imageTag = tags.filter(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
if (imageTag.length)
inferredMeta.image = imageTag[0].props.content
}
let tags: { tag: string; props: any; children?: string }[] = []
// @ts-expect-error version mismatch
if (typeof ctx.head.resolveTags === 'function')
// @ts-expect-error version mismatch
tags = await ctx.head.resolveTags()
// @ts-expect-error version mismatch
else if (typeof ctx.head.headTags === 'object')
// @ts-expect-error version mismatch
tags = ctx.head.headTags
// @ts-expect-error version mismatch
else if (typeof ctx.head.headTags === 'function')
// @ts-expect-error version mismatch
tags = await ctx.head.headTags()

tags = tags.reverse()

const titleTag = tags.find(t => t.tag === 'title' && (!!t.props.children || !!t.children))
if (titleTag)
inferredMeta.title = titleTag.props.children || titleTag.children
const descTag = tags.find(t => t.tag === 'meta' && t.props.name === 'description' && !!t.props.content)
if (descTag)
inferredMeta.description = descTag.props.content
const imageTag = tags.find(t => t.tag === 'meta' && t.props.property === 'og:image' && !!t.props.content)
if (imageTag)
inferredMeta.image = imageTag.props.content

return {
path: ctx.router.currentRoute.value.path,
Expand Down
4 changes: 2 additions & 2 deletions playgrounds/nuxt3/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default defineNuxtConfig({
'@vueuse/schema-org-vite': resolve(__dirname, '../../packages/vite/src/index.ts'),
},
modules: [
// 'nuxt-windicss',
// 'nuxt-schema-org',
'nuxt-windicss',
'nuxt-schema-org',
],
schemaOrg: {
canonicalHost: 'https://example.com',
Expand Down

0 comments on commit cc4b1af

Please sign in to comment.