You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Next.js中用next-sitemap配置多语言站点的路径过滤规则

配置next-sitemap实现多语言站点地图的特定规则

你可以通过next-sitemap.config.js中的exclude数组和transform函数结合,精准实现需求中的站点地图规则。以下是完整配置示例:

/** @type {import('next-sitemap').IConfig} */
module.exports = {
  siteUrl: 'https://your-domain.com', // 替换为你的实际站点域名
  generateRobotsTxt: true, // 可选,按需开启robots.txt生成
  exclude: [
    '/', // 排除根路径
    '/en/cancel', // 排除英文版本的cancel页面
    '/fr/cancel' // 排除法文版本的cancel页面
  ],
  transform: async (config, path) => {
    // 定义允许纳入站点地图的路径规则
    const isAllowed = 
      path === '/cancel' || 
      path.startsWith('/en/') || 
      path === '/en' || 
      path.startsWith('/fr/') || 
      path === '/fr';

    // 不符合规则的路径直接返回null,不纳入站点地图
    if (!isAllowed) {
      return null;
    }

    // 符合规则的路径生成标准站点地图条目
    return {
      loc: `${config.siteUrl}${path}`,
      changefreq: config.changefreq || 'daily',
      priority: config.priority || 0.7,
      lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
    };
  },
};

配置说明

  1. exclude数组:直接声明需要排除的特定路径,快速过滤根路径和带语言前缀的cancel页面。
  2. transform函数:做精细化的路径校验,确保只有/cancel、/en及子路径、/fr及子路径被保留。任何不符合规则的路径都会被过滤,不会出现在最终的站点地图中。

验证方式

运行next-sitemap命令生成站点地图后,查看项目根目录下的public/sitemap.xml文件,确认路径是否完全符合你的需求。

内容的提问来源于stack exchange,提问作者NroGamerz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 04:27:23