I’m trying to generate a sitemap. I have a collection Quicks
that contains about a handful of search shortcuts. Each of this Quicks
is rendered as an URL in the sitemap.
For each quick I’d like to get the date of the last upload (to the collection EboyPix
) — and use it as the lastmod
property for the sitemap.
When iterating through Quicks
the first two return undefined
for the createdAt
field. As if function getLatestQuickDate
needed a bit of time to get going.
What can I do? This script sits on /server/sitemap.js
Thanks!
// Meteor stuff
import { sitemaps } from 'meteor/gadicohen:sitemaps';
// Collections
import { Quicks } from '../imports/api/quicks/quicks.js'; // search shortcuts
import { EboyPix } from '../imports/api/eboypix/eboypix.js';
// Returns the date of the last upload matching the slug
function getLatestQuickDate(slug) {
const latestSlug = EboyPix.findOne(
{ tags: slug },
{ sort: { createdAt: -1, limit: 1 } }
);
if (latestSlug) { // is undefined for for first two quicks
return latestSlug.createdAt;
}
}
// Return array of pages from Quicks collection
const quicksLabels = Quicks.find().fetch();
const quicksLinks = function() {
let quicksPagesAll = [];
if (quicksLabels) {
Object.keys(quicksLabels).forEach(function(key) {
const slug = quicksLabels[key].slug.toString();
const lastmod = getLatestQuickDate(slug);
const quicksPage = {
page: 'pool/' + quicksLabels[key].label + '/1',
changefreq: 'weekly',
priority: quicksLabels[key].rank,
lastmod: lastmod
};
quicksPagesAll.push(quicksPage);
});
return quicksPagesAll;
}
};
sitemaps.add('/sitemap.xml', function() {
return quicksLinks();
});