From 2154447ef84f97bee860106158e814183df88645 Mon Sep 17 00:00:00 2001 From: Atiqur Rahman Date: Thu, 3 Oct 2024 23:34:52 -0500 Subject: [PATCH] disable caching on events api for fresh data --- src/app/api/events/route.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/app/api/events/route.ts b/src/app/api/events/route.ts index 16ef291..d4dc070 100644 --- a/src/app/api/events/route.ts +++ b/src/app/api/events/route.ts @@ -1,4 +1,4 @@ -import axios from 'axios'; +import axios from "axios"; import { Event } from "@/lib/types"; // Define the type for the Google Calendar API event @@ -9,9 +9,8 @@ interface GoogleCalendarEvent { dateTime?: string; // Use string because Google Calendar returns dateTime as a string date?: string; // All-day event date }; - location?: string; + location?: string; } - export async function GET(req: Request) { const GOOGLE_CALENDAR_ID = process.env.GOOGLE_CALENDAR_ID; const CALENDAR_API_KEY = process.env.CALENDAR_API_KEY; @@ -26,30 +25,35 @@ export async function GET(req: Request) { // Map to extract the specific information const modifiedEvents: Event[] = events.map((event) => { - - const startDateTime = event.start.dateTime ? new Date(event.start.dateTime) : - (event.start.date ? new Date(event.start.date) : undefined); + const startDateTime = event.start.dateTime + ? new Date(event.start.dateTime) + : event.start.date + ? new Date(event.start.date) + : undefined; return { id: event.id, title: event.summary, start: startDateTime, - location: event.location, + location: event.location, }; }); + // Add Cache-Control: no-store to ensure no caching return new Response(JSON.stringify(modifiedEvents), { status: 200, headers: { - 'Content-Type': 'application/json', + "Content-Type": "application/json", + "Cache-Control": "no-store", // Disable caching for fresh data }, }); } catch (error) { - console.error('Error fetching events:', error); - return new Response(JSON.stringify({ error: 'Failed to fetch events' }), { + console.error("Error fetching events:", error); + return new Response(JSON.stringify({ error: "Failed to fetch events" }), { status: 500, headers: { - 'Content-Type': 'application/json', + "Content-Type": "application/json", + "Cache-Control": "no-store", // Disable caching for error responses as well }, }); }