Skip to content

Commit

Permalink
add sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
matmair committed Mar 20, 2024
1 parent 97794fe commit f7dff7b
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions web/pages/sitemap.xml.js
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;

0 comments on commit f7dff7b

Please sign in to comment.