Skip to content

Commit 39afc0f

Browse files
committed
added sitemap
1 parent e5fd0ab commit 39afc0f

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

CHANGELOG.md

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

9192
### Fixed
9293

components/buttons/ButtonLink.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ export default function ButtonLink({
1313
return (
1414
<a
1515
href={link}
16-
className={customClassName?`${btnStyles.btn} ${btnStyles[customClassName]}`:btnStyles.btn}
16+
className={
17+
customClassName
18+
? `${btnStyles.btn} ${btnStyles[customClassName]}`
19+
: btnStyles.btn
20+
}
1721
style={styles}
1822
target={openNewTab ? '_blank' : undefined}
1923
rel='noopener noreferrer'

pages/sitemap.xml.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
<lastmod>2023-05-04</lastmod>
9+
</url>
10+
<url>
11+
<loc>https://www.webdevpath.co/about</loc>
12+
<lastmod>2023-05-04</lastmod>
13+
</url>
14+
<url>
15+
<loc>https://www.webdevpath.co/blog</loc>
16+
<lastmod>2023-05-04</lastmod>
17+
</url>
18+
<url>
19+
<loc>https://www.webdevpath.co/contact</loc>
20+
<lastmod>2023-05-04</lastmod>
21+
</url>
22+
<url>
23+
<loc>https://www.webdevpath.co/contact</loc>
24+
<lastmod>2023-05-04</lastmod>
25+
</url>
26+
${posts
27+
.map(({ tag_list }) => {
28+
return tag_list.map(tag => {
29+
return `
30+
<url>
31+
<loc>${`https://www.webdevpath.co/blog/category/${tag}`}</loc>
32+
</url> `;
33+
});
34+
})
35+
.join('')}
36+
</urlset>
37+
`;
38+
}
39+
40+
function SiteMap() {
41+
// getServerSideProps will do the heavy lifting
42+
}
43+
44+
export async function getServerSideProps({ res }) {
45+
// We make an API call to gather the URLs for our site
46+
const request = await fetch(EXTERNAL_DATA_URL);
47+
const posts = await request.json();
48+
49+
// We generate the XML sitemap with the posts data
50+
const sitemap = generateSiteMap(posts);
51+
52+
res.setHeader('Content-Type', 'text/xml');
53+
// we send the XML to the browser
54+
res.write(sitemap);
55+
res.end();
56+
57+
return {
58+
props: {},
59+
};
60+
}
61+
62+
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)