Skip to content

Commit

Permalink
Exclude sitemaps that are already in a sitemap index.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Oct 7, 2017
1 parent 25bc447 commit db48345
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/middleware/sitemapindex.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ exports = module.exports = function() {
sm.a('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')

pages = page.site.pages.filter(function(p) {
return p.sitemap == true;
return p.sitemap == true && !p._inSitemap;
});

for (i = 0, len = pages.length; i < len; i++) {
res = pages[i];

if (!res.fullURL) { return next(new Error('Unable to add "' + res.absoluteURL + '" to sitemap, set \'base url\' setting and try again')); }

if (res.fullURL) {
smm = sm.e('sitemap');
smm.e('loc', pages[i].fullURL);
}
smm = sm.e('sitemap');
smm.e('loc', res.fullURL);

res._inSitemap = page.absoluteURL;
}

var xml = sm.end({ pretty: true });
Expand Down
7 changes: 7 additions & 0 deletions test/sitemapindex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe('sitemapindex', function() {
before(function(done) {
chai.kerouac.use(sitemap.index())
.page(function(page) {
page.absoluteURL = '/sitemap_index.xml';

page.site = new mock.Site();
page.site.pages = [
{ url: '/hello', fullURL: 'http://www.example.com/hello' },
Expand Down Expand Up @@ -49,6 +51,11 @@ describe('sitemapindex', function() {
it('should not set sitemap property', function() {
expect(page.sitemap).to.equal(undefined);
});

it('should add sitemaps to sitemap index', function() {
expect(page.site.pages[0]._inSitemap).to.equal(undefined);
expect(page.site.pages[1]._inSitemap).to.equal('/sitemap_index.xml');
});
}); // with one sitemap

describe('with two sitemaps', function() {
Expand Down
6 changes: 6 additions & 0 deletions test/urlset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe('urlset', function() {
before(function(done) {
chai.kerouac.use(sitemap())
.page(function(page) {
page.absoluteURL = '/sitemap.xml'

page.site = new mock.Site();
page.site.pages = [
{ url: '/', fullURL: 'http://www.example.com/' }
Expand Down Expand Up @@ -44,6 +46,10 @@ describe('urlset', function() {
it('should set sitemap property', function() {
expect(page.sitemap).to.equal(true);
});

it('should add pages to sitemap', function() {
expect(page.site.pages[0]._inSitemap).to.equal('/sitemap.xml');
});
}); // with one page

describe('with one page, including lastmod', function() {
Expand Down

0 comments on commit db48345

Please sign in to comment.