diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ccd9c4f..7e8ccdca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -88,6 +88,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Search functionality for blog posts - Styled components to Title component - Links to blog tags to show all posts with the same tag +- Added XML Sitemap using getServerSideProps - Converting components into styled-components ### Fixed diff --git a/pages/sitemap.xml.js b/pages/sitemap.xml.js new file mode 100644 index 00000000..05c32887 --- /dev/null +++ b/pages/sitemap.xml.js @@ -0,0 +1,57 @@ +const EXTERNAL_DATA_URL = 'https://dev.to/api/articles?username=wdp'; + +function generateSiteMap(posts) { + return ` + + + https://www.webdevpath.co + + + https://www.webdevpath.co/about + + + https://www.webdevpath.co/blog + + + https://www.webdevpath.co/contact + + + https://www.webdevpath.co/contact + + ${posts + .map(({ tag_list }) => { + return tag_list.map(tag => { + return ` + + ${`https://www.webdevpath.co/blog/category/${tag}`} + `; + }); + }) + .join('')} + + `; +} + +function SiteMap() { + // getServerSideProps will do the heavy lifting +} + +export async function getServerSideProps({ res }) { + // We make an API call to gather the URLs for our site + const request = await fetch(EXTERNAL_DATA_URL); + const posts = await request.json(); + + // We generate the XML sitemap with the posts data + const sitemap = generateSiteMap(posts); + + res.setHeader('Content-Type', 'text/xml'); + // we send the XML to the browser + res.write(sitemap); + res.end(); + + return { + props: {}, + }; +} + +export default SiteMap; diff --git a/public/robots.txt b/public/robots.txt index e826bb20..15b1e50d 100644 --- a/public/robots.txt +++ b/public/robots.txt @@ -1,2 +1,3 @@ User-agent: * -Disallow: /404 \ No newline at end of file +Disallow: / +Sitemap: https://www.webdevpath.co/sitemap.xml \ No newline at end of file