diff --git a/.github/workflows/ui-deploy.yml b/.github/workflows/ui-deploy.yml index 0cc2ce5a30..d741c90d8f 100644 --- a/.github/workflows/ui-deploy.yml +++ b/.github/workflows/ui-deploy.yml @@ -77,6 +77,7 @@ jobs: echo "prerendered pages: $(find apps/loopover-ui/dist/client -name '*.html' | wc -l)" test -s apps/loopover-ui/dist/client/index.html test -s apps/loopover-ui/dist/client/404.html + echo "sitemap URLs: $(grep -c '' apps/loopover-ui/dist/client/sitemap.xml)" - name: Deploy loopover.ai if: ${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} diff --git a/apps/loopover-ui/public/sitemap.xml b/apps/loopover-ui/public/sitemap.xml deleted file mode 100644 index 3e368c5a3e..0000000000 --- a/apps/loopover-ui/public/sitemap.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - https://loopover.ai/ - https://loopover.ai/miners - https://loopover.ai/maintainers - https://loopover.ai/agents - https://loopover.ai/roadmap - https://loopover.ai/docs - https://loopover.ai/docs/beta-onboarding - https://loopover.ai/docs/quickstart - https://loopover.ai/docs/mcp-clients - https://loopover.ai/docs/miner-workflow - https://loopover.ai/docs/maintainer-workflow - https://loopover.ai/docs/github-app - https://loopover.ai/docs/maintainer-install-trust - https://loopover.ai/docs/maintainer-self-hosting - https://loopover.ai/docs/self-hosting-quickstart - https://loopover.ai/docs/self-hosting-configuration - https://loopover.ai/docs/self-hosting-github-app - https://loopover.ai/docs/self-hosting-ai-providers - https://loopover.ai/docs/self-hosting-rees - https://loopover.ai/docs/self-hosting-rees-analyzers - https://loopover.ai/docs/self-hosting-rag - https://loopover.ai/docs/self-hosting-operations - https://loopover.ai/docs/self-hosting-backup-scaling - https://loopover.ai/docs/self-hosting-releases - https://loopover.ai/docs/self-hosting-security - https://loopover.ai/docs/self-hosting-troubleshooting - https://loopover.ai/docs/branch-analysis - https://loopover.ai/docs/scoreability - https://loopover.ai/docs/upstream-drift - https://loopover.ai/docs/privacy-security - https://loopover.ai/docs/troubleshooting - https://loopover.ai/docs/ai-summaries - https://loopover.ai/api - https://loopover.ai/changelog - diff --git a/apps/loopover-ui/scripts/prerender-static.ts b/apps/loopover-ui/scripts/prerender-static.ts index 9e0d15433f..a54325763c 100644 --- a/apps/loopover-ui/scripts/prerender-static.ts +++ b/apps/loopover-ui/scripts/prerender-static.ts @@ -4,10 +4,13 @@ // Why a crawl: the app is a TanStack Start SSR build (Nitro, cloudflare-module preset). Nitro's own prerenderer // renders nothing under @lovable.dev/vite-tanstack-config, and TanStack Start's needs a Node server bundle the // Cloudflare preset does not produce. So this runs the BUILT Worker in workerd -- several `wrangler dev --local` -// instances, since each renders one request at a time -- and fetches every page from it: the sitemap, every -// docs page in content/docs, every operation in public/openapi.json, and every internal link those pages -// render. A page is written as .html (/ as index.html), which Workers' default html_handling serves at -// with no redirect, and an unknown path's render becomes 404.html for not_found_handling. +// instances, since each renders one request at a time -- and fetches every page from it: every static route in +// src/routes, every docs page in content/docs, every operation in public/openapi.json, and every internal link +// those pages render. A page is written as .html (/ as index.html), which Workers' default html_handling +// serves at with no redirect, and an unknown path's render becomes 404.html for not_found_handling. +// +// sitemap.xml is written from the same crawl (src/lib/sitemap.ts): every rendered page's canonical URL, so the +// sitemap lists exactly the pages the site serves and cannot drift from them. // // Any internal link that does not render, or a page that embeds the local origin, fails the run: a link to a // removed page cannot ship. @@ -34,6 +37,8 @@ import { import { tmpdir } from "node:os"; import { dirname, join, relative, resolve } from "node:path"; +import { canonicalUrlOf, sitemapXml } from "../src/lib/sitemap.ts"; + const APP_DIR = resolve(import.meta.dirname, ".."); const CLIENT_DIR = join(APP_DIR, "dist/client"); const SERVER_DIR = join(APP_DIR, "dist/server"); @@ -192,10 +197,6 @@ function seedPaths(): string[] { .filter(Boolean); paths.add(`/${segments.join("/")}`); } - const sitemap = readFileSync(join(APP_DIR, "public/sitemap.xml"), "utf8"); - for (const match of sitemap.matchAll(/\s*([^<\s]+)\s*<\/loc>/g)) { - paths.add(normalize(new URL(match[1]!).pathname) ?? "/"); - } for (const file of readdirSync(join(APP_DIR, "content/docs"))) { if (file.endsWith(".mdx")) paths.add(`/docs/${file.slice(0, -".mdx".length)}`); } @@ -256,6 +257,7 @@ async function main(): Promise { process.on("SIGTERM", () => process.exit(143)); const failures: string[] = []; + const sitemapUrls = new Set(); let written = 0; try { await Promise.all(servers.map(({ origin, child, log }) => waitReady(origin, child, log))); @@ -293,6 +295,12 @@ async function main(): Promise { failures.push(`unsafe path ${path}`); return; } + try { + sitemapUrls.add(canonicalUrlOf(path, html)); + } catch (error) { + failures.push(`canonical ${path}: ${(error as Error).message}`); + return; + } mkdirSync(dirname(join(CLIENT_DIR, file)), { recursive: true }); writeFileSync(join(CLIENT_DIR, file), html); written += 1; @@ -339,8 +347,10 @@ async function main(): Promise { writeFileSync(join(CLIENT_DIR, "404.html"), notFoundHtml); } + writeFileSync(join(CLIENT_DIR, "sitemap.xml"), sitemapXml(sitemapUrls)); console.log( - `prerender: ${written} pages written (${seeds.length} seeded, ${seen.size} discovered)`, + `prerender: ${written} pages written (${seeds.length} seeded, ${seen.size} discovered), ` + + `${sitemapUrls.size} canonical URLs in sitemap.xml`, ); } finally { stopAll(); @@ -352,8 +362,12 @@ async function main(): Promise { for (const line of failures.slice(0, 50)) console.error(` ${line}`); process.exit(1); } - if (!existsSync(join(CLIENT_DIR, "index.html")) || !existsSync(join(CLIENT_DIR, "404.html"))) { - console.error("prerender: index.html or 404.html was not written"); + if ( + !existsSync(join(CLIENT_DIR, "index.html")) || + !existsSync(join(CLIENT_DIR, "404.html")) || + !existsSync(join(CLIENT_DIR, "sitemap.xml")) + ) { + console.error("prerender: index.html, 404.html or sitemap.xml was not written"); process.exit(1); } writeFileSync(GENERATED_CONFIG, `${JSON.stringify(deploy, null, 2)}\n`); diff --git a/apps/loopover-ui/src/lib/sitemap.test.ts b/apps/loopover-ui/src/lib/sitemap.test.ts new file mode 100644 index 0000000000..6818ff6379 --- /dev/null +++ b/apps/loopover-ui/src/lib/sitemap.test.ts @@ -0,0 +1,68 @@ +import { describe, expect, it } from "vitest"; + +import { canonicalUrlOf, sitemapXml } from "./sitemap"; + +describe("canonicalUrlOf", () => { + it("takes the URL a page names as canonical, in either attribute order", () => { + expect( + canonicalUrlOf( + "/docs", + '', + ), + ).toBe("https://loopover.ai/docs"); + expect( + canonicalUrlOf( + "/api/get-v1-repos", + '', + ), + ).toBe("https://loopover.ai/api"); + }); + + it("falls back to the page's own loopover.ai URL when it names no canonical", () => { + expect(canonicalUrlOf("/roadmap", '')).toBe( + "https://loopover.ai/roadmap", + ); + expect(canonicalUrlOf("/", "")).toBe("https://loopover.ai/"); + }); + + it("refuses a canonical on another host, naming the page", () => { + expect(() => + canonicalUrlOf("/agents", ''), + ).toThrow(/\/agents names https:\/\/www\.loopover\.ai\/agents as canonical/); + }); +}); + +describe("sitemapXml", () => { + it("lists each URL once, sorted, in the sitemap protocol's shape", () => { + expect( + sitemapXml([ + "https://loopover.ai/docs", + "https://loopover.ai/", + "https://loopover.ai/api", + "https://loopover.ai/docs", + ]), + ).toBe( + [ + '', + '', + " https://loopover.ai/", + " https://loopover.ai/api", + " https://loopover.ai/docs", + "", + "", + ].join("\n"), + ); + }); + + it("escapes XML metacharacters in a URL", () => { + expect(sitemapXml(["https://loopover.ai/a?b=1&c='d'"])).toContain( + "https://loopover.ai/a?b=1&c='d'", + ); + }); + + it("refuses a URL on another host", () => { + expect(() => sitemapXml(["https://example.com/"])).toThrow( + /sitemap URL https:\/\/example\.com\/ is not on https:\/\/loopover\.ai/, + ); + }); +}); diff --git a/apps/loopover-ui/src/lib/sitemap.ts b/apps/loopover-ui/src/lib/sitemap.ts new file mode 100644 index 0000000000..d8886cfd70 --- /dev/null +++ b/apps/loopover-ui/src/lib/sitemap.ts @@ -0,0 +1,61 @@ +// loopover.ai's sitemap.xml, built from the pages the prerender actually rendered (scripts/prerender-static.ts) +// rather than kept by hand: a hand-kept list named 34 of the site's pages and drifted from the rest. Each rendered +// page contributes the URL it names as canonical, so a page that defers to another (every /api/ page +// names /api) is listed once, under the URL search engines are told to index. +// +// The `.ts` extension on the import lets the prerender load this file under plain Node's type stripping. +import { SITE_ORIGIN } from "./canonical.ts"; + +const LINK_TAG = /]*>/gi; + +function attribute(tag: string, name: string): string | undefined { + const match = tag.match(new RegExp(`\\s${name}="([^"]*)"`, "i")); + return match?.[1]?.replaceAll("&", "&"); +} + +/** Whether a URL is on loopover.ai: the sitemap lists only this site's pages. */ +function onSite(url: string): boolean { + return new URL(url).origin === SITE_ORIGIN; +} + +/** A page's canonical URL, or an error naming the page when that URL is on another host. */ +function canonicalOnSite(url: string, path: string): string { + if (!onSite(url)) { + throw new Error(`${path} names ${url} as canonical, which is not on ${SITE_ORIGIN}`); + } + return url; +} + +/** The URL a rendered page names as canonical, or its own loopover.ai URL when it names none. */ +export function canonicalUrlOf(path: string, html: string): string { + for (const tag of html.match(LINK_TAG) ?? []) { + if (attribute(tag, "rel")?.toLowerCase() !== "canonical") continue; + const href = attribute(tag, "href"); + if (href) return canonicalOnSite(href, path); + } + return canonicalOnSite(new URL(path, SITE_ORIGIN).toString(), path); +} + +function escapeXml(text: string): string { + return text + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">") + .replaceAll('"', """) + .replaceAll("'", "'"); +} + +/** sitemap.xml for the given loopover.ai URLs: each once, sorted, so a rebuild of the same site is byte-identical. */ +export function sitemapXml(urls: Iterable): string { + const unique = [...new Set(urls)].sort(); + const foreign = unique.find((url) => !onSite(url)); + if (foreign) throw new Error(`sitemap URL ${foreign} is not on ${SITE_ORIGIN}`); + const entries = unique.map((url) => ` ${escapeXml(url)}`); + return [ + '', + '', + ...entries, + "", + "", + ].join("\n"); +}