[Cache Data] How to revalidate on server side, on Vercel
Unanswered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
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:
And this is on api/banner :
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?
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?
2 Replies
@Asiatic Lion 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: **
js
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 : **
js
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?
you need to revalidate your data cache. You can do that by using the
revalidatePath or revalidateTag function. Like that, the new data will directly be shown when a new request is made. Also, if you are on the same page with the banner and use revalidateTag there, the banner will directly be updated@Asiatic Lion solved?