also looking at this
Adopt the hq-apps app conventions
#76
31 files
+1656
-395
| @@ -49,8 +49,9 @@ const distDir = 'dist' | ||
| 49 | 49 | * /api/judges, …). In dev the views server proxies it; a static host does |
| 50 | 50 | * not. Without the proxy every API call resolves against the static origin |
| 51 | 51 | * and 404s, so the site renders its shells and then stays empty forever. |
| 52 | * Note this preview server does NOT fake it, so `bun run preview` shows | |
| 53 | * chrome without data — that is expected here, not a bug. | |
| 52 | * This server DOES fake it (see API_ORIGIN below) so the built artifact can | |
| 53 | * be exercised end-to-end locally — but that is a local convenience, not a | |
| 54 | * substitute for configuring it at the host. | |
| 54 | 55 | * |
| 55 | 56 | * On CloudFront (config/cloud.ts sets driver: 'aws') both are cache behaviours: |
| 56 | 57 | * an /api/* behaviour with the API as origin, and a CloudFront Function or a |
| @@ -62,12 +63,55 @@ const SHELL_REWRITES: Array<[RegExp, string]> = [ | ||
| 62 | 63 | [/^\/verify-email\/[^/]+\/[^/]+\/?$/, '/verify-email.html'], |
| 63 | 64 | ] |
| 64 | 65 | |
| 66 | /** | |
| 67 | * Where /api/* is forwarded, so the BUILT artifact can be exercised locally | |
| 68 | * against a real API. | |
| 69 | * | |
| 70 | * Without this, previewing dist/ was only half an end-to-end: the pre-rendered | |
| 71 | * pages, the per-entity SEO and the hashed CSP were all real, but every store | |
| 72 | * call 404'd against the static origin, so nothing ever loaded data. Meanwhile | |
| 73 | * the dev server has the API but serves from source, so it shows NONE of the | |
| 74 | * build-time output. Neither one alone exercises what actually ships. | |
| 75 | * | |
| 76 | * Defaults to the dev API port. Override with API_ORIGIN to point at a deployed | |
| 77 | * one. | |
| 78 | */ | |
| 79 | const apiOrigin = (process.env.API_ORIGIN || 'http://localhost:4008').replace(/\/+$/, '') | |
| 80 | ||
| 65 | 81 | Bun.serve({ |
| 66 | 82 | port, |
| 67 | 83 | async fetch(req) { |
| 68 | 84 | const url = new URL(req.url) |
| 69 | 85 | let pathname = url.pathname |
| 70 | 86 | |
| 87 | // API proxy runs before everything: /api/... has no file behind it, and the | |
| 88 | // extensionless-to-.html mapping below would otherwise turn it into a | |
| 89 | // confusing 404 for a page nobody asked for. | |
| 90 | if (pathname.startsWith('/api/')) { | |
| 91 | const target = `${apiOrigin}${pathname}${url.search}` | |
| 92 | try { | |
| 93 | const upstream = await fetch(target, { | |
| 94 | method: req.method, | |
| 95 | headers: req.headers, | |
| 96 | body: req.method === 'GET' || req.method === 'HEAD' ? undefined : await req.arrayBuffer(), | |
| 97 | redirect: 'manual', | |
| 98 | }) | |
| 99 | return new Response(upstream.body, { | |
| 100 | status: upstream.status, | |
| 101 | statusText: upstream.statusText, | |
| 102 | headers: upstream.headers, | |
| 103 | }) | |
| 104 | } | |
| 105 | catch { | |
| 106 | // Distinct from a 404: the API simply isn't up. Say so, rather than | |
| 107 | // letting the client see an ambiguous failure and blame the artifact. | |
| 108 | return new Response( | |
| 109 | JSON.stringify({ error: `preview: no API reachable at ${apiOrigin}. Start it, or set API_ORIGIN.` }), | |
| 110 | { status: 502, headers: { 'content-type': 'application/json' } }, | |
| 111 | ) | |
| 112 | } | |
| 113 | } | |
| 114 | ||
| 71 | 115 | // Shell rewrites run FIRST — before the .html mapping below, which would |
| 72 | 116 | // otherwise turn /verify-email/1/abc into a lookup for a file that by |
| 73 | 117 | // definition cannot exist. |