sitemap.xml

2026年9月6日

sitemap 是给爬虫的 URL 清单,可附带修改时间、频率、优先级提示。禁止抓取靠 robots / noindex,不要指望在 sitemap 里「反向删除」。

新站、层级深、内链弱时,sitemap 能明显提高被发现的机会。进不进索引仍看内容质量。

协议:sitemaps.org。Google 对单个文件的实践上限大约是 5 万条 URL、50MB 未压缩,多了就拆。


常用字段

字段必填说明
loc绝对 URL,& 写成 &
lastmod2026-04-20 或带时区的时间
changefreqalways / hourly / daily / weekly / monthly / yearly / never
priority站内相对权重 0.0–1.0,默认 0.5

Google 不会changefreq / priority 决定抓取周期或排序,当提示即可,别造假 lastmod

图片、视频是扩展命名空间:image:locvideo:title / thumbnail_loc / description,以及 content_locplayer_loc 至少其一。详见 图片 sitemap视频 sitemap


sitemap.ts

import type { MetadataRoute } from "next";

export default function sitemap(): MetadataRoute.Sitemap {
  return [
    {
      url: "https://example.com",
      lastModified: new Date(),
      changeFrequency: "yearly",
      priority: 1,
      images: ["https://example.com/og.png"],
    },
    {
      url: "https://example.com/blog",
      lastModified: new Date(),
      changeFrequency: "weekly",
      priority: 0.8,
    },
  ];
}
ts

访问 /sitemap.xml。本站用的是 Pages Router 下的 src/pages/sitemap.xml.tsx,道理一样:扫博客和 docs/ 拼绝对地址。


多份:generateSitemaps

文章、商品各自上万条时,拆子图,根路径出索引:

import type { MetadataRoute } from "next";

export async function generateSitemaps() {
  return [{ id: "posts" }, { id: "docs" }];
}

export default async function sitemap({
  id,
}: {
  id: Promise<string>;
}): Promise<MetadataRoute.Sitemap> {
  const key = await id;
  // 按 key 查库或扫目录
  return [
    {
      url: `https://example.com/${key}/1`,
      lastModified: new Date(),
      changeFrequency: "weekly",
      priority: 0.6,
    },
  ];
}
ts

子图地址形如 /sitemap/posts.xml。提交给 Search Console 时优先交索引文件 /sitemap.xml

下一篇:TDK,也就是 title / description / keywords。


参考文档