From c349f5f3209fa0563600acbca74f33d60d916a90 Mon Sep 17 00:00:00 2001 From: Pratham Dubey <134331217+prathamdby@users.noreply.github.com> Date: Thu, 17 Jul 2025 18:36:29 +0530 Subject: [PATCH] Add cache control headers and force-cache to API requests (#23) Set 'Cache-Control' headers in solutions and topics API routes to improve caching. Updated fetch calls in dashboard, solution dialog, and progress utils to use 'force-cache' for better client-side caching. --- app/api/solutions/route.ts | 6 +++++- app/api/topics/route.ts | 4 ++++ app/dashboard/page.tsx | 2 +- components/SolutionDialog.tsx | 5 ++++- utils/progressUtils.ts | 2 +- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/api/solutions/route.ts b/app/api/solutions/route.ts index 6af8717..69a7152 100644 --- a/app/api/solutions/route.ts +++ b/app/api/solutions/route.ts @@ -14,7 +14,11 @@ export async function GET(request: Request) { const result = await model.generateContent(prompt); const solution = result?.response?.text?.(); - return NextResponse.json({ solution }); + return NextResponse.json({ solution }, { + headers: { + 'Cache-Control': 'public, max-age=3600, stale-while-revalidate=86400' + } + }); } catch (error: any) { console.error("API Error:", error.message || error); const message = error.message || "An unknown error occurred while generating the solution"; diff --git a/app/api/topics/route.ts b/app/api/topics/route.ts index 00baa3e..5662019 100644 --- a/app/api/topics/route.ts +++ b/app/api/topics/route.ts @@ -7,6 +7,10 @@ export async function GET() { return NextResponse.json({ topics, + }, { + headers: { + 'Cache-Control': 'public, max-age=3600, stale-while-revalidate=60' + } }); } catch (error) { console.error("Error fetching topics:", error); diff --git a/app/dashboard/page.tsx b/app/dashboard/page.tsx index 2bcccbf..7b053be 100644 --- a/app/dashboard/page.tsx +++ b/app/dashboard/page.tsx @@ -15,7 +15,7 @@ export default function DashboardPage() { useEffect(() => { if (userId) { - fetch("/api/questions") + fetch("/api/questions", { cache: 'force-cache' }) .then((res) => res.json()) .then((data) => { setQuestions(data.questions); diff --git a/components/SolutionDialog.tsx b/components/SolutionDialog.tsx index d2a2dff..6d42084 100644 --- a/components/SolutionDialog.tsx +++ b/components/SolutionDialog.tsx @@ -39,7 +39,10 @@ export function SolutionDialog({ questionId, title }: SolutionDialogProps) { const response = await fetch( `/api/solutions?id=${questionId}&language=${preferredLanguage}`, - { headers } + { + headers, + cache: 'force-cache' + } ); const contentType = response.headers.get("content-type") || ""; diff --git a/utils/progressUtils.ts b/utils/progressUtils.ts index 43115de..05a6696 100644 --- a/utils/progressUtils.ts +++ b/utils/progressUtils.ts @@ -4,7 +4,7 @@ export interface ProgressData { export async function fetchUserProgress(): Promise { try { - const response = await fetch("/api/progress"); + const response = await fetch("/api/progress", { cache: 'force-cache' }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); }