Next.js Discord

Discord Forum

How to make a server action never cache?

Unanswered
North Pacific hake posted this in #help-forum
Open in Discord
North Pacific hakeOP
Hi there I am using a client component to poll my server with a server action on a 5 second interval, however it seems like the return value is cached and my server actually never receives the request. Only when I refresh I see the backend react and give fresh data. How do I make it so that my poll always goes through, basically how do I make the action/client never cache?

11 Replies

Multiflora rose seed chalcid
What exactly do you mean by "return value cached"?
Only when I refresh I see the backend react and give fresh data.

Are you using revalidateTag or revalidatePath to update the cache?
North Pacific hakeOP
So I am using a client component with useEffect which runs my server action returning a simple object response. Then I have a 5 second poll, this poll also runs the same action however the server does not receive the request.
Multiflora rose seed chalcid
I don't know if a Server Action is the right fit for this.

Any reason not to use an async Server Component to fetch the data instead?
North Pacific hakeOP
I could not get some tailwindui component to work on server so i made it client (dont need ssr for this page)
i am not exactly sure what you're suggesting btw
i assumed i would be able to just make it so that the poll goes through to the server
blunder on my part... i forgot to dispatch the action second time with redux
thought the response is cached but i simply never sent the actual request
Willow shoot sawfly
// disable cache for this server action
const _cookies = cookies();

export const getRecentAppointmentList = async () => {
  try {
    // disable cache for this server action
    const _cookies = cookies();

    const appointments = await databases.listDocuments(
      DATABASE_ID as string,
      APPOINTMENT_COLLECTION_ID as string,
      [Query.orderDesc("$createdAt")]
    );

    const initialCounts = {
      pending: 0,
      cancelled: 0,
      appointments: 0,
    };

    const counts = (appointments.documents as Appointment[]).reduce(
      (acc: any, appointment: any) => {
        if (appointment.status === "pending") {
          acc.pending += 1;
        } else if (appointment.status === "cancelled") {
          acc.cancelled += 1;
        } else {
          acc.appointments += 1;
        }
        return acc;
      },
      initialCounts
    );

    const data = {
      ...counts,
      totalCount: appointments.total,
      documents: appointments.documents,
    };

    return parseStringify(data);
  } catch (error) {
    console.error(
      "An error occurred while retrieving the appointments:",
      error
    );
  }
};