useSeoMeta

源文件
useSeoMeta 组合式函数允许您以扁平对象的形式定义网站的 SEO meta 标签,并提供完整的 TypeScript 支持。

这有助于避免常见错误,例如使用 name 而不是 property,以及拼写错误——其中包含 100 多个完全类型化的 meta 标签。

这是向网站添加 meta 标签的推荐方法,因为它具有 XSS 安全性并完全支持 TypeScript。
文档 > 4 X > 入门 > Seo Meta中阅读更多内容。

使用

app/app.vue
<script setup lang="ts">
useSeoMeta({
  title: 'My Amazing Site',
  ogTitle: 'My Amazing Site',
  description: 'This is my amazing site, let me tell you all about it.',
  ogDescription: 'This is my amazing site, let me tell you all about it.',
  ogImage: 'https://example.com/image.png',
  twitterCard: 'summary_large_image',
})
</script>

当插入具有响应式的标签时,您应该使用计算属性 getter 语法(() => value

app/app.vue
<script setup lang="ts">
const title = ref('My title')

useSeoMeta({
  title,
  description: () => `This is a description for the ${title.value} page`,
})
</script>

参数

有 100 多个参数。请参阅源码中的完整参数列表

文档 > 4 X > 入门 > Seo Meta中阅读更多内容。

性能

在大多数情况下,SEO meta 标签不需要是响应式的,因为搜索引擎机器人主要扫描初始页面加载。

为了获得更好的性能,当 meta 标签不需要是响应式时,您可以将 useSeoMeta 调用包裹在仅服务端的条件中

app/app.vue
<script setup lang="ts">
if (import.meta.server) {
  // These meta tags will only be added during server-side rendering
  useSeoMeta({
    robots: 'index, follow',
    description: 'Static description that does not need reactivity',
    ogImage: 'https://example.com/image.png',
    // other static meta tags...
  })
}

const dynamicTitle = ref('My title')
// Only use reactive meta tags outside the condition when necessary
useSeoMeta({
  title: () => dynamicTitle.value,
  ogTitle: () => dynamicTitle.value,
})
</script>

这以前使用的是 useServerSeoMeta 组合式函数,但为了采用这种新方法,它已被弃用。