Skip to content

Instantly share code, notes, and snippets.

@spemer
Last active April 11, 2020 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spemer/935bb996d7d4539ebe7d23b178ddc6bc to your computer and use it in GitHub Desktop.
Save spemer/935bb996d7d4539ebe7d23b178ddc6bc to your computer and use it in GitHub Desktop.
sitemap.js
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