ReviewOS

also looking at this

stacks/bench-review

Adopt the hq-apps app conventions

#76
Merged glennmichael123 wants to merge chore/hq-apps-conventions into main
31 files +1656 -395
preview.tsmodified+46-2
Changes to preview.ts
@@ -49,8 +49,9 @@ const distDir = 'dist'
4949 * /api/judges, …). In dev the views server proxies it; a static host does
5050 * not. Without the proxy every API call resolves against the static origin
5151 * 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.
5455 *
5556 * On CloudFront (config/cloud.ts sets driver: 'aws') both are cache behaviours:
5657 * an /api/* behaviour with the API as origin, and a CloudFront Function or a
@@ -62,12 +63,55 @@ const SHELL_REWRITES: Array<[RegExp, string]> = [
6263 [/^\/verify-email\/[^/]+\/[^/]+\/?$/, '/verify-email.html'],
6364]
6465
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 */
79const apiOrigin = (process.env.API_ORIGIN || 'http://localhost:4008').replace(/\/+$/, '')
80
6581Bun.serve({
6682 port,
6783 async fetch(req) {
6884 const url = new URL(req.url)
6985 let pathname = url.pathname
7086
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
71115 // Shell rewrites run FIRST — before the .html mapping below, which would
72116 // otherwise turn /verify-email/1/abc into a lookup for a file that by
73117 // definition cannot exist.