Commit Graph

11 Commits

Author SHA1 Message Date
Leandro Facchinetti cb0bf74025 . 2020-12-12 02:37:21 +00:00
Leandro Facchinetti 640fbef200 Close #31 2020-12-07 22:56:00 +00:00
Leandro Facchinetti e452838d27 Don’t store alternates explicitly to save disk space
Instead, fetch alternates from within the feed on the demand.

This makes alternates marginally more expensive to retrieve, but saves on storage (which we were running out on the DigitalOcean deployment), and is a cleaner architecture overall: no need to keep the feeds and alternates in sync.

Here’s a script to migrate existing feeds:

// Call me with, for example: env "BASE_URL=https://kill-the-newsletter.com" npx ts-node migrate.ts
// I’m idempotent and reentrant, you may call me multiple times if necessary (for example, if the migration fails in the middle for whatever reason)

import { promises as fs } from "fs";
import path from "path";
import { JSDOM } from "jsdom";

const BASE_URL = process.env.BASE_URL ?? "http://localhost:8000";
const FEEDS_PATH = "static/feeds";

(async () => {
  await fs.rmdir("static/alternate", { recursive: true });
  for (const feedPath of (await fs.readdir(FEEDS_PATH)).filter((feedPath) =>
    feedPath.endsWith(".xml")
  )) {
    const text = await fs.readFile(path.join(FEEDS_PATH, feedPath), "utf-8");
    const feed = new JSDOM(text, { contentType: "text/xml" });
    const document = feed.window.document;
    const feedIdentifier = document
      .querySelector("id")!
      .textContent!.split(":")[2];
    for (const entry of document.querySelectorAll("entry")) {
      const entryIdentifier = entry
        .querySelector("id")!
        .textContent!.split(":")[2];
      entry
        .querySelector(`link[rel="alternate"]`)
        ?.setAttribute(
          "href",
          `${BASE_URL}/alternate/${feedIdentifier}/${entryIdentifier}.html`
        );
    }
    await fs.writeFile(
      path.join(FEEDS_PATH, feedPath),
      `<?xml version="1.0" encoding="utf-8"?>${feed.serialize()}`.trim()
    );
    console.log(feedIdentifier);
  }
})();
2020-11-24 17:12:14 +00:00
Leandro Facchinetti 2724333412 Add html tagged template literal 2020-08-10 00:30:12 +01:00
Leandro Facchinetti f2fee64925 Switch src and alt 2020-08-09 23:05:31 +01:00
Leandro Facchinetti c6540fc54c Add optional tags back 2020-08-09 22:53:32 +01:00
Leandro Facchinetti c51272f340 Remove www 2020-08-04 22:57:49 +01:00
Leandro Facchinetti 80ae8fab52 Fixes & refactorings 2020-07-23 16:11:41 +01:00
Johan Holmerin d56232e6b7 Remove old alternate entries 2020-07-15 21:28:42 +02:00
Johan Holmerin 62dee956d7 Add support for alternate URL 2020-07-14 19:42:41 +02:00
Leandro Facchinetti 9292d04a7e . 2020-06-09 09:26:41 +01:00