Last active
November 17, 2017 14:17
-
-
Save ivanoats/8d01d9e934fdc17bae9090147f1e799b to your computer and use it in GitHub Desktop.
Sitemap for Gatsby
This file contains hidden or 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
import fs from 'fs-extra-promise' | |
import sm from 'sitemap' // npm install --save sitemap | |
function pagesToSitemap(pages) { | |
const urls = pages.map((p) => { | |
if (p.path !== undefined) { | |
return { | |
url: p.path, | |
changefreq: 'daily', | |
priority: 0.7 | |
} | |
} | |
}) | |
// remove undefined (template pages) | |
return urls.filter(u => u !== undefined) | |
} | |
function generateSiteMap(pages) { | |
const 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() | |
) | |
} | |
export function postBuild(pages, callback) { | |
generateSiteMap(pages) | |
callback() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment