File tree Expand file tree Collapse file tree 3 files changed +60
-1
lines changed Expand file tree Collapse file tree 3 files changed +60
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 11User-agent: *
2- Disallow: /404
2+ Disallow: /
3+ Sitemap: https://www.webdevpath.co/sitemap.xml
You can’t perform that action at this time.
0 commit comments