-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.xml.tsx
58 lines (46 loc) · 1.37 KB
/
sitemap.xml.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import type { NextPage, GetServerSideProps } from 'next';
import sitemap from '@/data/sitemap.json';
import { DEFAULT_SITE_DOMAIN } from '@/helpers/constants';
import getSpecies from '@/helpers/getSpecies';
import getNews from '@/helpers/getNews';
const Page: NextPage = () => (
<>Sitemap</>
);
const generateSiteMap = (
additionalPages: Array<string>,
) => `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${Object.values(sitemap).filter(({ path }) => path.startsWith('/')).map(({ path }) => `
<url>
<loc>${DEFAULT_SITE_DOMAIN}${path}</loc>
</url>
`).join('\n')}
${additionalPages.map((path) => `
<url>
<loc>${DEFAULT_SITE_DOMAIN}${path}</loc>
</url>
`).join('\n')}
</urlset>
`.trim();
export const getServerSideProps: GetServerSideProps = async ({ res }) => {
const [species, news] = await Promise.all([
getSpecies(),
getNews({ limit: 1000 }),
]);
const additionalPages = [];
if (species) {
additionalPages.push(...species.map(({ slug }) => `/education/species/${slug}`));
}
if (news) {
additionalPages.push(...news.map(({ slug }) => `/news/${slug}`));
}
const content = generateSiteMap(additionalPages);
res.setHeader('Content-Type', 'text/xml');
res.write(content);
res.end();
return {
props: {},
};
};
export default Page;