From 37420d2526e4d956e70c75c92380c5d7f021239d Mon Sep 17 00:00:00 2001 From: William McGonagle Date: Wed, 16 Aug 2023 09:00:05 -0400 Subject: [PATCH] Added Sitemap --- data/rules.md | 0 pages/sitemap.xml.js | 83 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 data/rules.md create mode 100644 pages/sitemap.xml.js diff --git a/data/rules.md b/data/rules.md new file mode 100644 index 0000000..e69de29 diff --git a/pages/sitemap.xml.js b/pages/sitemap.xml.js new file mode 100644 index 0000000..543253e --- /dev/null +++ b/pages/sitemap.xml.js @@ -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 ` + + + ${domain}/ + ${prettifyDate(new Date())} + + + ${domain}/map + ${prettifyDate(new Date())} + + + ${domain}/charter + ${prettifyDate(new Date())} + + + ${domain}/join + 2023-08-14 + + + ${domain}/user/login + 2023-08-14 + + ${charters + .map(({ id }) => { + return ` + + ${`${domain}/charter/${id}`} + + `; + }) + .join('')} + ${charters + .map(({ id }) => { + return ` + + ${`${domain}/charter/${id}`} + + `; + }) + .join('')} + + `; +} + +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; \ No newline at end of file