diff --git a/.changeset/small-ravens-cover.md b/.changeset/small-ravens-cover.md new file mode 100644 index 000000000..67b8e31f0 --- /dev/null +++ b/.changeset/small-ravens-cover.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Allow setting domain when deleting cookies diff --git a/packages/astro/src/core/cookies/cookies.ts b/packages/astro/src/core/cookies/cookies.ts index f999db8a1..73996f51f 100644 --- a/packages/astro/src/core/cookies/cookies.ts +++ b/packages/astro/src/core/cookies/cookies.ts @@ -11,9 +11,7 @@ interface AstroCookieSetOptions { secure?: boolean; } -interface AstroCookieDeleteOptions { - path?: string; -} +type AstroCookieDeleteOptions = Pick; interface AstroCookieInterface { value: string | undefined; @@ -75,6 +73,9 @@ class AstroCookies implements AstroCookiesInterface { expires: DELETED_EXPIRATION, }; + if (options?.domain) { + serializeOptions.domain = options.domain; + } if (options?.path) { serializeOptions.path = options.path; } diff --git a/packages/astro/test/units/cookies/delete.test.js b/packages/astro/test/units/cookies/delete.test.js index 571a3e40d..67fa1306b 100644 --- a/packages/astro/test/units/cookies/delete.test.js +++ b/packages/astro/test/units/cookies/delete.test.js @@ -56,5 +56,16 @@ describe('astro/src/core/cookies', () => { expect(headers).to.have.a.lengthOf(1); expect(headers[0]).to.match(/Path=\/subpath\//); }); + + it('can provide a domain', () => { + let req = new Request('http://example.com/'); + let cookies = new AstroCookies(req); + cookies.delete('foo', { + domain: '.example.com', + }); + let headers = Array.from(cookies.headers()); + expect(headers).to.have.a.lengthOf(1); + expect(headers[0]).to.match(/Domain=\.example\.com/); + }); }); });