Skip to content

Commit e7054d6

Browse files
committed
feat(nuxt): allow inferences on description and image
1 parent 7380db7 commit e7054d6

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

packages/nuxt/src/runtime/plugin.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createSchemaOrg } from '@vueuse/schema-org'
2+
import type { ResolvedMeta } from 'schema-org-graph-js'
23
import { defineNuxtPlugin } from '#app'
34
import { unref, watch } from '#imports'
45
import config from '#build/nuxt-schema-org-config.mjs'
@@ -17,11 +18,16 @@ export default defineNuxtPlugin(async (nuxtApp) => {
1718
async meta() {
1819
const head = nuxtApp.vueApp._context.provides.usehead
1920

20-
const inferredMeta: Record<string, any> = {}
21+
const inferredMeta = {} as ResolvedMeta
2122
const headTag = head.headTags.reverse().filter(t => t.tag === 'title' && !!t.props.children)
2223
if (headTag.length)
2324
inferredMeta.title = headTag[0].props.children
24-
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
2531
const schemaOrgMeta = {
2632
path: nuxtApp._route.path,
2733
...inferredMeta,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<script lang="ts" setup>
2+
useHead({
3+
title: 'Title Override',
4+
meta: [
5+
{
6+
name: 'description',
7+
content: 'Description override',
8+
},
9+
{
10+
property: 'og:image',
11+
content: 'https://example-image-override.com/img.png',
12+
},
13+
],
14+
})
15+
</script>
16+
17+
<template>
18+
<div>
19+
<h1>Meta Overrides</h1>
20+
</div>
21+
</template>

test/fixtures/nuxt/pages/title-override.vue

Lines changed: 0 additions & 11 deletions
This file was deleted.

test/nuxt/basic.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('pages', () => {
6161
})
6262

6363
it('render title override', async () => {
64-
const schema = await $fetchSchemaOrg('/title-override')
64+
const schema = await $fetchSchemaOrg('/meta-overrides')
6565

6666
// Snapshot
6767
expect(schema).toMatchInlineSnapshot(`
@@ -85,7 +85,7 @@ describe('pages', () => {
8585
"url": "https://example.com",
8686
},
8787
{
88-
"@id": "https://example.com/title-override/#webpage",
88+
"@id": "https://example.com/meta-overrides/#webpage",
8989
"@type": "WebPage",
9090
"about": {
9191
"@id": "https://example.com/#identity",
@@ -98,21 +98,22 @@ describe('pages', () => {
9898
{
9999
"@type": "ReadAction",
100100
"target": [
101-
"https://example.com/title-override",
101+
"https://example.com/meta-overrides",
102102
],
103103
},
104104
],
105-
"url": "https://example.com/title-override",
105+
"url": "https://example.com/meta-overrides",
106106
},
107107
],
108108
}
109109
`)
110110

111111
const webpageNode = schema['@graph'].filter(n => n['@type'] === 'WebPage')[0]
112-
expect(webpageNode.url).toEqual('https://example.com/title-override')
112+
expect(webpageNode.url).toEqual('https://example.com/meta-overrides')
113113
expect(webpageNode.name).toEqual('Title Override')
114+
expect(webpageNode.description).toEqual('Description override')
114115

115-
await expectNoClientErrors('/title-override')
116+
await expectNoClientErrors('/meta-overrides')
116117
})
117118

118119
it('render plugin override', async () => {

0 commit comments

Comments
 (0)