-
-
Save chiedo/cd1e88ba3db440eb1f44fa6aaf8c3bd5 to your computer and use it in GitHub Desktop.
Sitemap for Gatsby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs-extra-promise') //install this package | |
var sm = require('sitemap') // install this package | |
function pagesToSitemap(pages) { | |
var urls = pages.map(function(p) { | |
if (p.path !== undefined) { | |
return { | |
url: p.path, | |
changefreq: 'daily', | |
priority: 0.7 | |
} | |
} | |
}) | |
// remove undefined (template pages) | |
return urls.filter(function(u) { return u !== undefined}) | |
} | |
function generateSiteMap(pages) { | |
var sitemap = sm.createSitemap({ | |
hostname: 'https://www.example.com', | |
cacheTime: '60000', | |
urls: pagesToSitemap(pages), | |
}) | |
console.log('Generating sitemap.xml') | |
fs.writeFileSync( | |
`${__dirname}/public/sitemap.xml`, | |
sitemap.toString() | |
) | |
} | |
module.exports.postBuild = function(pages, callback) { | |
generateSiteMap(pages) | |
callback() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment