Astro sitemap 怎么做

在 Astro 里怎么生成、校验、提交 sitemap.xml——包括 hreflang 配对、排除规则、以及 Google 实际怎么用它。

一份正确的 sitemap 是性价比最高的 SEO 改动。错的那种会悄悄漏页或者给 Google 喂死链。

问题背景

Astro 官方 @astrojs/sitemap 集成开箱覆盖 90% 场景。剩下 10%——排除、多语言配对、lastmod——是最容易出问题的地方。一开始就处理好,可以省后面无数轮收录问题。

判断标准

  • 站点有超过 30 个想被收录的 URL。
  • 有多语言或区域版本。
  • 有草稿、隐藏、登录后才看的页面,绝对不能让搜索引擎收录。
  • 希望内容变更时给 Google 新鲜的 lastmod 信号。

快速结论

@astrojs/sitemap 打底,通过 filterserialize 选项处理排除与 hreflang。

实操步骤

  1. 装官方集成:
npx astro add sitemap
  1. astro.config.mjs——site: 不填,集成会悄悄生成废文件。双语内容站可用的形态:
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';

export default defineConfig({
  site: 'https://yourdomain.com',
  trailingSlash: 'always',
  build: { format: 'directory' },
  integrations: [
    sitemap({
      i18n: {
        defaultLocale: 'en',
        locales: { en: 'en', zh: 'zh-CN' },
      },
      filter: (page) =>
        !page.includes('/drafts/')
        && !page.includes('/preview/')
        && !page.includes('/admin/')
        && !page.endsWith('/404/'),
      serialize: (item) => {
        // 首页 daily、文章 weekly、其余 monthly
        if (item.url.match(/\/articles\/[^/]+\/$/)) item.changefreq = 'weekly';
        else if (item.url === 'https://yourdomain.com/') item.changefreq = 'daily';
        else item.changefreq = 'monthly';
        return item;
      },
    }),
  ],
});
  1. 构建后确认 sitemap 文件存在:
npm run build
ls -la dist/sitemap*.xml
# dist/sitemap-index.xml
# dist/sitemap-0.xml
head -20 dist/sitemap-0.xml
  1. 生成的 XML 应该是这种形态——每对 URL 带 xhtml:link 作为 hreflang:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <url>
    <loc>https://yourdomain.com/en/articles/astro-sitemap-setup/</loc>
    <xhtml:link rel="alternate" hreflang="en"
                href="https://yourdomain.com/en/articles/astro-sitemap-setup/" />
    <xhtml:link rel="alternate" hreflang="zh-CN"
                href="https://yourdomain.com/zh/articles/astro-sitemap-setup/" />
    <changefreq>weekly</changefreq>
  </url>
</urlset>
  1. public/robots.txt 里指向 sitemap,省一道手动提交:
User-agent: *
Allow: /

Disallow: /drafts/
Disallow: /preview/
Disallow: /admin/

Sitemap: https://yourdomain.com/sitemap-index.xml
  1. 部署完拉 sitemap 数 URL,跟文章数对一下:
curl -s https://yourdomain.com/sitemap-index.xml | grep -c '<loc>'
curl -s https://yourdomain.com/sitemap-0.xml | grep -c '<loc>'
# 大致等于(en 文章 + zh 文章 + hub 页)
  1. sitemap-index.xml 提交一次到 Search Console → Sitemaps。之后除非 URL 变化否则不用再提交——Google 自己轮询。

容易踩的坑

  • 忘配 site:——Astro 不报错,但生成的 sitemap 是废的。
  • 把会 404 或重定向的页面列进去——每条错 URL 都在浪费 crawl budget。
  • 把草稿或测试 URL 写进去,被收录后才发现。
  • 所有页面 lastmod 都设为构建日期,Google 当成噪声直接忽略。
  • 多语言每种语言提一份 sitemap,其实带 hreflang 的 sitemap index 更干净。

这篇适合谁

任何靠自然搜索的 Astro 站。

这篇不适合谁

私有内部工具或单页站,sitemap 反而是噪声。

FAQ

  • Astro 会自动生成 sitemap 吗: 只有装了 @astrojs/sitemap 才会。原生 Astro 不会。
  • 一份 sitemap 最大多大: 单文件最多 50,000 URL 或 50 MB。Astro 集成会自动用 sitemap index 拆分。
  • sitemap 变了要不要 ping Google: 不用,那个接口已经废弃。保持 sitemap 新鲜就行,Google 按自己节奏抓。
  • 想让某些页面不在 sitemap 里怎么办: filter 选项排除,同时在页面上加 noindex,双保险。

相关阅读

标签: #独立开发 #Astro #SEO #Technical SEO #收录