Skip to content

Cache all required routes and enforce client-side caching #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/api/solutions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
4 changes: 4 additions & 0 deletions app/api/topics/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion components/SolutionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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") || "";

Expand Down
2 changes: 1 addition & 1 deletion utils/progressUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface ProgressData {

export async function fetchUserProgress(): Promise<ProgressData> {
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}`);
}
Expand Down