#context.cookies undefined

2 messages · Page 1 of 1 (latest)

grand pebbleBOT
#

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

#

Based on the information provided in the extracts, it seems like you're trying to access cookies in your Astro application.

Astro provides a utility to read and modify cookies through Astro.cookies or context.cookies in server-side rendering mode. You can use methods like get, set, has, and delete to manipulate cookies.

Here's an example of how you can use it:

let counter = 0
if (Astro.cookies.has("counter")) { 
  const cookie = Astro.cookies.get("counter") 
  counter = cookie.number() + 1
}
Astro.cookies.set("counter", counter)

(Source: Astro Documentation (https://docs.astro.build/en/guides/server-side-rendering/))

However, please note that cookies are not available in static site generation (SSG) mode. If you're using SSG, you might see a warning like this:

[ssg] Headers are not exposed in static (SSG) output mode. To enable headers: set `output: "server"` in your config file.

(Source: GitHub Issue (https://github.com/withastro/astro/issues/6499))

If you're still having issues, it might be due to a bug or a specific configuration in your project. In that case, I would recommend checking the official Astro documentation or their GitHub issues for more information.

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