revalidatePath in Route Handler
Answered
Devon Rex posted this in #help-forum
Devon RexOP
Hi All, does anyone got that revalidatePath working in API routes? According to the docs it should work in Route Handler(https://nextjs.org/docs/app/api-reference/functions/revalidatePath#route-handler)
I've got something like:
and it doesn't seem to be triggering anything to revalidate.
I tried the way from docs exactly but:
What am I missing here? Thanks in advance.
I've got something like:
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
await deleteResource((await params).id);
revalidatePath("/ui/dashboard/resources");
return NextResponse.json(
{ message: "Deleted successfully" },
{ status: 200 }
);
} catch (error) {
return NextResponse.json(
{ message: "Error deleting item", error },
{ status: 500 }
);
}
}
and it doesn't seem to be triggering anything to revalidate.
I tried the way from docs exactly but:
const path = request.nextUrl.searchParams.get('path')
gives me null.What am I missing here? Thanks in advance.
Answered by LuisLl
Are you calling this Route Handler from a Client Component and your current page isn’t getting refreshed?
If that’s the issue that’s expected behavior,
You can do
Or you can use a Server Action and it’ll automatically update the UI in the same request/response roundtrip since Server Actions can do both mutate data and return updated UI sending the new RSC payload.
If that’s the issue that’s expected behavior,
revalidatePath()
(and tag) will only revalidate the page for the next visit. You can do
router.refresh()
from your client component right after you perform the mutation instead. Or you can use a Server Action and it’ll automatically update the UI in the same request/response roundtrip since Server Actions can do both mutate data and return updated UI sending the new RSC payload.
3 Replies
Are you calling this Route Handler from a Client Component and your current page isn’t getting refreshed?
If that’s the issue that’s expected behavior,
You can do
Or you can use a Server Action and it’ll automatically update the UI in the same request/response roundtrip since Server Actions can do both mutate data and return updated UI sending the new RSC payload.
If that’s the issue that’s expected behavior,
revalidatePath()
(and tag) will only revalidate the page for the next visit. You can do
router.refresh()
from your client component right after you perform the mutation instead. Or you can use a Server Action and it’ll automatically update the UI in the same request/response roundtrip since Server Actions can do both mutate data and return updated UI sending the new RSC payload.
Answer
Devon RexOP
yes. that was the case. I took server action path. Thanks for shedding some light here.
Cool, is it solved then?