Skip to content

Instantly share code, notes, and snippets.

@chiedo
Forked from ivanoats/gatsby-node.js
Created September 15, 2016 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiedo/cd1e88ba3db440eb1f44fa6aaf8c3bd5 to your computer and use it in GitHub Desktop.
Save chiedo/cd1e88ba3db440eb1f44fa6aaf8c3bd5 to your computer and use it in GitHub Desktop.
Sitemap for Gatsby
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