mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-11-29 15:51:52 +00:00
app.logicsrc.com: sitemap.xml + robots Sitemap line (public pages only)
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 <noreply@anthropic.com>
This commit is contained in:
parent
b20afe6f50
commit
d3e07430b4
3 changed files with 63 additions and 1 deletions
31
apps/pwa/src/lib/seo.mjs
Normal file
31
apps/pwa/src/lib/seo.mjs
Normal file
|
|
@ -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, ">").replace(/"/g, """);
|
||||
|
||||
export function sitemapXml(origin) {
|
||||
const urls = PUBLIC_PATHS.map((p) => ` <url><loc>${xmlEscape(origin + p)}</loc></url>`).join("\n");
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls}\n</urlset>\n`;
|
||||
}
|
||||
|
||||
export function robotsTxt(origin) {
|
||||
return ["User-agent: *", "Allow: /", ...DISALLOW.map((p) => `Disallow: ${p}`), "", `Sitemap: ${origin}/sitemap.xml`, ""].join("\n");
|
||||
}
|
||||
|
|
@ -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/<token>.json|.keys): public, CORS-open, no session
|
||||
// (mounted before the session middleware so it never sets a cookie on a cacheable answer).
|
||||
|
|
|
|||
26
apps/pwa/test/seo.test.mjs
Normal file
26
apps/pwa/test/seo.test.mjs
Normal file
|
|
@ -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, /<urlset /);
|
||||
assert.match(xml, /<loc>https:\/\/app\.logicsrc\.com\/<\/loc>/);
|
||||
assert.match(xml, /<loc>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/);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue