Skip to content

Commit

Permalink
Added Sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
William-McGonagle committed Aug 16, 2023
1 parent d5c12dc commit 37420d2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Empty file added data/rules.md
Empty file.
83 changes: 83 additions & 0 deletions pages/sitemap.xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Charter, User } from "../server/models"

function prettifyDate(date) {

return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`

}

function generateSiteMap({ charters, users }) {

const domain = "https://c.fairfieldprogramming.org"

return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>${domain}/</loc>
<lastmod>${prettifyDate(new Date())}</lastmod>
</url>
<url>
<loc>${domain}/map</loc>
<lastmod>${prettifyDate(new Date())}</lastmod>
</url>
<url>
<loc>${domain}/charter</loc>
<lastmod>${prettifyDate(new Date())}</lastmod>
</url>
<url>
<loc>${domain}/join</loc>
<lastmod>2023-08-14</lastmod>
</url>
<url>
<loc>${domain}/user/login</loc>
<lastmod>2023-08-14</lastmod>
</url>
${charters
.map(({ id }) => {
return `
<url>
<loc>${`${domain}/charter/${id}`}</loc>
</url>
`;
})
.join('')}
${charters
.map(({ id }) => {
return `
<url>
<loc>${`${domain}/charter/${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 charters = await Charter.findAll({ });
const users = await User.findAll({ });

// We generate the XML sitemap with the posts data
const sitemap = generateSiteMap({ charters, users });

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 37420d2

Please sign in to comment.