Sitemap.xml: Add last updated date

Get each location last photo created date as the latest content added
This commit is contained in:
2020-04-03 21:07:41 +02:00
parent 5a49bc7b5f
commit 76e2f8242e

View File

@@ -34,6 +34,8 @@ const render = (pages, locations) => {
<url>
<loc>${baseURL}/location/${loc.country.slug}/${loc.slug}</loc>
<priority>1</priority>
<lastmod>${loc.updated}</lastmod>
<changefreq>weekly</changefreq>
</url>`).join('\n')}
</urlset>`
}
@@ -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)
}