Creating sitemap index based on dynamic sitemaps

JohanneA
0
JohanneA
commented 6 months ago

I am trying to create a sitemapindex based on a total amount of pages returned by a request to my CMS. However, when I try I get this error The path option is either empty or missing in your config for a sitemap. Which makes sense, since it isn't expecting an async function for the configuration.

I am not quite sure how to solve my problem though. Is there a way to dynamically generate the sitemap?

This is what I'm trying to do:

const getNewsItemSiteMaps = () =>
    newsItemRequest().then((response) => {
      const totalPages = response.total_pages;

      return range(totalPages).map(it => (
        {
          path: `sitemap-posts-${it}.xml`,
          routes: () => getNewsItemRoutes(it + 1),
          cacheTime: 1000 * 60 * 60 * 2,
          exclude: ["/**"]
        }
      ));
    });

nuxt.config.js

sitemap: {
...
  sitemaps: [
    getNewsItemSiteMaps()
  ]

}
0
NicoPennec
2.1k
NicoPennec
commented 6 months ago

Can you share all of your sitemap config?

Have you try to set the sitemaps array directly with the getNewsItemSiteMaps function?

  sitemaps: getNewsItemSiteMaps()

or

  sitemaps: () => getNewsItemSiteMaps()
0
JohanneA
0
JohanneA
commented 5 months ago

Thank you very much and sorry for the late reply.

Here's the full sitemap:

  sitemap: {
    defaults: {
      changefreq: "daily",
      priority: 0.5,
      lastmod: new Date(),
      lastmodrealtime: true
    },
    hostname: "https://kambr.media",
    cacheTime: 1000 * 60 * 15,
    gzip: true,
    sitemaps: [
      {
        defaults: {
          lastmod: new Date(),
          lastmodrealtime: true
        },
        path: "/sitemap-pages.xml",
        routes: getStaticSites()
      },
      {
        path: "/sitemap-authors.xml",
        routes: () => getAuthors(),
        exclude: ["/**"]
      },
      {
        path: "/sitemap-news.xml",
        routes: () => getNews(),
        exclude: ["/**"]
      },
      ...getNewsItemSiteMaps()
    ]
  },

Where the getNewsItemSiteMaps() looks like this:

export const getNewsItemSiteMaps = () =>
//   const api = await Prismic.getApi(apiEndpoint);
//   const totalPages = (await newsItemRequest(api)).total_pages;

// TODO: Fix this when we figure out how to do dynamic sitemap generation
  range(1).map((it: any) => (
    {
      path: `sitemap-posts-${it + 1}.xml`,
      routes: () => getNewsItemRoutes(it + 1),
      cacheTime: 1000 * 60 * 60 * 2,
      exclude: ["/**"]
    }
  ));

I tried sitemaps: () => getNewsItemSiteMaps() with the promise, but then it doesn't even generate the sitemapindex.

0
Informations
Question โ€ข Unresolved
#c79 - Created 6 months ago