Sitemap: Fix updated location date from latest photo
- Sorting was wrong - Simplify requests
This commit is contained in:
@@ -45,20 +45,26 @@ const render = (pages, locations) => {
|
|||||||
|
|
||||||
// Get function
|
// Get function
|
||||||
export async function get (req, res, next) {
|
export async function get (req, res, next) {
|
||||||
|
let locations
|
||||||
|
|
||||||
// Define base url from request
|
// Define base url from request
|
||||||
baseURL = `https://${req.headers.host}`
|
baseURL = `https://${req.headers.host}`
|
||||||
|
|
||||||
// Get locations
|
// Get locations
|
||||||
const locationsReq = await fetch(`${apiEndpoints.rest}/items/locations?fields=slug,country.slug,modified_on&status=published`)
|
await fetch(`${apiEndpoints.rest}/items/locations?fields=slug,country.slug,modified_on&status=published&sort=created_on`)
|
||||||
const locationsData = await locationsReq.json()
|
.then(res => res.json())
|
||||||
const locations = locationsData.data
|
.then(res => {
|
||||||
|
locations = res.data
|
||||||
|
})
|
||||||
|
|
||||||
// Add last modified date to each location from its last photo
|
// Add last modified date to each location from its last photo
|
||||||
const updatedLocations = locations.map(async (location, i) => {
|
const updatedLocations = locations.map(async (location, i) => {
|
||||||
const latestPhotoReq = await fetch(`${apiEndpoints.rest}/items/photos?fields=created_on&limit=1&sort=created_on&status=published&filter[location.slug][rlike]=%${location.slug}`)
|
await fetch(`${apiEndpoints.rest}/items/photos?fields=created_on&limit=1&sort=-created_on&status=published&filter[location.slug][rlike]=%${location.slug}`)
|
||||||
const latestPhotoData = await latestPhotoReq.json()
|
.then(res => res.json())
|
||||||
const latestPhoto = latestPhotoData.data[0]
|
.then(res => {
|
||||||
location.updated = latestPhoto ? latestPhoto.created_on : location.modified_on
|
const latestPhoto = res.data[0]
|
||||||
|
location.updated = latestPhoto ? latestPhoto.created_on : location.modified_on
|
||||||
|
})
|
||||||
return location
|
return location
|
||||||
})
|
})
|
||||||
await Promise.all(updatedLocations)
|
await Promise.all(updatedLocations)
|
||||||
|
|||||||
Reference in New Issue
Block a user