Hi!
Question about Cache Data: i've a banner on my "/" who come from a server side request (no fetch, just a .findOne with mongoose)
I've a Dashboard who can update the banner, which is doing a client side fetch to "api/banner" to update the banner. Following this code :
** from the Client Component: **
const saveBanner = async (bannerData: BannerTypes) => {
const response = await fetch("/api/banner", {
method: banner ? "PUT" : "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(bannerData),
cache: "no-store",
});
if (response.ok) {
showNotification(
"success",
`Bannière en ${banner ? "modifiée" : "ajoutée"} !`
);
router.refresh();
router.push("/admin/banner");
setBanner({
...banner,
message: bannerData.message,
});
}
};
** And this is on api/banner : **
export async function PUT(request: NextRequest) {
const authResponse = await middleware(request);
if (authResponse.status !== 200) return authResponse;
await dbConnect();
try {
const body = await request.json();
const banner = await Banner.findOneAndUpdate({}, body, { new: true, upsert: true });
return NextResponse.json({ success: true, data: banner });
} catch (error) {
if (error instanceof Error) {
return NextResponse.json({ success: false, error: error.message }, { status: 400 });
}
return NextResponse.json({ success: false, error: 'Unknown error' }, { status: 400 });
}
}
It works on localhost! But on Vercel it doesn't work because the Data Cache.. I need to purge it on vercel to see the update.
Why ? What can i improve to update correctly the data and have the latest informations?