feat: proxy standalone domain landing pages for rspace.online/{moduleId}

Instead of the generated landing page, rspace.online/rnotes now fetches
and serves the real page from rnotes.online (with <base> tag for asset
resolution). 5-minute in-memory cache avoids repeated fetches. Falls
back to the generated landing page for modules without a standalone
domain or when the fetch fails.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jeff Emmett 2026-02-25 23:14:07 -08:00
parent 2cfe2c744d
commit 138716660b
1 changed files with 36 additions and 2 deletions

View File

@ -98,6 +98,30 @@ const PORT = Number(process.env.PORT) || 3000;
const INTERNAL_API_KEY = process.env.INTERNAL_API_KEY || "";
const DIST_DIR = resolve(import.meta.dir, "../dist");
// ── Standalone landing page proxy cache ──
const landingCache = new Map<string, { html: string; at: number }>();
const LANDING_TTL = 5 * 60 * 1000; // 5 minutes
async function fetchStandaloneLanding(domain: string): Promise<string | null> {
const cached = landingCache.get(domain);
if (cached && Date.now() - cached.at < LANDING_TTL) return cached.html;
try {
const resp = await fetch(`https://${domain}/`, {
headers: { "Accept": "text/html" },
redirect: "follow",
});
if (!resp.ok) return null;
let html = await resp.text();
// Inject <base> so relative assets (CSS, JS, images) resolve to the standalone domain
html = html.replace(/<head([^>]*)>/i, `<head$1>\n<base href="https://${domain}/">`);
landingCache.set(domain, { html, at: Date.now() });
return html;
} catch {
return null;
}
}
// ── Hono app ──
const app = new Hono();
@ -780,7 +804,7 @@ const server = Bun.serve<WSData>({
}
// ── Bare-domain module routes: rspace.online/{moduleId} ──
// Exact module path → serve module landing page.
// Exact module path → proxy the standalone domain's landing page.
// Sub-paths (API, assets) → rewrite to /demo/{moduleId}/... for backward compat.
if (!subdomain && hostClean.includes("rspace.online")) {
const pathSegments = url.pathname.split("/").filter(Boolean);
@ -788,8 +812,18 @@ const server = Bun.serve<WSData>({
const firstSegment = pathSegments[0];
const knownModuleIds = new Set(getAllModules().map((m) => m.id));
if (knownModuleIds.has(firstSegment)) {
// Exact module path → landing page
// Exact module path → proxy from standalone domain
if (pathSegments.length === 1) {
const mod = getAllModules().find((m) => m.id === firstSegment);
if (mod?.standaloneDomain) {
const html = await fetchStandaloneLanding(mod.standaloneDomain);
if (html) {
return new Response(html, {
headers: { "Content-Type": "text/html; charset=utf-8" },
});
}
}
// Fallback: generated landing page (no standalone domain or fetch failed)
const modInfo = getModuleInfoList().find((m) => m.id === firstSegment);
if (modInfo) {
return new Response(