Created
September 27, 2018 17:11
-
-
Save botic/2d30bcfef07c1ac3bb2234d682ef6679 to your computer and use it in GitHub Desktop.
Generator for a sitemap.xml in Node.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
| "use strict"; | |
| const DOMAIN = "example.com"; | |
| const URLS_PER_FILE = 40000; | |
| const fs = require("fs"); | |
| const readline = require("readline"); | |
| const rd = readline.createInterface({ | |
| input: fs.createReadStream(`${__dirname}/input/ids-and-dates.txt`) | |
| }); | |
| function writeSitemap(sitemapNumber, urls) { | |
| const writeStream = fs.createWriteStream(`${__dirname}/output/sitemap-${sitemapNumber}.xml`); | |
| writeStream.on("finish", () => { | |
| console.log(`Wrote sitemap-${sitemapNumber}.xml`); | |
| }); | |
| writeStream.write(`<?xml version="1.0" encoding="UTF-8"?>\n`); | |
| writeStream.write(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">\n`); | |
| for (const url of urls) { | |
| writeStream.write(url + "\n"); | |
| } | |
| writeStream.write(`</urlset> `); | |
| writeStream.end(); | |
| } | |
| let counter = 1; | |
| let urlBuffer = []; | |
| rd.on("line", function (line) { | |
| const [storyId, date] = line.split(","); | |
| urlBuffer.push(` | |
| <url> | |
| <loc>https://${DOMAIN}/stories/${storyId}/</loc> | |
| <lastmod>${date}</lastmod> | |
| <xhtml:link rel="alternate" media="only screen and (max-width: 640px)" href="https://${DOMAIN}/m/stories/${storyId}/" /> | |
| </url> | |
| `); | |
| if (counter % URLS_PER_FILE === 0) { | |
| writeSitemap((counter / URLS_PER_FILE), urlBuffer); | |
| urlBuffer = []; | |
| } | |
| counter ++; | |
| }); | |
| rd.on("close", () => { | |
| if (urlBuffer.length > 0) { | |
| writeSitemap(Math.ceil(counter / URLS_PER_FILE), urlBuffer); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment