Generate sitemap xml in gatsby with dynamic storyblok urls

151 views Asked by At

I like to generate the sitemap.xml dynamically like list of cities added in storyblok based on the states, I have done the cities pages url dynamically by using createPage.

if (page.path.match(/^\/tn/)) {
    page.matchPath = "/tn/*";
} else if (page.path.match(/^\/ap/)) {
    page.matchPath = "/ap/*";
}
createPage(page);

But I am not able to generate in sitemap.xml

I am trying add these cities as constants for hack and generate sitemap.xml but thats also not happening, can anyone help me how to generate the sitemap.xml using constants in the gatsconfig.js

my config

    resolve: `gatsby-plugin-sitemap`,
    options: {
        query: `
      {
        site {
          siteMetadata {
            siteUrl
          }
        }
      }
    `,
        resolveSiteUrl: ({
            site: {
                siteMetadata: {
                    siteUrl
                }
            }
        }) => siteUrl,
        resolvePages: () => {
            console.log("coming -------")
            const cities = ['test', 'test2'].map(city => {
                return {
                    path: `/tn/${city}`,
                    changefreq: 'weekly',
                    priority: 0.7,
                }
            })
            console.log("cities", cities);
            return [...cities]
        },
        serialize: ({
            path,
            changefreq,
            priority
        }) => {
            return {
                siteUrl: path,
                changefreq,
                priority,
            }
        },
    }
}```
1

There are 1 answers

0
jesusrlm On

Here is the configuration that I use:

  resolve: "gatsby-plugin-sitemap",
  options: {
    output: "/",
    query: `
      {
        site {
          siteMetadata {
            siteUrl
          }
        }
        allSitePage {
          nodes {
            path
          }
        }
      }
    `,
    resolveSiteUrl: ({ site }) => {
      return site.siteMetadata.siteUrl;
    },
    serialize: ({ path }) => {
      return {
        url: path,
      };
    },
    createLinkInHead: true,
  },