Last active
April 11, 2020 16:52
-
-
Save spemer/935bb996d7d4539ebe7d23b178ddc6bc to your computer and use it in GitHub Desktop.
sitemap.js
This file contains hidden or 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
const fs = require("fs"); | |
const globby = require("globby"); | |
const prettier = require("prettier"); | |
const getDate = new Date().toISOString(); | |
const webrootDomain = "https://website.com"; | |
const formatted = sitemap => prettier.format(sitemap, { parser: "html" }); | |
(async () => { | |
const pages = await globby(["../public/sitemap/*.gz"]); | |
const sitemapIndex = ` | |
${pages | |
.map(page => { | |
const path = page.replace("../public/", ""); | |
return ` | |
<sitemap> | |
<loc>${`${webrootDomain}/${path}`}</loc> | |
<lastmod>${getDate}</lastmod> | |
</sitemap>`; | |
}) | |
.join("")} | |
`; | |
const sitemap = ` | |
<?xml version="1.0" encoding="UTF-8"?> | |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | |
${sitemapIndex} | |
</sitemapindex> | |
`; | |
const formattedSitemap = [formatted(sitemap)]; | |
fs.writeFileSync("../public/sitemap.xml", formattedSitemap, "utf8"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment