21 lines · 1.1 KBRaw
| 1 | import type { Route } from "./+types/repo-code-graph"; |
| 2 | import { refsErrorCode } from "@refs/backend/contract"; |
| 3 | import { refs, fullName } from "../lib/session"; |
| 4 | |
| 5 | /** |
| 6 | * The code map of one commit, as JSON. A commit never changes, so a ready map is cached for good and a second |
| 7 | * visit opens it without a request. A map that is still building answers 202, so the page asks again. |
| 8 | */ |
| 9 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 10 | try { |
| 11 | const repo = await refs(context).repo(fullName(params)); |
| 12 | const map = await repo.codeGraph(params.commit); |
| 13 | if (map.kind !== "ready") return Response.json(map, { status: map.kind === "indexing" ? 202 : 409, headers: { "cache-control": "no-store" } }); |
| 14 | return new Response(JSON.stringify(map.graph), { |
| 15 | headers: { "content-type": "application/json", "cache-control": `${repo.summary.visibility === "public" ? "public" : "private"}, max-age=31536000, immutable` }, |
| 16 | }); |
| 17 | } catch (error) { |
| 18 | if (refsErrorCode(error)) return Response.json({ kind: "unavailable" }, { status: 404 }); |
| 19 | throw error; |
| 20 | } |
| 21 | } |