Files
gerbeur/api/lib/static.ts
Vincent Lannurien f171027673
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 41s
v3: added customizable site emoji (logo), small visual tweaks
2026-06-29 15:12:25 +02:00

45 lines
1.3 KiB
TypeScript

import { Context, Next, send } from "@oak/oak";
import { OG_SITE_NAME, SITE_EMOJI } from "../config.ts";
import { emojiToCodepoint } from "./site-icons.ts";
const ICON_VERSION = emojiToCodepoint(SITE_EMOJI);
async function serveIndexHtml(
context: Context<Record<string, object>>,
root: string,
) {
const filePath = `${root}/index.html`;
const raw = await Deno.readTextFile(filePath);
const html = raw
.replaceAll("__SITE_NAME__", OG_SITE_NAME)
.replaceAll("__SITE_EMOJI__", SITE_EMOJI)
.replaceAll("__ICON_VERSION__", ICON_VERSION);
context.response.type = "text/html";
context.response.body = html;
}
export function routeStaticFilesFrom(staticPaths: string[]) {
return async (context: Context<Record<string, object>>, next: Next) => {
const pathname = context.request.url.pathname;
// Serve index.html with runtime placeholder replacement
if (pathname === "/" || pathname === "/index.html") {
await serveIndexHtml(context, staticPaths[0]);
return;
}
for (const path of staticPaths) {
try {
await send(context, pathname, { root: path });
return;
} catch {
continue;
}
}
// SPA fallback: serve index.html so client-side routes work on direct navigation
await serveIndexHtml(context, staticPaths[0]);
await next();
};
}