Skip to content

Instantly share code, notes, and snippets.

@iwarner
Created August 4, 2019 16:27
Show Gist options
  • Save iwarner/1b143118077a15e4799568edcfc67a1d to your computer and use it in GitHub Desktop.
Save iwarner/1b143118077a15e4799568edcfc67a1d to your computer and use it in GitHub Desktop.
NextJS Sitemap / Robots and anything else
/**
* 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