javascriptreactjsgatsbysitemapgatsby-plugin

gatsby-plugin-sitemap Putting trailing slash to homepage only


Hello Guys I am using plugin above to create a sitemap for Gatsby website, and removing forward slashes from all urls, but when I check the sitemap it is putting trailing slash to the homepage only here is the code.

serialize: ({ path }) => {
      let domain = process.env.url || 'https://example.com';
      domain =
        domain.charAt(domain.length - 1) === '/' ? domain.slice(1) : domain;
      let pathStr =
        path.charAt(path.length - 1) === '/' ? path.slice(0, -1) : path;
      let url = domain + pathStr;
      console.log(url);
      return {
        url,
        changefreq: 'daily',
        priority: 0.7
      };
    }

url is getting displayed in console, and there is no any trailing slashes, but in sitemap there is.


Solution

  • With the v4.7 of Gatsby, there's a built-in option to customize the behavior of the trailing slashes across your application. It's currently in beta but should fix your issue.

    trailingSlash Option Currently in Public Beta

    Through the RFC Integrated handling of trailing slashes in Gatsby we’ve worked on making the trailing slashes feature a first-class citizen in Gatsby. We’re happy to announce that gatsby-config now supports a trailingSlash configuration with these three main options:

    • always: Always add trailing slashes to each URL, e.g. /x to /x/.

    • never: Remove all trailing slashes on each URL, e.g. /x/ to /x.

    • ignore: Don’t automatically modify the URL.

    You can simply customize it using:

    module.exports = {
      trailingSlash: "never"
    }