diff --git a/src/routes/sitemap.xml.js b/src/routes/sitemap.xml.js index 325a300..35a30b1 100644 --- a/src/routes/sitemap.xml.js +++ b/src/routes/sitemap.xml.js @@ -34,6 +34,8 @@ const render = (pages, locations) => { ${baseURL}/location/${loc.country.slug}/${loc.slug} 1 + ${loc.updated} + weekly `).join('\n')} ` } @@ -45,13 +47,24 @@ export async function get (req, res, next) { // Get locations const locationsReq = await fetch(`${apiEndpoints.rest}/items/locations?fields=slug,country.slug`) - const locations = await locationsReq.json() + const locationsData = await locationsReq.json() + const locations = locationsData.data + + // Add last modified date to each location from its last photo + const updatedLocations = locations.map(async (location, i) => { + const latestPhotoReq = await fetch(`${apiEndpoints.rest}/items/photos?fields=created_on&limit=1&sort=created_on&filter[location.slug][rlike]=%${location.slug}`) + const latestPhotoData = await latestPhotoReq.json() + const latestPhoto = latestPhotoData.data[0] + location.updated = latestPhoto.created_on + return location + }) + await Promise.all(updatedLocations) // Set headers res.setHeader('Cache-Control', 'max-age=0, s-max-age=600') // 10 minutes res.setHeader('Content-Type', 'application/rss+xml') // Render sitemap - const sitemap = render(pages, locations.data) + const sitemap = render(pages, locations) res.end(sitemap) }