Skip to content

Commit

Permalink
Merge pull request #168 from Web-Dev-Path/chore/add-sitemap
Browse files Browse the repository at this point in the history
Added sitemap
  • Loading branch information
vmcodes authored May 29, 2023
2 parents c1fda52 + 3a2c9e9 commit 8a48d6b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions pages/sitemap.xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const EXTERNAL_DATA_URL = 'https://dev.to/api/articles?username=wdp';

function generateSiteMap(posts) {
return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.webdevpath.co</loc>
</url>
<url>
<loc>https://www.webdevpath.co/about</loc>
</url>
<url>
<loc>https://www.webdevpath.co/blog</loc>
</url>
<url>
<loc>https://www.webdevpath.co/contact</loc>
</url>
<url>
<loc>https://www.webdevpath.co/contact</loc>
</url>
${posts
.map(({ tag_list }) => {
return tag_list.map(tag => {
return `
<url>
<loc>${`https://www.webdevpath.co/blog/category/${tag}`}</loc>
</url> `;
});
})
.join('')}
</urlset>
`;
}

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;
3 changes: 2 additions & 1 deletion public/robots.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
User-agent: *
Disallow: /404
Disallow: /
Sitemap: https://www.webdevpath.co/sitemap.xml

0 comments on commit 8a48d6b

Please sign in to comment.