Next.js Discord

Discord Forum

Revalidate path not working on route but working on server action.

Answered
Tan posted this in #help-forum
Open in Discord
TanOP
I am trying to revalidate path via api route its not working. While from server action it works. I have a page that has a Post route handler via /api route and a delete action via server actions.

It looks like this
// API route
      await connectDB();
      const newDoc = new Doc({ title, url });
      const savedDoc = await newDoc.save();
      revalidatePath("/admin/documents");
      console.log("SAVED DOCUMENT:", savedDoc);
      return NextResponse.json(
        {
          message: "Successfully added new document",
        },
        { status: 200 },
      );

While I have defined my delete action like this
export const deleteDocument = async (id: string) => {
  const doc = await Doc.findById(id);
  if (!doc) throw new Error("Document couldn't found !");
  try {
    const res = await fetch(doc.url, {
      method: "DELETE",
      headers: {
        "X-Custom-Auth-Key": getAuthKeySecret(),
      },
    });

    if (res.ok) {
      try {
        await connectDB();
        await Doc.findByIdAndDelete(id);
        revalidatePath("/admin/documents");
        revalidatePath("/documents");
      } catch (err) {
        console.log(err);
      }
    }
  } catch (err) {
    console.log(err);
  }
};

While I add new documents it revalidates. But while I delete it doesn't.
NOTE: I am also using middleware to route to different language
  const pathname = req.nextUrl.pathname;
  const newUrl = new URL(`/${lang}${pathname}`, req.nextUrl);

return NextResponse.rewrite(newUrl);
Answered by @ts-ignore
if I am not wrong the revalidatePath invalidates the client side router cache when called in server action and server side route cache when called in route handlers, to update the page you might have to call router.refresh(please correct me if I am wrong)
View full answer

2 Replies

@Tan I am trying to revalidate path via api route its not working. While from server action it works. I have a page that has a Post route handler via `/api` route and a delete action via `server actions`. It looks like this ts // API route await connectDB(); const newDoc = new Doc({ title, url }); const savedDoc = await newDoc.save(); revalidatePath("/admin/documents"); console.log("SAVED DOCUMENT:", savedDoc); return NextResponse.json( { message: "Successfully added new document", }, { status: 200 }, ); While I have defined my delete action like this ts export const deleteDocument = async (id: string) => { const doc = await Doc.findById(id); if (!doc) throw new Error("Document couldn't found !"); try { const res = await fetch(doc.url, { method: "DELETE", headers: { "X-Custom-Auth-Key": getAuthKeySecret(), }, }); if (res.ok) { try { await connectDB(); await Doc.findByIdAndDelete(id); revalidatePath("/admin/documents"); revalidatePath("/documents"); } catch (err) { console.log(err); } } } catch (err) { console.log(err); } }; While I add new documents it revalidates. But while I delete it doesn't. NOTE: I am also using middleware to route to different language ts const pathname = req.nextUrl.pathname; const newUrl = new URL(`/${lang}${pathname}`, req.nextUrl); return NextResponse.rewrite(newUrl);
if I am not wrong the revalidatePath invalidates the client side router cache when called in server action and server side route cache when called in route handlers, to update the page you might have to call router.refresh(please correct me if I am wrong)
Answer