Created
August 4, 2019 16:27
-
-
Save iwarner/1b143118077a15e4799568edcfc67a1d to your computer and use it in GitHub Desktop.
NextJS Sitemap / Robots and anything else
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Post install script | |
*/ | |
// Import | |
import fs from 'fs-extra' | |
// UI | |
import { formatDateStandard } from 'industry-ui/components/utils/formatDate' | |
// Data | |
import { Canonical } from '../config' | |
import getPages from '../config/pages' | |
const directory = 'out' | |
// Robots | |
const robotsTxt = `User-agent: * | |
Sitemap: ${Canonical}/sitemap.xml | |
Disallow:` | |
fs.writeFileSync(`${directory}/robots.txt`, robotsTxt) | |
console.log('- Robots.txt saved!') | |
// Sitemap | |
const pages = getPages() | |
const today = formatDateStandard(new Date()) | |
const sitemapXml = | |
`<?xml version="1.0" encoding="UTF-8"?> | |
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | |
${Object.keys(pages).map((path) => { | |
return `<url> | |
<loc>${Canonical}${path}</loc> | |
<lastmod>${pages[path].lastModified ? formatDateStandard(new Date(pages[path].lastModified)) : today}</lastmod> | |
</url>\n ` | |
}).join('')} | |
</urlset>` | |
fs.writeFileSync(`${directory}/sitemap.xml`, sitemapXml) | |
console.log('- Sitemap.xml saved!\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment