Wait for revalidatePath to complete #53206
-
|
I find it would be useful if // actions.js
"use server";
export async function action(id, pathname) {
await db.deactivateItem(id);
await revalidatePath(pathname); // <-- Allows to return from the function only after revalidation happens
}// page.js
import Items from "./client.js";
export default async function ItemsPage() {
const items = await db.getItems();
return <Items items={items}/>
}// client.js
"use client";
import {useTransition} from "react";
import {usePathname} from "next/navigation";
import {action} from "./actions.js"
export default function Items({items}) {
const listItems = items.map(item => <Item item={item}/>);
return <ul>{listItems}</ul>;
}
function Item({item}) {
const pathname = usePathname();
const [isLoading, startTransition] = useTransition();
const handler = () => {
startTransition(() => {
action(item.id, pathname).catch();
});
};
let className;
if (isLoading) {
className = "loading";
}
else if (!item.active) {
className = "inactive";
}
else {
className = "default";
}
return <li>
<span className={className}>
{item.name}
</span>
<button onClick={handler}>deactivate</button>
</li>;
}This way, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
|
@asnaeb did you find a workaround for this issue? |
Beta Was this translation helpful? Give feedback.
-
|
Hey! Let me try and explain.
We believe the non-blocking API is a better default for the App Router. You have a couple options here.
|
Beta Was this translation helpful? Give feedback.
Hey! Let me try and explain.
revalidateTagis different thanres.revalidatein the Pages Router:revalidateTagis a "purge", so it is not blocking to retrieve new data. There isn't a way to wait for the event to finish.res.revalidatecan beawait-ed, and it does wait for the entire revalidation to complete.We believe the non-blocking API is a better default for the App Router. You have a couple options here.
pages/api) from your Server Action, which callsres.revalidate.revalidatePathand then callfetchon the path, to initiate the revalidation of the data.