Skip to content

Commit 6b156dd

Browse files
Allow setting domain when deleting cookies (#5341)
* Allow setting domain when deleting cookies * Add delete cookie with domain tests * Fix syntax for AstroCookieDeleteOptions type * Add changeset * Update to a minor change rather than a patch Co-authored-by: Matthew Phillips <matthew@skypack.dev>
1 parent dced4a8 commit 6b156dd

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

.changeset/small-ravens-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Allow setting domain when deleting cookies

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ interface AstroCookieSetOptions {
1111
secure?: boolean;
1212
}
1313

14-
interface AstroCookieDeleteOptions {
15-
path?: string;
16-
}
14+
type AstroCookieDeleteOptions = Pick<AstroCookieSetOptions, 'domain' | 'path'>;
1715

1816
interface AstroCookieInterface {
1917
value: string | undefined;
@@ -75,6 +73,9 @@ class AstroCookies implements AstroCookiesInterface {
7573
expires: DELETED_EXPIRATION,
7674
};
7775

76+
if (options?.domain) {
77+
serializeOptions.domain = options.domain;
78+
}
7879
if (options?.path) {
7980
serializeOptions.path = options.path;
8081
}

packages/astro/test/units/cookies/delete.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,16 @@ describe('astro/src/core/cookies', () => {
5656
expect(headers).to.have.a.lengthOf(1);
5757
expect(headers[0]).to.match(/Path=\/subpath\//);
5858
});
59+
60+
it('can provide a domain', () => {
61+
let req = new Request('http://example.com/');
62+
let cookies = new AstroCookies(req);
63+
cookies.delete('foo', {
64+
domain: '.example.com',
65+
});
66+
let headers = Array.from(cookies.headers());
67+
expect(headers).to.have.a.lengthOf(1);
68+
expect(headers[0]).to.match(/Domain=\.example\.com/);
69+
});
5970
});
6071
});

0 commit comments

Comments
 (0)