Skip to content

Commit 08e0fd7

Browse files
Support partitioned cookies (#15063)
* Allow creation of partitioned cookies * Add changeset * Update changelog to minor * Update changeset with detailed description and example --------- Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
1 parent 54f6006 commit 08e0fd7

5 files changed

Lines changed: 37 additions & 1 deletion

File tree

.changeset/nine-olives-drop.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Adds a new `partitioned` option when setting a cookie to allow creating partitioned cookies.
6+
7+
[Partitioned cookies](https://developer.mozilla.org/en-US/docs/Web/Privacy/Guides/Privacy_sandbox/Partitioned_cookies) can only be read within the context of the top-level site on which they were set. This allows cross-site tracking to be blocked, while still enabling legitimate uses of third-party cookies.
8+
9+
You can create a partitioned cookie by passing `partitioned: true` when setting a cookie. Note that partitioned cookies must also be set with `secure: true`:
10+
11+
```js
12+
Astro.cookies.set('my-cookie', 'value', {
13+
partitioned: true,
14+
secure: true,
15+
});
16+
```
17+
18+
For more information, see the [`AstroCookieSetOptions` API reference](https://docs.astro.build/en/reference/api-reference/#astrocookiesetoptions).

packages/astro/src/core/config/schemas/base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ export const AstroConfigSchema = z.object({
463463
maxAge: z.number().optional(),
464464
sameSite: z.union([z.enum(['strict', 'lax', 'none']), z.boolean()]).optional(),
465465
secure: z.boolean().optional(),
466+
partitioned: z.boolean().optional(),
466467
})
467468
.or(z.string())
468469
.transform((val) => {

packages/astro/src/core/cookies/cookies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AstroError, AstroErrorData } from '../errors/index.js';
44

55
export type AstroCookieSetOptions = Pick<
66
SerializeOptions,
7-
'domain' | 'path' | 'expires' | 'maxAge' | 'httpOnly' | 'sameSite' | 'secure' | 'encode'
7+
'domain' | 'path' | 'expires' | 'maxAge' | 'httpOnly' | 'sameSite' | 'secure' | 'encode' | 'partitioned'
88
>;
99

1010
export interface AstroCookieGetOptions {

packages/integrations/netlify/test/functions/cookies.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ describe(
2727
assert.deepEqual(resp.headers.getSetCookie(), ['foo=foo; HttpOnly', 'bar=bar; HttpOnly']);
2828
});
2929

30+
it('Can set partitioned cookie', async () => {
31+
const entryURL = new URL(
32+
'./fixtures/cookies/.netlify/v1/functions/ssr/ssr.mjs',
33+
import.meta.url,
34+
);
35+
const { default: handler } = await import(entryURL);
36+
const resp = await handler(new Request('http://example.com/partitioned'), {});
37+
assert.equal(resp.status, 200);
38+
const cookie = resp.headers.getSetCookie()[0];
39+
assert.ok(cookie.includes('Partitioned'), 'Cookie should include Partitioned attribute');
40+
});
41+
3042
it('renders dynamic 404 page', async () => {
3143
const entryURL = new URL(
3244
'./fixtures/cookies/.netlify/v1/functions/ssr/ssr.mjs',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
export function GET({ cookies }) {
3+
cookies.set('partitioned-cookie', 'value', { partitioned: true, secure: true });
4+
return new Response('ok');
5+
}

0 commit comments

Comments
 (0)