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

Nuxt.js如何为动态路由的sitemap.xml添加lastmod等属性

解决方案

nuxt/sitemap 模块的 routes 配置项支持返回对象数组,无需仅返回路径字符串,你可以直接在返回的对象中定义站点地图需要的额外属性即可。

修改后的配置示例

// nuxt.config.js
// 顶部需提前引入axios,如果你原有配置已经引入可忽略
import axios from 'axios'

export default {
  // 其他项目配置...
  /* sitemap XML */
  sitemap: {
    hostname: 'example.com',
    gzip: true,
    exclude: [
      '/user/**',
      '/admin/**',
      '/user',
      '/admin',
      '/booking',
      '/sales',
      '/payment',
      '/payment/**',
      '/search-result'
    ],
    // 可选:全局默认属性,所有路由默认继承该配置,不需要重复定义的话可以加这个字段
    defaults: {
      changefreq: 'daily',
      priority: 0.7,
      lastmod: new Date()
    },
    routes:async () => {
      const { data } = await axios.get('/api/book')
      return data.data.map((product) => {
        return {
          // 对应<loc>标签,必填
          url: `/product/${product.id}`,
          // 对应<lastmod>标签,建议用商品实际更新时间,没有的话也可以自定义时间
          lastmod: product.updatedAt || new Date(),
          // 对应<changefreq>标签,可选值:always/hourly/daily/weekly/monthly/yearly/never
          changefreq: 'weekly',
          // 对应<priority>标签,取值范围0-1
          priority: 0.9
        }
      })
    },
  },
}

注意事项

  • 动态路由单独配置的属性优先级高于全局defaults配置,重复字段会覆盖默认值。
  • lastmod字段传入Date对象或者ISO标准时间字符串都可以被模块自动正确格式化。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 03:51:01