-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
'astro': minor | ||
'@astrojs/cloudflare': minor | ||
'@astrojs/deno': minor | ||
'@astrojs/netlify': minor | ||
'@astrojs/node': minor | ||
'@astrojs/vercel': minor | ||
--- | ||
|
||
Adds the Astro.cookies API | ||
|
||
`Astro.cookies` is a new API for manipulating cookies in Astro components and API routes. | ||
|
||
In Astro components, the new `Astro.cookies` object is a map-like object that allows you to get, set, delete, and check for a cookie's existence (`has`): | ||
|
||
```astro | ||
--- | ||
type Prefs = { | ||
darkMode: boolean; | ||
} | ||
Astro.cookies.set<Prefs>('prefs', { darkMode: true }, { | ||
expires: '1 month' | ||
}); | ||
const prefs = Astro.cookies.get<Prefs>('prefs').json(); | ||
--- | ||
<body data-theme={prefs.darkMode ? 'dark' : 'light'}> | ||
``` | ||
|
||
Once you've set a cookie with Astro.cookies it will automatically be included in the outgoing response. | ||
|
||
This API is also available with the same functionality in API routes: | ||
|
||
```js | ||
export function post({ cookies }) { | ||
cookies.set('loggedIn', false); | ||
|
||
return new Response(null, { | ||
status: 302, | ||
headers: { | ||
Location: '/login' | ||
} | ||
}); | ||
} | ||
``` | ||
|
||
See [the RFC](https://github.com/withastro/rfcs/blob/main/proposals/0025-cookie-management.md) to learn more. |