From 2b14d95fd2dc477e5693a80e5588cde9c9888ce4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 17 Jul 2026 06:47:12 +0000 Subject: [PATCH 1/2] perf: optimize OG image fetch by deduping concurrent requests Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com> --- src/app/api/og/[username]/route.tsx | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/app/api/og/[username]/route.tsx b/src/app/api/og/[username]/route.tsx index 0223dd1d..5488ad08 100644 --- a/src/app/api/og/[username]/route.tsx +++ b/src/app/api/og/[username]/route.tsx @@ -7,6 +7,7 @@ import { RateLimiter } from "@/lib/rateLimit"; import { getClientIp } from "@/lib/rateLimit"; export const runtime = "edge"; +const inflightFetch = new Map>(); const rateLimiter = new RateLimiter(50, 60 * 1000); const ONE_HOUR_IN_SECONDS = 60 * 60; const ONE_DAY_IN_SECONDS = 24 * ONE_HOUR_IN_SECONDS; @@ -42,13 +43,25 @@ export async function GET( let publicRepos = 0; try { - const res = await fetch(`https://api.github.com/users/${encodeURIComponent(username)}`, { - headers: { - Accept: "application/vnd.github.v3+json", - "User-Agent": "github-user-summary", - }, - next: { revalidate: ONE_DAY_IN_SECONDS }, - }); + const url = `https://api.github.com/users/${encodeURIComponent(username)}`; + let fetchPromise = inflightFetch.get(url); + if (!fetchPromise) { + fetchPromise = fetch(url, { + headers: { + Accept: "application/vnd.github.v3+json", + "User-Agent": "github-user-summary", + }, + next: { revalidate: ONE_DAY_IN_SECONDS }, + }).finally(() => { + inflightFetch.delete(url); + }); + inflightFetch.set(url, fetchPromise); + } + + // We clone the response because multiple concurrent requests + // will need to read the body stream independently + const res = (await fetchPromise).clone(); + if (res.ok) { const data = await res.json(); name = data.name ?? username; From eabd4fc199eabc8ea1a42be29fbc35ea0fa08363 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 17 Jul 2026 06:51:34 +0000 Subject: [PATCH 2/2] test: add coverage for concurrent deduplication fetch in OG image route Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com> --- src/app/api/og/[username]/route.test.ts | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/app/api/og/[username]/route.test.ts b/src/app/api/og/[username]/route.test.ts index edabdca5..806a96e4 100644 --- a/src/app/api/og/[username]/route.test.ts +++ b/src/app/api/og/[username]/route.test.ts @@ -149,6 +149,35 @@ describe("OG Image Route", () => { expect(res.status).toBe(429); expect(res.headers.get("Retry-After")).toBe("0"); }); + + it("should dedupe concurrent fetches for the same username", async () => { + // Create a delayed fetch so multiple concurrent GETs will hit the inflight map + let callCount = 0; + const mockFetch = vi.spyOn(global, "fetch").mockImplementation(() => { + callCount++; + return new Promise((resolve) => { + setTimeout(() => { + resolve(new Response(JSON.stringify({ name: "Concurrent User" }), { status: 200 })); + }, 10); + }); + }); + + const req1 = new NextRequest("http://localhost/api/og/concurrentuser"); + const req2 = new NextRequest("http://localhost/api/og/concurrentuser"); + + // Fire concurrently + const [res1, res2] = await Promise.all([ + GET(req1, { params: Promise.resolve({ username: "concurrentuser" }) }), + GET(req2, { params: Promise.resolve({ username: "concurrentuser" }) }) + ]); + + expect(res1.status).toBe(200); + expect(res2.status).toBe(200); + expect(callCount).toBe(1); // fetch should only be called once + expect(mockFetch).toHaveBeenCalledTimes(1); + + // We should restore fetch, though afterEach does that + }); }); function collectText(node: unknown): string[] {