Skip to content

Commit 8a48d6b

Browse files
authored
Merge pull request #168 from Web-Dev-Path/chore/add-sitemap
Added sitemap
2 parents c1fda52 + 3a2c9e9 commit 8a48d6b

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
8888
- Search functionality for blog posts
8989
- Styled components to Title component
9090
- Links to blog tags to show all posts with the same tag
91+
- Added XML Sitemap using getServerSideProps
9192
- Converting components into styled-components
9293

9394
### Fixed

pages/sitemap.xml.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const EXTERNAL_DATA_URL = 'https://dev.to/api/articles?username=wdp';
2+
3+
function generateSiteMap(posts) {
4+
return `<?xml version="1.0" encoding="UTF-8"?>
5+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
6+
<url>
7+
<loc>https://www.webdevpath.co</loc>
8+
</url>
9+
<url>
10+
<loc>https://www.webdevpath.co/about</loc>
11+
</url>
12+
<url>
13+
<loc>https://www.webdevpath.co/blog</loc>
14+
</url>
15+
<url>
16+
<loc>https://www.webdevpath.co/contact</loc>
17+
</url>
18+
<url>
19+
<loc>https://www.webdevpath.co/contact</loc>
20+
</url>
21+
${posts
22+
.map(({ tag_list }) => {
23+
return tag_list.map(tag => {
24+
return `
25+
<url>
26+
<loc>${`https://www.webdevpath.co/blog/category/${tag}`}</loc>
27+
</url> `;
28+
});
29+
})
30+
.join('')}
31+
</urlset>
32+
`;
33+
}
34+
35+
function SiteMap() {
36+
// getServerSideProps will do the heavy lifting
37+
}
38+
39+
export async function getServerSideProps({ res }) {
40+
// We make an API call to gather the URLs for our site
41+
const request = await fetch(EXTERNAL_DATA_URL);
42+
const posts = await request.json();
43+
44+
// We generate the XML sitemap with the posts data
45+
const sitemap = generateSiteMap(posts);
46+
47+
res.setHeader('Content-Type', 'text/xml');
48+
// we send the XML to the browser
49+
res.write(sitemap);
50+
res.end();
51+
52+
return {
53+
props: {},
54+
};
55+
}
56+
57+
export default SiteMap;

public/robots.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
User-agent: *
2-
Disallow: /404
2+
Disallow: /
3+
Sitemap: https://www.webdevpath.co/sitemap.xml

0 commit comments

Comments
 (0)