#Cookies

1 messages · Page 1 of 1 (latest)

quaint pebble
#

import { cookies } from "next/headers";

const NAME = "sidebar-collapsed";

export interface SidebarCookie {
isCollapsed: boolean;
}

export const sidebarCookie = {
async get(): Promise<SidebarCookie> {
const cookiesStore = await cookies();
const cookie = cookiesStore.get(NAME);
const isCollapsed = cookie?.value === "true";

return { isCollapsed };

},
async set(value: SidebarCookie) {
const cookiesStore = await cookies();
cookiesStore.set(NAME, String(value.isCollapsed));
},
};

how come i can canm't find cookies?
module doesn't have exported member cookies

brittle pantherBOT
#

Hi @quaint pebble. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

It seems you're encountering an issue with importing the cookies function from "next/headers". Based on the information provided in the knowledge sources, I can help clarify this for you.

The cookies function is indeed part of Next.js, but it's important to note that it's only available in certain contexts. Here are a few key points to consider:

  1. The cookies function is intended for use in Server Components (https://nextjs.org/docs/app/building-your-application/rendering/server-components) or Server Actions (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations) Source (https://nextjs.org/docs/app/api-reference/functions/cookies).

  2. It's a Dynamic Function, which means it can't be used in static contexts Source (https://nextjs.org/docs/app/api-reference/functions/cookies).

  3. The cookies function is part of the App Router in Next.js. If you're using the Pages Router, this function won't be available.

If you're still encountering the "module doesn't have exported member cookies" error, here are a few things to check:

  1. Ensure you're using the App Router and not the Pages Router.
  2. Make sure you're using this in a server component or server action.
  3. Verify that you're using a recent version of Next.js that includes this feature.

If you're certain that you're in the correct context and still can't access the cookies function, you might want to check your Next.js version and consider updating to the latest version.

For client-side cookie handling, you might need to use a different approach. The knowledge sources provide an example of a client-side cookie handling function:

#
export function getCookie(name: string) {
  if (typeof document === "undefined") return
  
  const value = "; " + document.cookie
  const decodedValue = decodeURIComponent(value)
  const parts = decodedValue.split("; " + name + "=")
  
  if (parts.length === 2) {
    return parts.pop()?.split(";").shift()
  }
}

Source (https://github.com/vercel/next.js/issues/49757)

Remember, the server-side cookies function and client-side cookie handling are different and should be used in their appropriate contexts.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: