Skip to content

Commit 9373b1c

Browse files
Merge pull request #16 from enricobattocchi/claude/fix-search-cache-QFBXY
fix(search): include term/locale in unstable_cache keyParts
2 parents bb11a3f + 3d2b1e4 commit 9373b1c

3 files changed

Lines changed: 30 additions & 12 deletions

File tree

src/app/api/search/route.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ export async function GET(request: NextRequest) {
1414

1515
try {
1616
const events = await searchWikidata(sanitized, lang);
17-
return NextResponse.json(events, {
18-
headers: { "Cache-Control": "public, s-maxage=86400, stale-while-revalidate=604800" },
19-
});
17+
return NextResponse.json(events);
2018
} catch {
2119
return NextResponse.json(
2220
{ error: "Failed to search Wikidata" },

src/lib/wikidata.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, vi, afterEach } from "vitest";
2-
import { bestClaim, fetchWithRetry, type WikidataClaim } from "./wikidata";
2+
import { bestClaim, fetchWithRetry, searchWikidata, type WikidataClaim } from "./wikidata";
33

44
vi.mock("next/cache", () => ({
55
unstable_cache: (fn: (...args: unknown[]) => unknown) => fn,
@@ -123,3 +123,22 @@ describe("bestClaim", () => {
123123
expect((result!.mainsnak.datavalue!.value as { time: string }).time).toBe("+2026-04-01T00:00:00Z");
124124
});
125125
});
126+
127+
describe("searchWikidata", () => {
128+
afterEach(() => {
129+
vi.restoreAllMocks();
130+
});
131+
132+
it("propagates the term into the Wikidata search URL", async () => {
133+
const empty = () =>
134+
new Response(JSON.stringify({ search: [] }), { status: 200 });
135+
const fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(async () => empty());
136+
137+
await searchWikidata("Napoleon", "en");
138+
await searchWikidata("Leonardo", "en");
139+
140+
const urls = fetchSpy.mock.calls.map((c) => String(c[0]));
141+
expect(urls.some((u) => u.includes("search=Napoleon"))).toBe(true);
142+
expect(urls.some((u) => u.includes("search=Leonardo"))).toBe(true);
143+
});
144+
});

src/lib/wikidata.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -520,15 +520,16 @@ async function _searchWikidata(term: string, locale: string): Promise<Event[]> {
520520
return events.filter((e) => e.link);
521521
}
522522

523-
const _cachedSearchWikidata = unstable_cache(
524-
_searchWikidata,
525-
["wikidata-search"],
526-
{ revalidate: 86400, tags: ["wikidata"] }
527-
);
528-
529-
/** Search Wikidata for events matching a search term (cross-request cached, 24h TTL) */
523+
/** Search Wikidata for events matching a search term.
524+
* Cached per (term, locale) for 24h — identical queries reuse the same entry,
525+
* different queries get distinct cache entries. */
530526
export async function searchWikidata(term: string, locale: string = "en"): Promise<Event[]> {
531-
return _cachedSearchWikidata(term, locale);
527+
const cached = unstable_cache(
528+
() => _searchWikidata(term, locale),
529+
["wikidata-search", term, locale],
530+
{ revalidate: 86400, tags: ["wikidata"] }
531+
);
532+
return cached();
532533
}
533534

534535
/** Fetch specific events by their Q-IDs (cross-request cached 24h + per-render deduplicated) */

0 commit comments

Comments
 (0)