From 0c58c4e1d8da490144be0db9b00a6470351ee66b Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Sun, 4 Jun 2023 19:57:55 -0400 Subject: [PATCH 1/2] Fixes cookies being set by middleware --- packages/astro/src/core/render/context.ts | 2 ++ packages/astro/src/core/render/core.ts | 1 + packages/astro/src/core/render/result.ts | 3 ++- .../astro/test/fixtures/middleware-dev/src/middleware.js | 4 ++++ packages/astro/test/middleware.test.js | 5 +++++ 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/core/render/context.ts b/packages/astro/src/core/render/context.ts index d4efe35df3b7..90aaae0d2029 100644 --- a/packages/astro/src/core/render/context.ts +++ b/packages/astro/src/core/render/context.ts @@ -1,4 +1,5 @@ import type { + AstroCookies, ComponentInstance, Params, Props, @@ -23,6 +24,7 @@ export interface RenderContext { componentMetadata?: SSRResult['componentMetadata']; route?: RouteData; status?: number; + cookies?: AstroCookies; params: Params; props: Props; } diff --git a/packages/astro/src/core/render/core.ts b/packages/astro/src/core/render/core.ts index 24bec9d30e29..f5282f59eee9 100644 --- a/packages/astro/src/core/render/core.ts +++ b/packages/astro/src/core/render/core.ts @@ -138,6 +138,7 @@ export async function renderPage({ mod, renderContext, env, apiContext }: Render scripts: renderContext.scripts, ssr: env.ssr, status: renderContext.status ?? 200, + cookies: apiContext?.cookies, locals, }); diff --git a/packages/astro/src/core/render/result.ts b/packages/astro/src/core/render/result.ts index ede30256e116..41a2fb9a44f0 100644 --- a/packages/astro/src/core/render/result.ts +++ b/packages/astro/src/core/render/result.ts @@ -53,6 +53,7 @@ export interface CreateResultArgs { request: Request; status: number; locals: App.Locals; + cookies?: AstroCookies; } function getFunctionExpression(slot: any) { @@ -155,7 +156,7 @@ export function createResult(args: CreateResultArgs): SSRResult { }); // Astro.cookies is defined lazily to avoid the cost on pages that do not use it. - let cookies: AstroCookies | undefined = undefined; + let cookies: AstroCookies | undefined = args.cookies; let componentMetadata = args.componentMetadata ?? new Map(); // Create the result object that will be passed into the render function. diff --git a/packages/astro/test/fixtures/middleware-dev/src/middleware.js b/packages/astro/test/fixtures/middleware-dev/src/middleware.js index c8b440239e19..2cc7cf2ca5e0 100644 --- a/packages/astro/test/fixtures/middleware-dev/src/middleware.js +++ b/packages/astro/test/fixtures/middleware-dev/src/middleware.js @@ -19,6 +19,10 @@ const first = defineMiddleware(async (context, next) => { headers: response.headers, }); } else { + if(context.url.pathname === '/') { + context.cookies.set('foo', 'bar'); + } + context.locals.name = 'bar'; } return await next(); diff --git a/packages/astro/test/middleware.test.js b/packages/astro/test/middleware.test.js index 270666678ec1..432e6b9833d1 100644 --- a/packages/astro/test/middleware.test.js +++ b/packages/astro/test/middleware.test.js @@ -66,6 +66,11 @@ describe('Middleware in DEV mode', () => { let $ = cheerio.load(html); expect($('title').html()).to.equal('MiddlewareNoDataOrNextCalled'); }); + + it('should allow setting cookies', async () => { + let res = await fixture.fetch('/'); + expect(res.headers.get('set-cookie')).to.equal('foo=bar'); + }); }); describe('Middleware in PROD mode, SSG', () => { From 78f6318bcecac1b9830dd36ae37acc1747b6877d Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Sun, 4 Jun 2023 19:59:26 -0400 Subject: [PATCH 2/2] Adding a changeset --- .changeset/lazy-falcons-divide.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/lazy-falcons-divide.md diff --git a/.changeset/lazy-falcons-divide.md b/.changeset/lazy-falcons-divide.md new file mode 100644 index 000000000000..a705a78d46d4 --- /dev/null +++ b/.changeset/lazy-falcons-divide.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix cookies not being set by middleware