sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / docs / archive / drakestapleton.com-old / scripts / spa-fallback.mjs
| 1 | import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 2 | import { dirname, join } from "node:path"; |
| 3 | |
| 4 | const mode = process.argv[2] || "portfolio"; |
| 5 | if (!new Set(["waitlist", "portfolio"]).has(mode)) { |
| 6 | throw new Error(`Unknown site mode: ${mode}`); |
| 7 | } |
| 8 | |
| 9 | const dist = "dist"; |
| 10 | const indexPath = join(dist, "index.html"); |
| 11 | const indexHtml = readFileSync(indexPath, "utf8"); |
| 12 | const pageMetadata = JSON.parse(readFileSync(new URL("../site-metadata.json", import.meta.url), "utf8")); |
| 13 | const routes = [ |
| 14 | { rel: "atlas/index.html", path: "/atlas", ...pageMetadata["/atlas"] }, |
| 15 | { rel: "aegis/index.html", path: "/aegis", ...pageMetadata["/aegis"] }, |
| 16 | { rel: "aien/index.html", path: "/aien", ...pageMetadata["/aien"] }, |
| 17 | { rel: "research/index.html", path: "/research", ...pageMetadata["/research"] }, |
| 18 | { rel: "scholar/index.html", path: "/scholar", ...pageMetadata["/scholar"] }, |
| 19 | { rel: "scholar/omega/index.html", path: "/scholar/omega", ...pageMetadata["/scholar/omega"] }, |
| 20 | { rel: "what-i-learned/index.html", path: "/what-i-learned", ...pageMetadata["/what-i-learned"] }, |
| 21 | { rel: "what-broke/index.html", path: "/what-i-learned", ...pageMetadata["/what-i-learned"], redirect: true }, |
| 22 | { rel: "path/index.html", path: "/path", ...pageMetadata["/path"] }, |
| 23 | { rel: "software/index.html", path: "/software", ...pageMetadata["/software"] }, |
| 24 | { rel: "evidence/index.html", path: "/evidence", ...pageMetadata["/evidence"] }, |
| 25 | { rel: "interest/index.html", path: "/interest", ...pageMetadata["/interest"] }, |
| 26 | { rel: "dad/index.html", path: "/dad", ...pageMetadata["/dad"] }, |
| 27 | { rel: "symphony/index.html", path: "/symphony", ...pageMetadata["/symphony"] }, |
| 28 | { rel: "symphony/first/index.html", path: "/symphony/first", ...pageMetadata["/symphony/first"] }, |
| 29 | { rel: "symphony/workflow/index.html", path: "/symphony/workflow", ...pageMetadata["/symphony/workflow"] }, |
| 30 | { rel: "symphony/map/index.html", path: "/symphony/map", ...pageMetadata["/symphony/map"] }, |
| 31 | { rel: "atlas-symphony/index.html", path: "/symphony", ...pageMetadata["/symphony"], redirect: true }, |
| 32 | { rel: "works/index.html", path: "/works", ...pageMetadata["/works"] }, |
| 33 | { rel: "works/atlas-symphony/index.html", path: "/works/atlas-symphony", ...pageMetadata["/works/atlas-symphony"] }, |
| 34 | { rel: "works/atlas-harness/index.html", path: "/works/atlas-harness", ...pageMetadata["/works/atlas-harness"] }, |
| 35 | { rel: "works/mobdev-panel-arena/index.html", path: "/works/mobdev-panel-arena", ...pageMetadata["/works/mobdev-panel-arena"] }, |
| 36 | { rel: "works/defiantmob/index.html", path: "/works/defiantmob", ...pageMetadata["/works/defiantmob"] }, |
| 37 | { rel: "works/beltbrain/index.html", path: "/works/beltbrain", ...pageMetadata["/works/beltbrain"] }, |
| 38 | { rel: "works/dhg-hive/index.html", path: "/works/dhg-hive", ...pageMetadata["/works/dhg-hive"] }, |
| 39 | { rel: "works/foundations-2025/index.html", path: "/works/foundations-2025", ...pageMetadata["/works/foundations-2025"] }, |
| 40 | { rel: "works/aien/index.html", path: "/works/aien", ...pageMetadata["/works/aien"] }, |
| 41 | { rel: "works/aegis/index.html", path: "/works/aegis", ...pageMetadata["/works/aegis"] }, |
| 42 | { rel: "works/research/index.html", path: "/works/research", ...pageMetadata["/works/research"] }, |
| 43 | ]; |
| 44 | |
| 45 | function escapeAttribute(value) { |
| 46 | return value |
| 47 | .replaceAll("&", "&") |
| 48 | .replaceAll('"', """) |
| 49 | .replaceAll("<", "<") |
| 50 | .replaceAll(">", ">"); |
| 51 | } |
| 52 | |
| 53 | function escapeText(value) { |
| 54 | return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">"); |
| 55 | } |
| 56 | |
| 57 | function noscriptSummary(page) { |
| 58 | const links = [ |
| 59 | ["/", "Home"], |
| 60 | ["/path", "Path"], |
| 61 | ["/software", "Software"], |
| 62 | ["/works", "Works"], |
| 63 | ["/evidence", "Evidence"], |
| 64 | ["/atlas", "Atlas"], |
| 65 | ["/interest", "Conversation"], |
| 66 | ["/aegis", "AEGIS"], |
| 67 | ["/aien", "AIEN"], |
| 68 | ["/research", "Research"], |
| 69 | ["/symphony", "Symphony"], |
| 70 | ]; |
| 71 | const nav = links.map(([href, label]) => `<a href="${href}">${label}</a>`).join(" "); |
| 72 | return `<noscript><main><h1>${escapeText(page.title)}</h1><p>${escapeText(page.description)}</p><nav>${nav}</nav></main></noscript>`; |
| 73 | } |
| 74 | |
| 75 | function structuredData(page) { |
| 76 | const canonical = `https://www.drakestapleton.com${page.path}`; |
| 77 | const person = { |
| 78 | "@type": "Person", |
| 79 | "@id": "https://www.drakestapleton.com/#person", |
| 80 | name: "Drake Stapleton", |
| 81 | jobTitle: "AI Architect & Operator", |
| 82 | description: "Freedom Fighter, AI Architect & Operator documenting projects, research, and the life behind the work.", |
| 83 | url: "https://www.drakestapleton.com/", |
| 84 | image: "https://www.drakestapleton.com/og.png?v=20260820", |
| 85 | sameAs: ["https://github.com/dhgmonkey"], |
| 86 | alumniOf: { |
| 87 | "@type": "CollegeOrUniversity", |
| 88 | name: "Western Kentucky University", |
| 89 | url: "https://www.wku.edu/", |
| 90 | }, |
| 91 | subjectOf: [ |
| 92 | { |
| 93 | "@type": "Thesis", |
| 94 | name: "Master of Science in Chemistry thesis", |
| 95 | datePublished: "2018", |
| 96 | url: "https://digitalcommons.wku.edu/theses/2101/", |
| 97 | }, |
| 98 | { |
| 99 | "@type": "ScholarlyArticle", |
| 100 | name: "2019 peer-reviewed nanomaterials paper", |
| 101 | datePublished: "2019", |
| 102 | url: "https://pubmed.ncbi.nlm.nih.gov/30931213/", |
| 103 | }, |
| 104 | ], |
| 105 | }; |
| 106 | |
| 107 | const graph = page.path === "/" |
| 108 | ? [ |
| 109 | { |
| 110 | "@type": "WebSite", |
| 111 | "@id": "https://www.drakestapleton.com/#website", |
| 112 | name: "Drake Stapleton", |
| 113 | url: "https://www.drakestapleton.com/", |
| 114 | }, |
| 115 | person, |
| 116 | { |
| 117 | "@type": "FAQPage", |
| 118 | "@id": "https://www.drakestapleton.com/#faq", |
| 119 | mainEntity: [ |
| 120 | { |
| 121 | "@type": "Question", |
| 122 | name: "Why did Drake Stapleton build this site?", |
| 123 | acceptedAnswer: { |
| 124 | "@type": "Answer", |
| 125 | text: "This site documents Drake Stapleton's projects, the work behind them, and the life that shaped them.", |
| 126 | }, |
| 127 | }, |
| 128 | { |
| 129 | "@type": "Question", |
| 130 | name: "What work does Drake Stapleton do?", |
| 131 | acceptedAnswer: { |
| 132 | "@type": "Answer", |
| 133 | text: "I design, operate, and audit production orchestration, GPU systems, and verifiable AI architecture.", |
| 134 | }, |
| 135 | }, |
| 136 | { |
| 137 | "@type": "Question", |
| 138 | name: "What is Atlas?", |
| 139 | acceptedAnswer: { |
| 140 | "@type": "Answer", |
| 141 | text: "Atlas is Drake Stapleton's personal R&D program in accountable AI architecture, continuity, model training, and orchestration.", |
| 142 | }, |
| 143 | }, |
| 144 | ], |
| 145 | }, |
| 146 | ] |
| 147 | : [ |
| 148 | { |
| 149 | "@type": "WebPage", |
| 150 | "@id": `${canonical}#webpage`, |
| 151 | name: page.title, |
| 152 | description: page.description, |
| 153 | url: canonical, |
| 154 | isPartOf: { "@id": "https://www.drakestapleton.com/#website" }, |
| 155 | about: { "@id": "https://www.drakestapleton.com/#person" }, |
| 156 | }, |
| 157 | person, |
| 158 | ]; |
| 159 | |
| 160 | return JSON.stringify({ "@context": "https://schema.org", "@graph": graph }).replaceAll("<", "\\u003c"); |
| 161 | } |
| 162 | |
| 163 | let ssrRender = null; |
| 164 | const ssrPath = join(process.cwd(), "dist-ssr/entry-server.js"); |
| 165 | if (existsSync(ssrPath)) { |
| 166 | try { |
| 167 | const ssrModule = await import(ssrPath); |
| 168 | ssrRender = ssrModule.render; |
| 169 | console.log("SSR pre-rendering active: pre-rendering full semantic HTML for all routes"); |
| 170 | } catch (err) { |
| 171 | console.warn("SSR module failed to load, using noscript fallback:", err.message); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | function withMetadata(html, page) { |
| 176 | const title = escapeText(page.title); |
| 177 | const description = escapeAttribute(page.description); |
| 178 | const canonical = `https://www.drakestapleton.com${page.path}`; |
| 179 | const jsonLd = structuredData(page); |
| 180 | let renderedBody = ""; |
| 181 | let headPreloads = ""; |
| 182 | if (ssrRender && !page.redirect) { |
| 183 | try { |
| 184 | renderedBody = ssrRender(page.path); |
| 185 | renderedBody = renderedBody.replace(/<link rel="preload"[^>]*\/?>/g, (match) => { |
| 186 | headPreloads += ` ${match}\n`; |
| 187 | return ""; |
| 188 | }); |
| 189 | } catch (e) { |
| 190 | console.warn(`SSR render failed for ${page.path}:`, e.message); |
| 191 | renderedBody = noscriptSummary(page); |
| 192 | } |
| 193 | } else { |
| 194 | renderedBody = noscriptSummary(page); |
| 195 | } |
| 196 | return html |
| 197 | .replace(/<title>[^<]*<\/title>/, `<title>${title}</title>`) |
| 198 | .replace(/<meta name="description" content="[^"]*"\s*\/?>/, `<meta name="description" content="${description}" />`) |
| 199 | .replace(/<link rel="canonical" href="[^"]*"\s*\/?>/, `<link rel="canonical" href="${canonical}" />`) |
| 200 | .replace(/<meta property="og:title" content="[^"]*"\s*\/?>/, `<meta property="og:title" content="${escapeAttribute(page.title)}" />`) |
| 201 | .replace(/<meta property="og:description" content="[^"]*"\s*\/?>/, `<meta property="og:description" content="${description}" />`) |
| 202 | .replace(/<meta property="og:url" content="[^"]*"\s*\/?>/, `<meta property="og:url" content="${canonical}" />`) |
| 203 | .replace(/<meta name="twitter:title" content="[^"]*"\s*\/?>/, `<meta name="twitter:title" content="${escapeAttribute(page.title)}" />`) |
| 204 | .replace(/<meta name="twitter:description" content="[^"]*"\s*\/?>/, `<meta name="twitter:description" content="${description}" />`) |
| 205 | .replace(/<script id="structured-data" type="application\/ld\+json">[\s\S]*?<\/script>/, `<script id="structured-data" type="application/ld+json">${jsonLd}</script>`) |
| 206 | .replace("</head>", `${headPreloads} </head>`) |
| 207 | .replace('<div id="root"></div>', `<div id="root">${renderedBody}</div>`); |
| 208 | } |
| 209 | |
| 210 | function writePage(rel, html) { |
| 211 | const destination = join(dist, rel); |
| 212 | mkdirSync(dirname(destination), { recursive: true }); |
| 213 | writeFileSync(destination, html); |
| 214 | console.log("site fallback", destination); |
| 215 | } |
| 216 | |
| 217 | if (mode === "portfolio") { |
| 218 | writeFileSync(indexPath, withMetadata(indexHtml, { path: "/", ...pageMetadata["/"] })); |
| 219 | } |
| 220 | |
| 221 | for (const route of routes) { |
| 222 | const routeHtml = mode === "portfolio" ? withMetadata(indexHtml, route) : indexHtml; |
| 223 | writePage( |
| 224 | route.rel, |
| 225 | mode === "portfolio" && route.redirect |
| 226 | ? routeHtml.replace('content="index, follow"', 'content="noindex, follow"') |
| 227 | : routeHtml, |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | const notFound = mode === "portfolio" |
| 232 | ? withMetadata(indexHtml, { path: "/404", ...pageMetadata["/404"] }).replace('content="index, follow"', 'content="noindex, follow"') |
| 233 | : indexHtml; |
| 234 | writePage("404.html", notFound); |
| 235 | |
| 236 | if (mode === "portfolio") { |
| 237 | const publicRoutes = ["/", ...routes.filter((route) => !route.redirect).map((route) => route.path)]; |
| 238 | const lastmod = new Date().toISOString().slice(0, 10); |
| 239 | const sitemap = [ |
| 240 | '<?xml version="1.0" encoding="UTF-8"?>', |
| 241 | '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">', |
| 242 | ...publicRoutes.map((path) => { |
| 243 | const priority = path === "/" ? "1.0" : new Set(["/interest", "/software"]).has(path) ? "0.9" : "0.7"; |
| 244 | return ` <url><loc>https://www.drakestapleton.com${path}</loc><lastmod>${lastmod}</lastmod><priority>${priority}</priority></url>`; |
| 245 | }), |
| 246 | "</urlset>", |
| 247 | "", |
| 248 | ].join("\n"); |
| 249 | writeFileSync(join(dist, "robots.txt"), "User-agent: *\nAllow: /\nSitemap: https://www.drakestapleton.com/sitemap.xml\nLLMs: https://www.drakestapleton.com/llms.txt\n"); |
| 250 | writeFileSync(join(dist, "sitemap.xml"), sitemap); |
| 251 | } else { |
| 252 | writeFileSync(join(dist, "robots.txt"), "User-agent: *\nDisallow: /\n"); |
| 253 | if (existsSync(join(dist, "sitemap.xml"))) { |
| 254 | throw new Error("Waitlist build retained a sitemap"); |
| 255 | } |
| 256 | } |