BATMAN_2008
BATMAN_2008

Reputation: 3530

Nuxt sitemap adding the default lastmod for the dynamic routes during the sitemap.xml generation

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?

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 have created a sample re-production in CodeSandBox.

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" }],
};

Upvotes: 1

Views: 240

Answers (1)

Flopont
Flopont

Reputation: 146

If present from the file then add it directly if not then add the lastest or current date time lastmod: new Date()

I would recommend against setting lastmod to any kind of default value. If you are unable to accurately determine the last modification date of a page, simply do not insert the <lastmod> tag in the XML sitemap, i.e. don't do anything :)

This can be inferred from Google's XML sitemap documentation (emphasis mine):

The <lastmod> value should reflect the date and time of the last significant update to the page. For example, an update to the main content, the structured data, or links on the page is generally considered significant, however an update to the copyright date is not.

But it is further clarified and expanded on in the Search Engine Journal article "Google’s Gary Illyes: Lastmod Signal Is Binary" (emphasis mine):

Google either accepts the lastmod dates provided in a site’s sitemap as accurate, or it disregards them.

This binary approach reinforces the need to implement the lastmod tag correctly and only specify dates when making meaningful changes.

Finally, if you insist on having a lastmod value for your Tag pages, I would suggest using the publication date of the most recent article listed in each Tag page, because (in a generic blog logic) Tag pages are auto-updated every time an article is added to the Tag. However, this will require more work with little to no gain in terms of SEO.

Hope this helps!

Upvotes: 1

Related Questions