Next.js Discord

Discord Forum

[NextJS 15] Set cookies "Error: Only plain objects...

Answered
Dwarf Crocodile posted this in #help-forum
Open in Discord
Avatar
Dwarf CrocodileOP
Hi, I'm migrating my codebase from Nextjs14 to NextJS15

I have a server action file containing the following code
'use server' 
export async function setCookie(cookieName: string, content: string) {
    console.log({ cookieName, content })
    return (await cookies()).set(cookieName, content)
}


And this function called inside a Client component like this
    useEffect(() => {
        if (basket && !isEmptyObject(basket)) {
            setCookie('basket', 'lol')
        }
    }, [basket])


It used to work perfectly, but now NextJS 15 is throwing this error :
[ Server ] Error: Only plain objects, and a few built-ins, can be passed to Client Components from Server Components. Classes or null prototypes are not supported.
  {}


See how I explicitly send a "lol" string as a parameter ? What is the reason here ?

Maybe I'm doing things wrong ? My use case it that based on a user interaction in a client component I need to define a cookie

Thank you for your time
Answered by Dwarf Crocodile
Ok actually all good, the issue was not coming from the parameter I send but what I return, returning cookies.set is not a good idea... 🤦 Removing the return fixed it
View full answer

2 Replies

Avatar
Dwarf CrocodileOP
Image
Avatar
Dwarf CrocodileOP
Ok actually all good, the issue was not coming from the parameter I send but what I return, returning cookies.set is not a good idea... 🤦 Removing the return fixed it
Answer