Skip to content

Commit

Permalink
feat: add sitemap for every combo, closes #587
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeluigi committed Jan 7, 2025
1 parent a565586 commit 8a3594e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
15 changes: 0 additions & 15 deletions src/pages/sitemap.xml.ts → src/pages/card-sitemap.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,6 @@ async function generateSiteMap(): Promise<string> {
<loc>${`${process.env.NEXT_PUBLIC_CLIENT_URL}/`}</loc>
<priority>1.0</priority>
</url>
<url>
<loc>${`${process.env.NEXT_PUBLIC_CLIENT_URL}/advanced-search/`}</loc>
</url>
<url>
<loc>${`${process.env.NEXT_PUBLIC_CLIENT_URL}/syntax-guide/`}</loc>
</url>
<url>
<loc>${`${process.env.NEXT_PUBLIC_CLIENT_URL}/find-my-combos/`}</loc>
<priority>0.9</priority>
</url>
<url>
<loc>${`${process.env.NEXT_PUBLIC_CLIENT_URL}/search/?q=is:featured`}</loc>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
${cards
.map(({ name }) => {
return `
Expand Down
74 changes: 74 additions & 0 deletions src/pages/combo-sitemap.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Variant } from '@spacecowmedia/spellbook-client';
import { NextPageContext } from 'next';

type SitemapCache = {
sitemap?: string;
lastUpdated?: number;
};

const cardsCache: SitemapCache = {};

async function getCombos(): Promise<Variant[]> {
const bulkVariants = JSON.parse(await (await fetch('https://json.commanderspellbook.com/variants.json')).text());
const variants: Variant[] = bulkVariants.variants;
return variants;
}

async function generateSiteMap(): Promise<string> {
if (cardsCache.sitemap && cardsCache.lastUpdated && Date.now() - cardsCache.lastUpdated < 1000 * 60 * 60 * 24 * 6) {
return cardsCache.sitemap;
}
const combos = await getCombos();
const updateTime = Date.now();
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>${`${process.env.NEXT_PUBLIC_CLIENT_URL}/`}</loc>
<priority>1.0</priority>
</url>
<url>
<loc>${`${process.env.NEXT_PUBLIC_CLIENT_URL}/find-my-combos/`}</loc>
<priority>0.9</priority>
</url>
<url>
<loc>${`${process.env.NEXT_PUBLIC_CLIENT_URL}/search/?q=is:featured`}</loc>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
${combos
.map(({ id }) => {
return `
<url>
<loc>${`${process.env.NEXT_PUBLIC_CLIENT_URL}/combo/${encodeURIComponent(id)}/`}</loc>
<changefreq>weekly</changefreq>
<lastmod>${new Date(updateTime).toISOString()}</lastmod>
</url>
`;
})
.join('')}
</urlset>
`;
cardsCache.sitemap = sitemap;
cardsCache.lastUpdated = updateTime;
return sitemap;
}

function SiteMap() {
// getServerSideProps will do the heavy lifting
}

export async function getServerSideProps({ res }: NextPageContext) {
// We generate the XML sitemap with the posts data
const sitemap = await generateSiteMap();

res?.setHeader('Content-Type', 'application/xml');
// we send the XML to the browser
res?.write(sitemap);
res?.end();

return {
props: {},
};
}

export default SiteMap;
7 changes: 6 additions & 1 deletion src/pages/robots.txt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ function RobotsTxt() {
}

function getRobotsTxt(): string {
return `User-agent: *\nAllow: /\nSitemap: ${process.env.NEXT_PUBLIC_CLIENT_URL}/sitemap.xml\n`;
return [
'User-agent: *',
'Allow: /',
`Sitemap: ${process.env.NEXT_PUBLIC_CLIENT_URL}/combo-sitemap.xml`,
`Sitemap: ${process.env.NEXT_PUBLIC_CLIENT_URL}/card-sitemap.xml`,
].join('\n');
}

export async function getServerSideProps({ res }: NextPageContext) {
Expand Down

0 comments on commit 8a3594e

Please sign in to comment.