diff --git a/server/index.ts b/server/index.ts index 7054ddf..eac52ac 100644 --- a/server/index.ts +++ b/server/index.ts @@ -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(); +const LANDING_TTL = 5 * 60 * 1000; // 5 minutes + +async function fetchStandaloneLanding(domain: string): Promise { + 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 so relative assets (CSS, JS, images) resolve to the standalone domain + html = html.replace(/]*)>/i, `\n`); + 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({ } // ── 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({ 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(