nuxt-modules / sitemap

Powerfully flexible XML Sitemaps that integrate seamlessly, for Nuxt.
https://nuxtseo.com/sitemap
312 stars 28 forks source link

help: Nuxt sitemap adding the default lastmod for the dynamic routes during the sitemap.xml generation #366

Open Aravinda93 opened 16 hours ago

Aravinda93 commented 16 hours ago

📚 What are you trying to do?

I am generating the sitemap.xml for my Nuxt 3 based Nuxt Content website pages and everything works fine and able to get the correct sitemap after running the npm run generate or nuxt generate. However I am unable to add the default value for lastmod for dynamically created routes. I have created a sample re-production in CodeSandBox.

My /content/imprint/index.md file looks like this:

---
title: "Imprint"
description: "Imprint/Registration information"
navigation:
    linkTitle: "Imprint"
    tags : ["convert", "compliance", "json"]
head:
  meta:
    - name: 'keywords'
sitemap:
  loc: /imprint
  lastmod: 2024-08-31
  changefreq: monthly
  priority: 1
---

And I get the following information within my sitemap.xml like this:

<url>
    <loc>https://localhost:3003/imprint</loc>
    <lastmod>2024-08-31</lastmod>
    <changefreq>monthly</changefreq>
    <priority>1</priority>
    <xhtml:link rel="alternate" href="https://localhost:3003/imprint" hreflang="x-default" />
    <xhtml:link rel="alternate" href="https://localhost:3003/imprint" hreflang="en" />
</url>
<url>
    <loc>https://localhost:3003/tags/convert</loc>
    <xhtml:link rel="alternate" href="https://localhost:3003/tags/convert" hreflang="x-default" />
    <xhtml:link rel="alternate" href="https://localhost:3003/tags/convert" hreflang="en" />
</url>
<url>
    <loc>https://localhost:3003/tags/compliance</loc>
    <xhtml:link rel="alternate" href="https://localhost:3003/tags/compliance" hreflang="x-default" />
    <xhtml:link rel="alternate" href="https://localhost:3003/tags/compliance" hreflang="en" />
</url>
<url>
    <loc>https://localhost:3003/tags/json</loc>
    <xhtml:link rel="alternate" href="https://localhost:3003/tags/json" hreflang="x-default" />
    <xhtml:link rel="alternate" href="https://localhost:3003/tags/json" hreflang="en" />
</url>

As we can see since I defined the lastmod for my imprint/index.md page I got the lastmod for only for that URL but for all the tags or dynamic routes which get created in the sitemap.xml it does not get the lastmod or changeFreq added by default to it.

I want to know how can I add the default lastmod value to all the dynamic tags routes during the generation of the sitemap.xml? If present from the file then add it directly if not then add the lastest or current date time lastmod: new Date(),

I tried to add the defaults to my nuxt.config.js:

  sitemap: {
    hostname: process.env.NUXT_PUBLIC_SITE_URL,
    gzip: true,
    trailingSlash: true,
    defaults: {
      changefreq: "daily",
      priority: 1,
      lastmod: new Date(),
    },
  },

But this did not make any difference and I did not get the lastmod for the /tags. How to ensure if the lastmod to be added for all the URLS generated in sitemap.xml including the dynamic routes?

Following is my nuxt.config.js:

Click to toggle contents of `nuxt.config.js` ``` export default { compatibilityDate: "2024-08-31", modules: [ "@nuxtjs/tailwindcss", "unplugin-fonts/nuxt", "@nuxtjs/i18n", "@nuxtjs/color-mode", "@nuxt/image", "@nuxt/content", "@nuxtjs/sitemap", ], ssr: true, target: 'static', site: { url: process.env.BASE_URL || 'http://localhost:3000/', name: "Test Application", trailingSlash: true, defaults: { changefreq: "daily", priority: 1, lastmod: new Date(), }, }, //To support and display the .md files from /content using @nuxt/content content: { // To highlight the code sections using the theme highlight: { theme: { default: "aurora-x", dark: "everforest-dark", sepia: "monokai", }, langs: ["json", "xml", "java", "shell"], }, markdown: { remarkPlugins: ["remark-reading-time"], //To get the reading time for each .md file in /content anchorLinks: false, // Do not underline and highlight the h2, h3, h4 etc } }, //To support the dark/light mode theme using @nuxtjs/color-mode colorMode: { classSuffix: "", preference: "system", fallback: "dark", }, app: { head: { link: [ { rel: "icon", type: "image/x-icon", href: `/img/favicon.ico`, }, ], }, }, buildModules: [ { vue: { config: { assetDirs: ["assets", "public"], }, }, }, ], runtimeConfig: { apiSecret: "123", public: {}, }, components: [ { path: "~/components", pathPrefix: false, }, ], build: { postcss: { postcssOptions: { plugins: { tailwindcss: {}, autoprefixer: {}, }, }, }, }, i18n: { locales: [ { code: "en", files: ["en.json", "en-extended.json"], }, ], lazy: true, langDir: "./locales", defaultLocale: "en", }, plugins: [{ src: "@/plugins/aos", ssr: false, mode: "client" }], }; ```

🔍 What have you tried?

I tried couple of things already:

  sitemap: {
    hostname: process.env.NUXT_PUBLIC_SITE_URL,
    trailingSlash: true,
    lastmod: new Date(),
    defaults: {
      lastmod: new Date()
    }
  },

Also added custom routes logic:

 sitemap: {
    filter ({ routes }) {
      return routes.map(route => {
        route.url = `${route.url}/`
        route.lastmod = new Date()
        return route
      })
    }
  }

I did not get even console.log when generating the sitemap:

site: {
    url: process.env.NUXT_PUBLIC_SITE_URL,
  },

  sitemap: {
    hostname: process.env.NUXT_PUBLIC_SITE_URL,
    gzip: true,
    trailingSlash: true,
    generate: false,
    defaults: {
      changefreq: 'daily',
      priority: 1,
      lastmod: new Date(),
    },
    filter({ routes }) {
      return routes.map(route => {
        console.log("*************************")
        console.log(route)
        console.log("*************************")
        route.lastmod = route.lastmod || new Date();
        return route;
      });
    }
  },

I have created a sample re-production in CodeSandBox.

ℹī¸ Additional context

No response