Next.js Discord

Discord Forum

Server Action Question

Answered
Japanese Bobtail posted this in #help-forum
Open in Discord
Japanese BobtailOP
Hey,

I've been trying to manage cookies through server actions in my server-side page with the app router. However, I always get the error Error: Cookies can only be modified in a Server Action or Route Handler.. Can anyone please explain what am I doing wrong?

import { ClientComponent } from "@/components/ClientComponent";
import { cookies } from "next/headers";

export default async function Home() {
  async function getData() {
    "use server";

    cookies().delete("access_token");

    return "hello world";
  }

  const data = await getData();

  return <ClientComponent data={data} />;
}

2 Replies