-
SummaryDescribe the bugWhen using ReproductionRepository: Cached server function: "use server";
import { cacheTag } from "next/cache";
export async function getCachedValue() {
"use cache";
cacheTag("random");
return Math.random();
}Server action (immediate UI update ✅): "use server";
import { revalidateTag } from "next/cache";
export async function invalidateCache() {
revalidateTag("random", { expire: 0 });
}API route (requires page refresh ❌): import { revalidateTag } from "next/cache";
export async function GET() {
revalidateTag("random", { expire: 0 });
return Response.json({ revalidated: true });
}Expected behaviorAccording to the documentation, Actual behavior
QuestionHow can I achieve the same immediate UI update behavior with API route calls as with server actions? Additional information### Version
- Next.js: 16.0.7
- React: 19.2.0
- React DOM: 19.2.0Example |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
That's a misunderstanding there. Server Functions/Actions communicate with the client router to achieve this kind of thing, where they can do UI updates, or redirect based on some special headers returned from the Server Function. An API Route or Router Handler, are a primitive escape hatch, to put it in another way, as far as they are concerned, the rest of your app doesn't exist. I think you'd need to do I'll review the docs to make sure this is properly communicated, I thought it was though. How did you arrive to this conclusion? |
Beta Was this translation helpful? Give feedback.
That's a misunderstanding there. Server Functions/Actions communicate with the client router to achieve this kind of thing, where they can do UI updates, or redirect based on some special headers returned from the Server Function.
An API Route or Router Handler, are a primitive escape hatch, to put it in another way, as far as they are concerned, the rest of your app doesn't exist. I think you'd need to do
router.refreshafter your fetch call to the API or Route Handler succeeds. They are nothing but a REST API call.I'll revie…