Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes cookies being set by middleware #7294

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-falcons-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix cookies not being set by middleware
2 changes: 2 additions & 0 deletions packages/astro/src/core/render/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
AstroCookies,
ComponentInstance,
Params,
Props,
Expand All @@ -23,6 +24,7 @@ export interface RenderContext {
componentMetadata?: SSRResult['componentMetadata'];
route?: RouteData;
status?: number;
cookies?: AstroCookies;
params: Params;
props: Props;
}
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/render/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface CreateResultArgs {
request: Request;
status: number;
locals: App.Locals;
cookies?: AstroCookies;
}

function getFunctionExpression(slot: any) {
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions packages/astro/test/fixtures/middleware-dev/src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down