Cache Confusion
Unanswered
oz posted this in #help-forum
ozOP
Hey guys I am using NextJS 14.2 App Router and I am trying to basically cache the response of an API route UNLESS my admin dashboard pushed updates to the data, if so then I wnat the cache to reset. I thought I did this but in production, the cache never updates no matter how many times I update data.
Essentially I am making a product deal eCommerce app so I need to ensure the data I am serving is the latest as if a deal ends the customer needs to know.
Relevant code:
Frontend Script
GET API In Question
Update Server Action which should update after run.
Essentially I am making a product deal eCommerce app so I need to ensure the data I am serving is the latest as if a deal ends the customer needs to know.
Relevant code:
Frontend Script
document.addEventListener("DOMContentLoaded", function () {
// Fetch the flash deals data from the API
fetch("https://xxxxx/api/flash-deals")
.then(response => response.json())
.then(data => {
const dealProductIds = data.map(deal => deal.productId);
const dealsInfo = data.reduce((acc, deal) => {
acc[deal.productId] = deal;
return acc;
}, {});
...GET API In Question
import { NextRequest, NextResponse } from "next/server";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export async function GET(request: NextRequest) {
try {
const flashDeals = await prisma.flashDeal.findMany({
...
const cacheOptions = {
revalidate: 3600,
staleWhileRevalidate: true,
tags: ["flash-deals"],
};
return NextResponse.json(
...
{
headers: {
"Cache-Control": `max-age=${cacheOptions.revalidate}, stale-while-revalidate=${cacheOptions.staleWhileRevalidate}`,
},
},
);
} ...Update Server Action which should update after run.
"use server";
import { PrismaClient } from "@prisma/client";
import { revalidatePath } from "next/cache";
...
export async function updateFlashDeal(flashDeal: FlashDeal) {
update logic...
revalidatePath("/");
return {
success: true,
message: "Flash Deal updated successfully!",
};
} catch (error) {
console.error("Failed to update Flash Deal:", error);
return {
success: false,
message: "Failed to update Flash Deal. Please try again.",
};
}
}