Skip to content

Commit

Permalink
feat: handle sitemap (#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
moshfeu authored Apr 25, 2022
1 parent aa0ae52 commit 5acd55d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
19 changes: 19 additions & 0 deletions pages/sitemap.xml/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { GetServerSideProps } from 'next/types';
import { buildSitemap } from '../../src/utils/sitemapGenerator';

export default function Index() {
return null;
}

export const getServerSideProps: GetServerSideProps = async ({ res }) => {
res.setHeader('Content-Type', 'text/xml');
const xml = await buildSitemap();
res.write(xml);

res.end();

// Empty since we don't render anything
return {
props: {},
};
};
56 changes: 27 additions & 29 deletions scripts/pre-build.mjs → src/utils/sitemapGenerator.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import fs from 'fs';
import fetch from 'node-fetch';
const getPath = (key?: string, value?: string) => {
if (!key) {
return '';
}
if (key === 'id') {
return `u/${value}`;
}
return `?${key}=${encodeURIComponent(value)}`;
};

(async () => {
const createUrl = (key?: string, value?: string) => {
return `<url><loc>https://mentors.codingcoach.io/${getPath(
key,
value
)}</loc></url>`;
};

export const buildSitemap = async () => {
const mentors = await fetch(`https://api.codingcoach.io/mentors?limit=1400`)
.then((data) => data.json())
.then((res) => res.data);
Expand All @@ -25,30 +39,14 @@ import fetch from 'node-fetch';
json.language = [...new Set(json.language)];

const lineBreak = '\n\t';
const URLs = Object.keys(json)
.map((key) => [...json[key]].map((value) => createUrl(key, value)))
.flat();
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${createUrl()}
${Object.keys(json)
.map((key) =>
[...json[key]].map((value) => createUrl(key, value)).join(lineBreak)
)
.join(lineBreak)}
</urlset>
`;

fs.writeFileSync('public/sitemap.xml', xml);
})();

const getPath = (key, value) => {
if (!key) {
return '';
}
if (key === 'id') {
return `u/${value}`;
}
return `?${key}=${encodeURIComponent(value)}`;
}

const createUrl = (key, value) => {
return `<url><loc>https://mentors.codingcoach.io/${getPath(key, value)}</loc></url>`;
}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${createUrl()}
${URLs.join(lineBreak)}
</urlset>
`;
return xml;
};
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"incremental": true
"incremental": true,
"downlevelIteration": true,
},
"include": [
"src"
Expand Down

1 comment on commit 5acd55d

@vercel
Copy link

@vercel vercel bot commented on 5acd55d Apr 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.