Skip to content

Commit 06ab906

Browse files
committed
fix: implement in-memory caching for probeHeaders function to deduplicate requests during SSR
1 parent b942469 commit 06ab906

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

server/services/headers.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getStatusCode } from "@readme/http-status-codes";
22
import { eq } from "drizzle-orm";
3+
import { cache } from "react";
34
import { IMPORTANT_HEADERS } from "@/lib/constants/headers";
45
import { db } from "@/lib/db/client";
56
import { findDomainByName } from "@/lib/db/repos/domains";
@@ -11,7 +12,14 @@ import { fetchWithSelectiveRedirects } from "@/lib/fetch";
1112
import { scheduleSectionIfEarlier } from "@/lib/schedule";
1213
import type { HttpHeader, HttpHeadersResponse } from "@/lib/schemas";
1314

14-
export async function probeHeaders(
15+
/**
16+
* Probe HTTP headers for a domain with Postgres caching.
17+
*
18+
* Wrapped in React's cache() for per-request deduplication during SSR,
19+
* ensuring multiple components can query headers without triggering
20+
* multiple HTTP requests to the target domain.
21+
*/
22+
export const probeHeaders = cache(async function probeHeaders(
1523
domain: string,
1624
): Promise<HttpHeadersResponse> {
1725
const url = `https://${domain}/`;
@@ -134,7 +142,7 @@ export async function probeHeaders(
134142
// Return empty on failure without caching to avoid long-lived negatives
135143
return { headers: [], status: 0, statusMessage: undefined };
136144
}
137-
}
145+
});
138146

139147
function normalize(h: HttpHeader[]): HttpHeader[] {
140148
// Normalize header names (trim + lowercase) then sort important first

0 commit comments

Comments
 (0)