Error: Cookies can only be modified in a Server Action or Route Handler.
Unanswered
Cape lion posted this in #help-forum
Cape lionOP
Code
Error
All I am trying to do is fetch some data from a supabase database and initially it was working fine but for some reason this error popped out of nowhere and I haven't been able to fix it.
"use server";
import { createServerClient, type CookieOptions } from "@supabase/ssr";
import { cookies } from "next/headers";
export const createClient = async (cookieStore: ReturnType<typeof cookies>) => {
return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
async getAll() {
return (await cookieStore).getAll();
},
setAll(cookiesToSet) {
try {
cookiesToSet.forEach(async ({ name, value, options }) =>
(await cookieStore).set(name, value, options)
);
} catch {
// The `setAll` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
},
}
);
};
Error
⨯ unhandledRejection: Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options
at eval (src/utils/supabase/server.ts:18:34)
16 | try {
17 | cookiesToSet.forEach(async ({ name, value, options }) =>
> 18 | (await cookieStore).set(name, value, options)
| ^
19 | );
20 | } catch {
21 | // The `setAll` method was called from a Server Component.
All I am trying to do is fetch some data from a supabase database and initially it was working fine but for some reason this error popped out of nowhere and I haven't been able to fix it.
3 Replies
Roseate Spoonbill
Have you tried removing
Also, what @Lao Gan Ma mentioned above - if you call server action from non-client code, then it might be treated as regular function call, not a server action.
.next
and/or node_modules
and restarting the app? It happens from time to time that cached assets have issues during development and you need to clear the build results to correct it.Also, what @Lao Gan Ma mentioned above - if you call server action from non-client code, then it might be treated as regular function call, not a server action.
Cape lionOP
I guess there was some issue with my environment.
I tried running it on github codespaces and it works completely fine.
I tried running it on github codespaces and it works completely fine.