-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const EXTERNAL_DATA_URL = 'https://mjmair.com/posts'; | ||
|
||
function generateSiteMap(posts) { | ||
return `<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
${posts | ||
.map(({ id }) => { | ||
return ` | ||
<url> | ||
<loc>${`${EXTERNAL_DATA_URL}/${id}`}</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; |