Next.js Discord

Discord Forum

Is cookies.delete() supposed to work from "next/headers"?

Unanswered
Giant wood wasp posted this in #help-forum
Open in Discord
Giant wood waspOP
I am making API endpoints with NextJS and am having trouble understanding whats the best or recommended approach to handling cookies, both getting and setting HTTP only cookies

https://nextjs.org/docs/app/building-your-application/routing/router-handlers#cookies

The docs say you can use import { cookies } from 'next/headers' from https://nextjs.org/docs/app/api-reference/functions/cookies. This works well, but it seems like the documentation is not up to date or I am potentially using something that could be seen as wrong in future updates.

It says "Deleting cookies: To "delete" a cookie, you must set a new cookie with the same name and an empty value. You can also set the maxAge to 0 to expire the cookie immediately.", however I am able to do cookies.delete("cookie-name") and it works perfectly fine. Is this expected?

4 Replies

Giant wood waspOP
Ah that solved my issue. It seems that the cookies from next/headers is pretty janky, like I mentioned, cookies().delete("cookie-name") works even though in the documentation theres no mention of it, and using cookies().has("cookie-name") like the documentation says I can gives me an error of TypeError: (0 , next_headers__WEBPACK_IMPORTED_MODULE_2__.cookies)(...).has is not a function

Its all over the place it seems
Kawakawa
Yeah. Just set the maxage to 0 :)