diff --git a/pages/sitemap.xml.tsx b/pages/sitemap.xml.tsx new file mode 100644 index 0000000..a77e159 --- /dev/null +++ b/pages/sitemap.xml.tsx @@ -0,0 +1,36 @@ +import React from 'react' + +import { SiteMap } from 'lib/types' +import { host } from 'lib/config' +import { getSiteMaps } from 'lib/get-site-maps' + +const createSitemap = ( + siteMap: SiteMap +) => ` + + + ${host} + ${host}/ + + + ${Object.keys(siteMap.canonicalPageMap) + .map((canonicalPagePath) => + ` + + ${host}/${canonicalPagePath} + + `.trim() + ) + .join('')} + + ` + +export default class Sitemap extends React.Component { + static async getInitialProps({ res }) { + const siteMaps = await getSiteMaps() + + res.setHeader('Content-Type', 'text/xml') + res.write(createSitemap(siteMaps[0])) + res.end() + } +}