From 39afc0f89ad502f41295a0e0da2a205a65eda8c9 Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 4 May 2023 21:11:40 -0400 Subject: [PATCH 1/2] added sitemap --- CHANGELOG.md | 1 + components/buttons/ButtonLink.js | 6 +++- pages/sitemap.xml.js | 62 ++++++++++++++++++++++++++++++++ public/robots.txt | 3 +- 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 pages/sitemap.xml.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ed69fb9..316b8d9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,6 +87,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 ### Fixed diff --git a/components/buttons/ButtonLink.js b/components/buttons/ButtonLink.js index 9a34c661..93d4ff3b 100644 --- a/components/buttons/ButtonLink.js +++ b/components/buttons/ButtonLink.js @@ -13,7 +13,11 @@ export default function ButtonLink({ return ( + + + https://www.webdevpath.co + 2023-05-04 + + + https://www.webdevpath.co/about + 2023-05-04 + + + https://www.webdevpath.co/blog + 2023-05-04 + + + https://www.webdevpath.co/contact + 2023-05-04 + + + https://www.webdevpath.co/contact + 2023-05-04 + + ${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 From 3f67ebbc8b951c35c8369783d855322839376ef7 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 8 May 2023 00:20:38 -0400 Subject: [PATCH 2/2] remove lastmod --- pages/sitemap.xml.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pages/sitemap.xml.js b/pages/sitemap.xml.js index 07c131c7..05c32887 100644 --- a/pages/sitemap.xml.js +++ b/pages/sitemap.xml.js @@ -5,23 +5,18 @@ function generateSiteMap(posts) { https://www.webdevpath.co - 2023-05-04 https://www.webdevpath.co/about - 2023-05-04 https://www.webdevpath.co/blog - 2023-05-04 https://www.webdevpath.co/contact - 2023-05-04 https://www.webdevpath.co/contact - 2023-05-04 ${posts .map(({ tag_list }) => {