#[Cache Data] How to revalidate on server side, on Vercel

3 messages · Page 1 of 1 (latest)

kind tiger
#

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?

hard elmBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

hallow garden