在 Nuxt.js 的 SSR(服务器端渲染)或 SSG(静态站点生成)应用中,SEO 优化是非常核心的工作内容之一。利用 Nuxt.js 的特性,我们可以通过多个维度系统地提升搜索引擎排名。
下面是我在实际项目中采取的 SEO 优化措施。
title
与 meta
标签export default {
head() {
return {
title: '产品详情 - 某某商城',
meta: [
{ hid: 'description', name: 'description', content: '这是一款高性能的产品...' },
{ hid: 'keywords', name: 'keywords', content: '产品,电商,商城' }
]
}
}
}
hid
防止 meta 重复Nuxt 自动去重 meta
,但必须提供 hid
,防止重复插入。
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Product",
"name": "苹果手机",
"description": "最新款 iPhone 15,拍照更清晰",
"brand": { "@type": "Brand", "name": "Apple" }
}
script>
export default {
target: 'static', // SSG
ssr: true // SSR 开启
}
<nuxt-img src="/banner.jpg" width="600" height="300" lazy />
pages/
├── product/
│ └── _id.vue → /product/123
sitemap.xml
自动生成使用 @nuxtjs/sitemap
模块:
modules: ['@nuxtjs/sitemap'],
sitemap: {
hostname: 'https://example.com',
routes: async () => {
const products = await axios.get('/api/products')
return products.map(p => `/product/${p.id}`)
}
}
head() {
return {
meta: [
{ property: 'og:title', content: '爆款苹果手机特卖' },
{ property: 'og:image', content: 'https://example.com/iphone.jpg' },
{ name: 'twitter:card', content: 'summary_large_image' }
]
}
}
modules: ['@nuxtjs/i18n'],
i18n: {
locales: ['en', 'zh'],
defaultLocale: 'zh',
seo: true
}
hreflang
,告诉搜索引擎页面对应语言版本。robots.txt
modules: ['@nuxtjs/robots'],
robots: {
UserAgent: '*',
Disallow: '',
Sitemap: 'https://example.com/sitemap.xml'
}
功能 | 模块名 |
---|---|
SEO 元数据管理 | @nuxtjs/head (已内置) |
sitemap.xml | @nuxtjs/sitemap |
robots.txt | @nuxtjs/robots |
多语言 SEO | @nuxtjs/i18n |
图片优化 | @nuxt/image |
优化点 | 工具/做法 | 作用 |
---|---|---|
标题描述优化 | head() 函数 |
提高相关性,吸引点击 |
Schema 标注 | JSON-LD 嵌入 | 生成富卡片,提高可见度 |
SSR/SSG 渲染模式 | ssr: true / target: 'static' |
提高首屏速度,利于爬虫抓取 |
图片懒加载 |
|
减少页面体积,提高性能评分 |
路由语义化 | 文件命名 + 动态路由 | 清晰的链接结构,提升权重 |
社交分享优化 | Open Graph / Twitter Card 元信息 | 提高社交媒体曝光 |
多语言 SEO | nuxt-i18n 模块 |
对不同语言做精准定位 |
robots/sitemap | 自动生成并配置 | 提升爬虫抓取效率 |