0

I've been trying to generate sitemaps from API for my Nextjs app by following this guide. I fetch data, put in a list like this:

const formatted = sitemap => prettier.format(sitemap, { parser: "html" });

(async () => {
  const fetchInfo = await fetch(fetchUrl)
    .then(res => res.json())
    .catch(err => console.log(err));


  const postList = [];
  const totalPages = fetchInfo.total_pages
  pageNumber = 1
  while (pageNumber < totalPages + 1) {
    const fetchposts = await fetch(fetchUrl+ `?page=${pageNumber}`)
    .then(res => res.json())
    .catch(err => console.log(err));
  
    fetchposts.results.forEach(post => postList.push(post.slug));
    console.log(postList);

    pageNumber ++;


  }

  const postListSitemap = `
  ${postList
    .map((slug,index) => {
      console.log(index);
      return `
        <url>
          <loc>${`${YOUR_AWESOME_DOMAIN}/posts/${slug}`}</loc>
          <lastmod>${getDate}</lastmod>
        </url>`;
    })
    .join("")}
`;

  const generatedSitemap = `
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
    >
      ${postListSitemap}
    </urlset>
  `;

  const formattedSitemap = [formatted(generatedSitemap)];

  fs.writeFileSync("public/sitemap-posts.xml", formattedSitemap, "utf8");
})();

First code works just fine, I also print postList while doing it and it shows all items but then when it starts to generate the sitemap I get UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded error. I'm not good in Javascript so not sure how to fix it.

stackyname
  • 516
  • 5
  • 18
  • Do you have any recursive function ? Maybe I am wrong but I can't see any instruction leading to a `Maximum call stack size exceeded` error. Because even `map` and `forEach` cannot loop forever – A.DUPONCHEL Nov 04 '20 at 14:54
  • @A.DUPONCHEL Sorry, I actually had some updates I made in my code. Now I updated my post accordingly. Basically I try to add all data from each page of pagination to the list – stackyname Nov 04 '20 at 15:00
  • `UnhandledPromiseRejectionWarning` Only happens when a promise fails but no catch callback was provided. Here all promise is correctly handled, so I guess it come from somewhere else. Maybe [this topic](https://stackoverflow.com/questions/43834559/how-to-find-which-promises-are-unhandled-in-node-js-unhandledpromiserejectionwar/47895841) could help you to find where it is happen. – A.DUPONCHEL Nov 04 '20 at 15:08
  • @A.DUPONCHEL Thank you so much. The solution in the link you gave me raised same error. I updated my post again with the whole code. I added index here: `postList.map((slug,index)`. Then logged it. It matches the total pages numbers so I guess no problem with mapping. So the problem must be the next code which writes the data into xml file – stackyname Nov 04 '20 at 15:46

0 Answers0