Next.js Discord

Discord Forum

RevalidateTag recalling apis from server action called page

Unanswered
TyZreX posted this in #help-forum
Open in Discord
request for cart items

const getCartItems = async (session: Session | null) => {
  try {
    const response = await fetch(`http://localhost:8000/api/orders/cart/`, {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${session?.user?.access}`,
      },
      cache: "no-cache",
      next: {
        tags: ["cart"],
      },
    });
    const data = await response.json();
    return data?.results;
  } catch (error) {
    console.log(error);
  }
};



server action for adding to cart
export const addToCart = async (productId: number) => {
        const session = await initSession()
        const url = `orders/cart/add/?product=${productId}`
        const res = await serverProtectedRequest(url,"POST", session)
        //only revalidate a certain path
        // revalidatePath("/user/cart")
        revalidateTag("cart")
        return res   
}

0 Replies