| 1 | import { Effect } from "effect"; |
| 2 | import { Server } from "foldkit/experimental"; |
| 3 | |
| 4 | import { Flags, init, view } from "./main.ts"; |
| 5 | |
| 6 | // THE SERVER ENTRY — one Web Request in, one delivery result out. The Worker |
| 7 | // places the result into the HTML shell; nothing here knows who called it. |
| 8 | // The same entry is what a build-time prerender would call. |
| 9 | |
| 10 | // The count comes off the query string purely so the render has something |
| 11 | // request-shaped to do: `/?count=7` serves a 7 before any JavaScript runs. |
| 12 | const flagsForRequest = (request: Request): Flags => { |
| 13 | const raw = new URL(request.url).searchParams.get("count"); |
| 14 | const parsed = raw === null ? Number.NaN : Number(raw); |
| 15 | return { initialCount: Number.isFinite(parsed) ? parsed : 0 }; |
| 16 | }; |
| 17 | |
| 18 | export const renderPage = (request: Request): Promise<Server.EntryResult> => |
| 19 | Effect.runPromise( |
| 20 | Effect.gen(function* () { |
| 21 | const application = yield* Server.renderToString( |
| 22 | { Flags, init, view }, |
| 23 | { |
| 24 | flags: flagsForRequest(request), |
| 25 | buildId: import.meta.env.FOLDKIT_BUILD_ID, |
| 26 | }, |
| 27 | ); |
| 28 | return Server.Rendered(application); |
| 29 | }), |
| 30 | ); |