如何向 Google Search Console 提交多个语言版本的站点地图?
解读
在 Vercel 托管的多语言站点中,每个语言版本通常对应独立子路径或子域名。Google 要求为每个语言版本单独提交站点地图,以便正确识别 hreflang 关系并避免重复内容惩罚。国内面试场景下,面试官想确认两点:一是候选人是否理解“多语言 SEO ≠ 单文件多 URL”,二是能否在 Vercel 无服务器环境里自动化、零停机地完成提交,而不是手动上传文件。
知识点
- 多语言架构模式
- 子目录 /zh、/en、/ja 最常见,Vercel 通过 rewrites 与 i18n 路由即可实现。
- 子域名 cn.example.com、en.example.com 需额外 DNS 与证书,Vercel 支持通配符证书。
- 站点地图规范
- 每套语言必须输出独立的 XML 索引文件(sitemap-index.xml),内含各语言版本的 <loc> 与 <xhtml:link rel="alternate" hreflang="x">。
- 单个 sitemap 文件 ≤ 5 万条 URL、≤ 50 MB。
- Vercel 自动化生成
- Next.js 官方插件 next-sitemap 支持 i18n 配置,build 时按 locale 生成多份 sitemap-zh.xml、sitemap-en.xml 并自动写入 public/。
- 通过 Incremental Static Regeneration 可在内容变更后触发 webhook 重新生成,无需全站重建。
- 提交通道
- Search Console 的“站点地图”栏目:每个属性(域名或前缀)最多 500 条索引文件。
- Indexing API 仅适用于 JobPosting/BroadcastEvent,不适用于普通页面,因此仍需 sitemap。
- 国内网络下,使用服务器端代提交比浏览器上传更稳定,需配置代理或香港节点出口。
- 验证与监控
- 利用 Search Console API 的 sitemaps.list 方法轮询状态,发现“couldn't fetch”立即触发 Vercel 函数重试。
- 结合 GitHub Action 在 main 分支合并后自动调用 API,实现“git push → 生成 → 提交 → 校验”闭环。
答案
步骤概览(以子目录方案为例,框架 Next.js,部署在 Vercel):
-
项目根目录安装并配置 next-sitemap
npm i next-sitemap新增 next-sitemap.config.js:
module.exports = { siteUrl: 'https://example.com', generateRobotsTxt: true, generateIndexSitemap: true, alternateRefs: [ { href: 'https://example.com/zh', hreflang: 'zh-cn' }, { href: 'https://example.com/en', hreflang: 'en-us' }, { href: 'https://example.com/ja', hreflang: 'ja-jp' }, ], transform: async (config, path) => { // 为每条路径注入多语言 alternate return { loc: path, lastmod: new Date().toISOString(), alternateRefs: config.alternateRefs.map(ref => ({ href: ref.href + path.replace(/^\/(zh|en|ja)/, ''), hreflang: ref.hreflang, })), }; }, };在 package.json 的 build 脚本后追加
&& next-sitemap;Vercel 构建时会自动生成 public/sitemap-zh.xml、public/sitemap-en.xml 及索引文件。 -
输出结构
public/sitemap-index.xml 内容示例:<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap><loc>https://example.com/zh/sitemap-zh.xml</loc></sitemap> <sitemap><loc>https://example.com/en/sitemap-en.xml</loc></sitemap> <sitemap><loc>https://example.com/ja/sitemap-ja.xml</loc></sitemap> </sitemapindex>每个语言文件内部再按 5 万条分页。
-
自动提交
创建 Vercel Serverless Function/api/submit-sitemap:import { google } from 'googleapis'; export default async function handler(req, res) { const auth = new google.auth.GoogleAuth({ keyFile: '/var/data/service-account.json', // 通过 Vercel ENV 注入 scopes: ['https://www.googleapis.com/auth/webmasters'], }); const webmasters = google.webmasters({ version: 'v3', auth }); const feeds = [ 'https://example.com/sitemap-index.xml', 'https://example.com/zh/sitemap-zh.xml', 'https://example.com/en/sitemap-en.xml', 'https://example.com/ja/sitemap-ja.xml', ]; for (const feed of feeds) { await webmasters.sitemaps.submit({ siteUrl: 'https://example.com/', feedpath: feed, }); } res.status(200).json({ ok: true }); }在 next-sitemap 生成完毕后,通过 GitHub Action 调用该函数,实现无人值守提交。
-
国内网络优化
- 将函数部署区域设为香港(HKG1),降低访问 Google API 的丢包率。
- 若公司政策禁止出境,可在构建机(阿里云香港 ECS)完成提交,再把结果回写 Slack。
-
验证
- Search Console 的“覆盖范围”报告应出现对应语言标签,无“备用页面无返回标记”警告。
- 使用 site:example.com/zh 搜索,结果标题下方出现“中文(简体)”标注,即表明 hreflang 生效。
拓展思考
- 若语言版本超过 50 种,sitemap-index.xml 本身会超限,此时需要二级索引:先按语言分组,再按日期/栏目拆分,最终层级为 sitemap-index → language-index → page-sitemap。
- 对于每日新增上万条 UGC 内容的平台,可放弃静态 XML,改用 Vercel Edge Function 动态输出 sitemap,缓存 1 小时,既满足实时性又避免构建超时。
- 百度与 Google 的差异:百度不支持 hreflang,需使用 <link rel="canonical"> + 独立二级域;此时应同时生成 baidu-sitemap.txt(每行一个 URL)并通过百度站长平台 API 推送,保持两套体系并行。
- 监测维度:把 Search Console 的“抓取异常”指标接入 Prometheus,结合 Vercel Analytics 的 LCP 数据,可快速判断“抓取失败”是网络问题还是边缘函数冷启动过慢,从而决定是扩容函数还是增加国内 CDN 节点。