From d5002540a51043ba9e02bacf89c13e0e496b9f35 Mon Sep 17 00:00:00 2001 From: Anthony Ettinger Date: Sun, 22 Nov 2026 08:03:57 -0800 Subject: [PATCH] app.logicsrc.com: sitemap.xml + robots Sitemap line (public pages only) (#294) The credentials app answered 404 for both. robots.txt and sitemap.xml now come from the request host, list only the sign-in and sign-up pages, and disallow the session-only areas. Co-authored-by: Claude Opus 5.5 --- apps/pwa/src/lib/seo.mjs | 31 +++++++++++++++++++++++++++++++ apps/pwa/src/server.mjs | 7 ++++++- apps/pwa/test/seo.test.mjs | 26 ++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 apps/pwa/src/lib/seo.mjs create mode 100644 apps/pwa/test/seo.test.mjs diff --git a/apps/pwa/src/lib/seo.mjs b/apps/pwa/src/lib/seo.mjs new file mode 100644 index 00000000..3ab0b3fb --- /dev/null +++ b/apps/pwa/src/lib/seo.mjs @@ -0,0 +1,31 @@ +// robots.txt and sitemap.xml for the credentials app. +// +// Almost everything here sits behind a session, so the sitemap lists only the +// pages an anonymous visitor (or a crawler) can open: the sign-in page and the +// sign-up form. The origin comes from the request, so whichever hostname the app +// answers on describes itself rather than another one. + +import { requestOrigin } from "./origin.mjs"; + +// Public pages, by path. Everything else redirects to sign-in. +export const PUBLIC_PATHS = ["/", "/?mode=up"]; + +// Session-only areas a crawler has no reason to visit. +const DISALLOW = ["/api/", "/auth/", "/cli/", "/dashboard", "/keyring", "/settings", "/teams/", "/k/"]; + +/** https origin for this request (http only for localhost development). */ +export function siteOrigin(req, fallback) { + const origin = requestOrigin(req, fallback); + return /^http:\/\/(localhost|127\.0\.0\.1)(:|$)/.test(origin) ? origin : origin.replace(/^http:/, "https:"); +} + +const xmlEscape = (s) => String(s).replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); + +export function sitemapXml(origin) { + const urls = PUBLIC_PATHS.map((p) => ` ${xmlEscape(origin + p)}`).join("\n"); + return `\n\n${urls}\n\n`; +} + +export function robotsTxt(origin) { + return ["User-agent: *", "Allow: /", ...DISALLOW.map((p) => `Disallow: ${p}`), "", `Sitemap: ${origin}/sitemap.xml`, ""].join("\n"); +} diff --git a/apps/pwa/src/server.mjs b/apps/pwa/src/server.mjs index bd3663ea..79d3f78e 100644 --- a/apps/pwa/src/server.mjs +++ b/apps/pwa/src/server.mjs @@ -24,6 +24,7 @@ import { profileRouter } from "./lib/profile.mjs"; import { descriptor } from "@profullstack/footer"; import { RING_FOOTER_OPTIONS } from "./lib/ring-footer.mjs"; import { securityHeaders } from "./lib/security-headers.mjs"; +import { robotsTxt, siteOrigin, sitemapXml } from "./lib/seo.mjs"; import { PROFILE_ENHANCE_SCRIPT } from "@profullstack/profile"; const app = express(); @@ -73,7 +74,11 @@ app.get("/vendor/profile-enhance.js", (_req, res) => res.type("application/javas app.get("/.well-known/openwebring.json", (_req, res) => res.json(descriptor({ site: RING_FOOTER_OPTIONS.site, name: "LogicSRC credentials" }))); -app.get("/healthz", (_req, res) => res.json({ ok: true, env: config.env })); +// Crawlers: only the public sign-in and sign-up pages, on whichever host was asked. +app.get("/robots.txt", (req, res) => res.type("text/plain").set("Cache-Control", "public, max-age=3600").send(robotsTxt(siteOrigin(req, config.origin)))); +app.get("/sitemap.xml", (req, res) => res.type("application/xml").set("Cache-Control", "public, max-age=3600").send(sitemapXml(siteOrigin(req, config.origin)))); + +app.get("/healthz",(_req, res) => res.json({ ok: true, env: config.env })); // The keyring share link (/k/.json|.keys): public, CORS-open, no session // (mounted before the session middleware so it never sets a cookie on a cacheable answer). diff --git a/apps/pwa/test/seo.test.mjs b/apps/pwa/test/seo.test.mjs new file mode 100644 index 00000000..daae6e76 --- /dev/null +++ b/apps/pwa/test/seo.test.mjs @@ -0,0 +1,26 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { robotsTxt, siteOrigin, sitemapXml } from "../src/lib/seo.mjs"; + +const req = (host, protocol = "http") => ({ protocol, headers: { host }, get: (h) => (h.toLowerCase() === "host" ? host : undefined) }); + +test("each hostname describes itself over https", () => { + assert.equal(siteOrigin(req("app.logicsrc.com"), "https://x"), "https://app.logicsrc.com"); + assert.equal(siteOrigin(req("localhost:8080"), "https://x"), "http://localhost:8080"); +}); + +test("sitemap lists only public pages on this host", () => { + const xml = sitemapXml("https://app.logicsrc.com"); + assert.match(xml, /https:\/\/app\.logicsrc\.com\/<\/loc>/); + assert.match(xml, /https:\/\/app\.logicsrc\.com\/\?mode=up<\/loc>/); + assert.doesNotMatch(xml, /dashboard|settings|keyring/); +}); + +test("robots points at this host's sitemap and keeps it crawlable", () => { + const txt = robotsTxt("https://app.logicsrc.com"); + assert.match(txt, /^Sitemap: https:\/\/app\.logicsrc\.com\/sitemap\.xml$/m); + assert.doesNotMatch(txt, /Disallow: \/sitemap/); + assert.match(txt, /Disallow: \/dashboard/); +});