Skip to content
Source
Sign inSign up
samwillis/hackernews-realtime
Open History to see the last commit

route.ts

31 lines1004 Bytes

1import { parseStoryId } from "@/lib/comments";
2import { sealStory, sealComments, storyQuery } from "@/lib/queries";
3import { ensureThreadLoaded } from "@/lib/thread-load";
4export const dynamic = "force-dynamic";
5export async function GET(request: Request) {
6 let id: number;
7 try {
8 id = parseStoryId(new URL(request.url).searchParams.get("id"));
9 } catch {
10 return Response.json({ error: "Invalid story ID" }, { status: 400 });
11 }
12 try {
13 if (!(await storyQuery(id)).length)
14 return Response.json({ error: "Story not found" }, { status: 404 });
15 if (new URL(request.url).searchParams.get("load") === "1")
16 await ensureThreadLoaded(id);
17 const [story, comments] = await Promise.all([
18 sealStory(id),
19 sealComments(id),
20 ]);
21 return Response.json(
22 { story, comments },
23 { headers: { "Cache-Control": "no-store" } },
24 );
25 } catch {
26 return Response.json(
27 { error: "Unable to renew the discussion" },
28 { status: 503 },
29 );
30 }