Skip to content

Commit

Permalink
Merge cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Jun 18, 2024
1 parent e47e3f5 commit 7253dcd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
5 changes: 1 addition & 4 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,7 @@ export class App {
for (const setCookieHeaderValue of App.getSetCookieFromResponse(response)) {
response.headers.append('set-cookie', setCookieHeaderValue);
}
} else {
// It may have been set already in a rewrite
response.headers.delete('set-cookie');
}
}

Reflect.set(response, responseSentSymbol, true);
return response;
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/src/core/cookies/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ class AstroCookies implements AstroCookiesInterface {
}
}

/**
* Merges a new AstroCookies instance into the current instance. Any new cookies
* will be added to the current instance, overwriting any existing cookies with the same name.
*/
merge(cookies: AstroCookies) {
const outgoing = cookies.#outgoing;
if (outgoing) {
for (const [key, value] of outgoing) {
this.#ensureOutgoingMap().set(key, value);
}
}
}

/**
* Astro.cookies.header() returns an iterator for the cookies that have previously
* been set by either Astro.cookies.set() or Astro.cookies.delete().
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/cookies/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function responseHasCookies(response: Response): boolean {
return Reflect.has(response, astroCookiesSymbol);
}

function getFromResponse(response: Response): AstroCookies | undefined {
export function getFromResponse(response: Response): AstroCookies | undefined {
let cookies = Reflect.get(response, astroCookiesSymbol);
if (cookies != null) {
return cookies as AstroCookies;
Expand Down
9 changes: 5 additions & 4 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
responseSentSymbol,
} from './constants.js';
import { AstroCookies, attachCookiesToResponse } from './cookies/index.js';
import { getFromResponse } from './cookies/response.js';
import { AstroError, AstroErrorData } from './errors/index.js';
import { callMiddleware } from './middleware/callMiddleware.js';
import { sequence } from './middleware/index.js';
Expand Down Expand Up @@ -164,11 +165,11 @@ export class RenderContext {
this.routeData
);

if (result.cookies) {
for (const cookie of AstroCookies.consume(result.cookies)) {
response.headers.append('Set-Cookie', cookie);
}
const responseCookies = getFromResponse(response)
if (result.cookies && responseCookies) {
result.cookies?.merge(responseCookies);
}

} catch (e) {
// If there is an error in the page's frontmatter or instantiation of the RenderTemplate fails midway,
// we signal to the rest of the internals that we can ignore the results of existing renders and avoid kicking off more of them.
Expand Down

0 comments on commit 7253dcd

Please sign in to comment.