Pre-Fetch number of sitemaps

asammitte
0
asammitte
commented 2 months ago

Trying to fetch number of sitemaps that should be created.

I have some amount of records for each entity. I want to prefetch this number to be able to add required amount to range.

Did try to fetch range amount with anonymous function but no luck.

// nuxt.config
sitemap: { path: '/sitemap.xml', cacheTime: 1000 * 60, sitemaps: [ ...lodash.range(30).map(index => ({ path:8a-sitemaps-e4532/8a-crags-sitemap${index + 1}.xml, routes: async () => { const { data } = await axios.get(${API_URL}/api/sitemap/products, { params: { pageIndex: index } }) console.log(data) return data }, cacheTime: 1000 * 60, trailingSlash: true, gzip: true, exclude: ['/**'], })) ] },

0
NicoPennec
2.0k
NicoPennec
commented 2 months ago

hi @asammitte,

Your script looks good to me.
The only detail that is unknown to me concerns your data object.
The return value of the routes function must be an array of string with URLs or an array of object with a url property.

eg.

data = ["/foo", "/bar"]

// or

data = [
  { url: "/foo" },
  { url: "/bar"},
]

Can you show me the output from your console.log(data)?

0
asammitte
0
asammitte
commented 2 months ago

Hi @NicoPennec
The { data } returns the array of strings. Actually the code I show is working perfect. The question is to determine number of sitemap files. In the code from first comment I'm using hardcoded number 30. Instead of this hardcoded value I want to fetch data: something like this:

sitemap: { path: '/sitemap.xml', cacheTime: 1, sitemaps: [ ...range( axios .get(${APIURL}/api/sitemap/main, { params: { entityType: 2 } }) .then(response => response && response.data) .then(count => { console.log(count) // <----- number of sitemap files needed fo the products return count }) .catch(e => {}) ).map(index => ({ path:8a-sitemaps-e4532/8a-products-sitemap${index + 1}.xml, routes: async () => { try { const { data } = await axios.get(${APIURL}/api/sitemap/products , { params: { pageIndex: index } }) console.log(data) // <---- array of strings return data } catch (ex) { console.log(ex) } }, cacheTime: 1000 * 60, trailingSlash: true, gzip: true, exclude: ['/**'], })) ] }

The question is how to prefetch number of products to calculate number of sitemaps. If I'll hardcode some number in range - it means that there can be not enough sitemaps fo products entity or too much which will result some amount of empty sitemap files. I want to have in range exactly the number of sitemaps needed for the products.

0
NicoPennec
2.0k
NicoPennec
commented a month ago

Ok, I understand you request better ;-)

From the sitemap-module point of view, it's hard to know the number of sitemaps to create in a sitemap-index. Your solution to calculate the range on API side seems the right way for now.

0
Informations
Question โ€ข Unresolved
#c88 - Created 2 months ago