Skip to content

Instantly share code, notes, and snippets.

@siegerhansma
Last active May 27, 2023 11:28
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 siegerhansma/d3ced7eacd0c4b5480aa19e3c3bdf273 to your computer and use it in GitHub Desktop.
Save siegerhansma/d3ced7eacd0c4b5480aa19e3c3bdf273 to your computer and use it in GitHub Desktop.
A simple sitemap generator with Deno and puppeteer

First, make sure you have installed Deno and Puppeteer on your machine.

Create the following typescript file and save it. In my example, I have called it sitemap-generator.ts. Update the websiteUrl variable (in my case https://dartsscoreboard.com) to the site you want to generate the sitemap for. This value is also included in the sitemap.

import puppeteer from "https://deno.land/x/puppeteer@16.2.0/mod.ts";

async function generateSitemap(url: string): Promise<void> {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Navigate to the URL
  await page.goto(url, { waitUntil: "networkidle0" });

  // Extract all the internal links on the page
  const links = await page.$$eval("a", (elements) =>
    elements.map((element) => element.href)
  );

  // Filter out external links
  let internalLinks = links.filter((link) => link.startsWith(url));

  internalLinks.unshift(url);
  // Generate the sitemap XML
  const sitemapXml = `<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
      ${internalLinks.map((link) => `<url><loc>${link}</loc></url>`).join("\n")}
    </urlset>`;

  // Write the sitemap.xml file
  await Deno.writeTextFile("sitemap.xml", sitemapXml);

  console.log("Sitemap generated successfully!");

  await browser.close();
}

// Replace the URL with your desired website URL
const websiteUrl = "https://dartsscoreboard.com";

generateSitemap(websiteUrl).catch((error) =>
  console.error("Error generating sitemap:", error)
);

Next run the file using the following command: deno run --allow-write --allow-read --allow-env --allow-run --allow-net --unstable sitemap-generator.ts

You should now have a sitemap.xml file in the same folder as your TypeScript file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment