I am trying to create sitemap with remote api in nuxt, but seems like promise.all not waiting for response of api and returns null object as result. I am using nuxtjs sitemap module for that job. Structure that i create at below:
import axios from 'axios'
import range from 'lodash/range'
export default {
sitemap: {
/**
* ...
*/
sitemaps: [
/**
* {...},
* {...},
*/
{
path: '/mg_chapters.xml',
/**
* ...
*/
sitemaps: async() => {
const base = "https://example.com",
a2 = "/api/sitemap/chapters?page=";
const fetcher = (index) =>
axios
.get(a2 + index)
.then((res) => res.data.data)
.then((els) =>
els.map((el) => ({
url: `${base}/cizgi-romanlar/${el}`,
/**
* ...
*/
}))
);
const sitemaps = async() => {
const len = await axios.get(a2 + 1).then(res => res.data.last_page)
return await Promise.all(
range(len).map(async(index) => ({
path: `/mg_chapters-${index + 1}.xml`,
routes: await fetcher(index + 1),
/**
* ...
*/
}))
);
}
return await sitemaps()
}
}
]
},
}
Weird thing is when i try this code block in async fetch() at a random vue file it works seemly, and returns expected result. Because of sitemap in nuxt.config file i can't see any error log, or what returns or not. Basically i am working on this job blindly and checking xml page. If you interested how expected result looks:
[
{
"path": "/mg_chapters-1.xml",
"routes": [
{
"url": "https://example.com/cizgi-romanlar/bolum-8",
"changefreq": "daily",
"lastmod": "2021-11-24T14:03:55.477Z"
}
],
"cacheTime": 86400000,
"trailingSlash": true,
"exclude": [
"/**"
]
}
]
So, in nuxt.config, my code block returns empty and not waiting for response. As far i check this is not a limitation of sitemap module.