Skip to content

Commit

Permalink
Merge pull request #4616 from jgable/siteMapOrder
Browse files Browse the repository at this point in the history
Sort newest to oldest in sitemap files
  • Loading branch information
jaswilli committed Dec 10, 2014
2 parents 910c3e3 + ef16c67 commit b81bde5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 21 deletions.
69 changes: 50 additions & 19 deletions core/server/data/sitemap/base-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ XMLNS_DECLS = {
function BaseSiteMapGenerator() {
this.lastModified = 0;
this.nodeLookup = {};
this.nodeTimeLookup = {};
this.siteMapContent = '';
}

Expand Down Expand Up @@ -53,11 +54,11 @@ _.extend(BaseSiteMapGenerator.prototype, {
return _.map(data, function (datum) {
var node = self.createUrlNodeFromDatum(datum, permalinks);
self.updateLastModified(datum);
self.nodeLookup[datum.id] = node;
self.updateLookups(datum, node);

return node;
});
}).then(self.generateXmlFromNodes);
}).then(this.generateXmlFromNodes.bind(this));
},

getPermalinksValue: function () {
Expand All @@ -77,14 +78,28 @@ _.extend(BaseSiteMapGenerator.prototype, {
this.permalinks = permalinks;

// Re-generate xml with new permalinks values
this.updateXmlFromNodes(_.values(this.nodeLookup));
},

generateXmlFromNodes: function (urlElements) {
var data = {
// Concat the elements to the _attr declaration
urlset: [XMLNS_DECLS].concat(urlElements)
};
this.updateXmlFromNodes();
},

generateXmlFromNodes: function () {
var self = this,
// Get a mapping of node to timestamp
timedNodes = _.map(this.nodeLookup, function (node, id) {
return {
id: id,
// Using negative here to sort newest to oldest
ts: -(self.nodeTimeLookup[id] || 0),
node: node
};
}, []),
// Sort nodes by timestamp
sortedNodes = _.sortBy(timedNodes, 'ts'),
// Grab just the nodes
urlElements = _.pluck(sortedNodes, 'node'),
data = {
// Concat the elements to the _attr declaration
urlset: [XMLNS_DECLS].concat(urlElements)
};

// Return the xml
return utils.getDeclarations() + xml(data);
Expand All @@ -103,19 +118,18 @@ _.extend(BaseSiteMapGenerator.prototype, {
return this.getPermalinksValue().then(function (permalinks) {
var node = self.createUrlNodeFromDatum(datum, permalinks);
self.updateLastModified(datum);
self.nodeLookup[datum.id] = node;
self.updateLookups(datum, node);

return self.updateXmlFromNodes(_.values(self.nodeLookup));
return self.updateXmlFromNodes();
});
},

removeUrl: function (datum) {
var lookup = this.nodeLookup;
delete lookup[datum.id];
this.removeFromLookups(datum);

this.lastModified = Date.now();

return this.updateXmlFromNodes(_.values(lookup));
return this.updateXmlFromNodes();
},

updateUrl: function (datum) {
Expand All @@ -124,9 +138,9 @@ _.extend(BaseSiteMapGenerator.prototype, {
var node = self.createUrlNodeFromDatum(datum, permalinks);
self.updateLastModified(datum);
// TODO: Check if the node values changed, and if not don't regenerate
self.nodeLookup[datum.id] = node;
self.updateLookups(datum, node);

return self.updateXmlFromNodes(_.values(self.nodeLookup));
return self.updateXmlFromNodes();
});
},

Expand All @@ -142,14 +156,18 @@ _.extend(BaseSiteMapGenerator.prototype, {
return 1.0;
},

getLastModifiedForDatum: function (datum) {
return datum.updated_at || datum.published_at || datum.created_at;
},

createUrlNodeFromDatum: function (datum, permalinks) {
var url = this.getUrlForDatum(datum, permalinks),
priority = this.getPriorityForDatum(datum);

return {
url: [
{loc: url},
{lastmod: moment(datum.updated_at || datum.published_at || datum.created_at).toISOString()},
{lastmod: moment(this.getLastModifiedForDatum(datum)).toISOString()},
{changefreq: CHANGE_FREQ},
{priority: priority}
]
Expand All @@ -161,11 +179,24 @@ _.extend(BaseSiteMapGenerator.prototype, {
},

updateLastModified: function (datum) {
var lastModified = datum.updated_at || datum.published_at || datum.created_at;
var lastModified = this.getLastModifiedForDatum(datum);

if (lastModified > this.lastModified) {
this.lastModified = lastModified;
}
},

updateLookups: function (datum, node) {
this.nodeLookup[datum.id] = node;
this.nodeTimeLookup[datum.id] = this.getLastModifiedForDatum(datum);
},

removeFromLookups: function (datum) {
var lookup = this.nodeLookup;
delete lookup[datum.id];

lookup = this.nodeTimeLookup;
delete lookup[datum.id];
}
});

Expand Down
27 changes: 25 additions & 2 deletions core/test/integration/sitemap_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ describe('Sitemap', function () {
});

generator.init().then(function () {
var idxFirst,
idxSecond,
idxThird;

should.exist(generator.siteMapContent);

// TODO: We should validate the contents against the XSD:
Expand All @@ -397,6 +401,14 @@ describe('Sitemap', function () {
validator.contains(generator.siteMapContent,
'<loc>http://my-ghost-blog.com/url/300</loc>').should.equal(true);

// Validate order newest to oldest
idxFirst = generator.siteMapContent.indexOf('<loc>http://my-ghost-blog.com/url/300</loc>');
idxSecond = generator.siteMapContent.indexOf('<loc>http://my-ghost-blog.com/url/200</loc>');
idxThird = generator.siteMapContent.indexOf('<loc>http://my-ghost-blog.com/url/100</loc>');

idxFirst.should.be.below(idxSecond);
idxSecond.should.be.below(idxThird);

done();
}).catch(done);
});
Expand Down Expand Up @@ -442,12 +454,15 @@ describe('Sitemap', function () {
});

generator.init().then(function () {
var idxFirst,
idxSecond,
idxThird;

should.exist(generator.siteMapContent);

// TODO: We should validate the contents against the XSD:
// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
// xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
// http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
// xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"

validator.contains(generator.siteMapContent,
'<loc>http://my-ghost-blog.com/url/100</loc>').should.equal(true);
Expand All @@ -467,6 +482,14 @@ describe('Sitemap', function () {
'<image:loc>http://my-ghost-blog.com/images/post-300.jpg</image:loc>')
.should.equal(true);

// Validate order newest to oldest
idxFirst = generator.siteMapContent.indexOf('<loc>http://my-ghost-blog.com/url/300</loc>');
idxSecond = generator.siteMapContent.indexOf('<loc>http://my-ghost-blog.com/url/200</loc>');
idxThird = generator.siteMapContent.indexOf('<loc>http://my-ghost-blog.com/url/100</loc>');

idxFirst.should.be.below(idxSecond);
idxSecond.should.be.below(idxThird);

done();
}).catch(done);
});
Expand Down

0 comments on commit b81bde5

Please sign in to comment.