Next.js Discord

Discord Forum

Disable Cache for Server Action

Answered
Harlequin Duck posted this in #help-forum
Open in Discord
Harlequin DuckOP
I have a server action:
"use server";
const pullData = async () => {
  // Library functions that use fetch to get the needed data
  return data;
};
export default pullData;


My parent page.tsx is not "use client" and has:
export const dynamic = "force-dynamic";
export const revalidate = 0;


A child Component from my page then uses the server action:
"use client";
import pullData from "./pull-data";
const BrowserPage = (props: { initialData: number }) => {
  pullData().then((e) => console.log("browser log: " + e));
  return <>{/*data*/}</>;
};
export default BrowserPage;


However, this always logs a cached value. How can i tell next to not use the cache when executing the server action?

If i call the same pullData() function in my async server page, i get the non cached, real value. But if i use pullData() via a server action (from a "use client file") i get a cached value.
If i do both at the same time and log the value in the function itself i even get 2 different values logged.
Answered by Harlequin Duck
Found the fix:
import { unstable_noStore as noStore } from "next/cache";
const pullData = async () => {
  noStore();
//....
View full answer

1 Reply

Harlequin DuckOP
Found the fix:
import { unstable_noStore as noStore } from "next/cache";
const pullData = async () => {
  noStore();
//....
Answer