Three separate things that were each costing more than they needed to on the hot path. - Single-flight avatar renders. The disk cache was checked first and a miss went straight to the upstream, with nothing shared between callers, so a page requesting dozens of avatars at once turned N concurrent requests for one figure into N renders. A render is the most expensive operation this app does, and the duplication happened exactly when the cache had nothing to offer. Eight concurrent requests now cause one render instead of eight. The map lives on globalThis because Next can evaluate the module more than once per process, and two copies would each start their own render. - Let public read-only routes be cached by a shared cache. Every JSON response was `cache-control: no-store`, so a CDN in front of the app could not answer any of it and every request reached the origin. publicCacheControl() opts a route in with s-maxage and stale-while-revalidate, using the same TTL as the server-side cache so the two layers cannot disagree. The default stays no-store: most routes here are personalised, admin-only or auth-dependent. /api/badges/leaderboard is deliberately left alone because it returns per-viewer rank entries to signed-in callers. Note this only takes effect once a cache rule exists for /api/* at the CDN, or the explicit `cache: "no-store"` is dropped from the client fetches (24 files do that today, including the /api/online poll). The headers alone are inert until one of those happens. - Take the homepage row counts from the storage engine estimate instead of COUNT(*), which walks an index and gets slower as the tables grow. A missing or zero estimate falls back to the exact count rather than ever showing a wrong zero. The online count stays exact: it is an indexed read over a small subset and a few seconds of drift reads as broken rather than approximate. The counters move into one module because the homepage and the boot warm-up populate the same cache keys, so two implementations would race to write different values into the same entry. 3223 tests pass.
88 lines
3.0 KiB
TypeScript
88 lines
3.0 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
/**
|
|
* JSON response helper for the public REST API. Serialises BigInt (DB ids)
|
|
* to strings — JSON.stringify throws on BigInt otherwise — and sets CORS to
|
|
* APP_URL so the game client / external integrations can read it.
|
|
*/
|
|
const CORS_ORIGIN = process.env.APP_URL ?? "http://localhost:3000";
|
|
|
|
/**
|
|
* Opt a response in to HTTP caching.
|
|
*
|
|
* The default is deliberately `no-store`: most routes here are personalised,
|
|
* admin-only, or auth-dependent, and caching any of those would be a data leak.
|
|
* A route may only pass this when its body is fully public and identical for
|
|
* every caller.
|
|
*
|
|
* `s-maxage` is what lets a shared cache (the CDN in front of this app) answer
|
|
* without the request reaching the app at all; `max-age` covers the browser.
|
|
* `stale-while-revalidate` lets that cache keep serving the previous body while
|
|
* it refreshes, which is the same trade-off the in-process cache makes.
|
|
*/
|
|
export function publicCacheControl(seconds: number): string {
|
|
return `public, max-age=${seconds}, s-maxage=${seconds}, stale-while-revalidate=${seconds}`;
|
|
}
|
|
|
|
export function apiJson(data: unknown, init?: ResponseInit): NextResponse {
|
|
const body = JSON.stringify(data, (_k, v) =>
|
|
typeof v === "bigint" ? v.toString() : v,
|
|
);
|
|
return new NextResponse(body, {
|
|
status: init?.status ?? 200,
|
|
headers: {
|
|
"content-type": "application/json; charset=utf-8",
|
|
"access-control-allow-origin": CORS_ORIGIN,
|
|
"cache-control": "no-store",
|
|
...(init?.headers ?? {}),
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Standard success envelope: { ok: true, ...data }. */
|
|
export function apiOk(data?: Record<string, unknown>): NextResponse {
|
|
return apiJson({ ok: true, ...data });
|
|
}
|
|
|
|
/** Standard error envelope. */
|
|
export function apiError(message: string, status = 400): NextResponse {
|
|
return apiJson({ error: message }, { status });
|
|
}
|
|
|
|
/** Safe response for temporary infrastructure failures. */
|
|
export function apiUnavailable(
|
|
message = "Service temporarily unavailable",
|
|
): NextResponse {
|
|
return apiError(message, 503);
|
|
}
|
|
|
|
/** Parse an unsigned, non-zero database identifier without Number precision loss. */
|
|
export function positiveBigInt(raw: string | null): bigint | null {
|
|
if (!raw || !/^\d+$/.test(raw)) return null;
|
|
const value = BigInt(raw);
|
|
return value > 0n ? value : null;
|
|
}
|
|
|
|
/** Clamp a ?page / ?perPage pair from search params. */
|
|
export function pagination(
|
|
searchParams: URLSearchParams,
|
|
defaultPer = 20,
|
|
maxPer = 100,
|
|
) {
|
|
const requestedPage = positiveInteger(searchParams.get("page"), 1);
|
|
const requestedPerPage = positiveInteger(
|
|
searchParams.get("perPage"),
|
|
defaultPer,
|
|
);
|
|
const perPage = Math.min(maxPer, requestedPerPage);
|
|
const maxSafePage = Math.floor(Number.MAX_SAFE_INTEGER / perPage) + 1;
|
|
const page = Math.min(requestedPage, maxSafePage);
|
|
return { page, perPage, skip: (page - 1) * perPage, take: perPage };
|
|
}
|
|
|
|
function positiveInteger(raw: string | null, fallback: number): number {
|
|
if (!raw || !/^\d+$/.test(raw)) return fallback;
|
|
const value = Number(raw);
|
|
return Number.isSafeInteger(value) && value > 0 ? value : fallback;
|
|
}
|