Fixes cookies being set by middleware (#7294)
* Fixes cookies being set by middleware * Adding a changeset
This commit is contained in:
parent
af9ac3360b
commit
dd1a6b6c94
6 changed files with 19 additions and 1 deletions
5
.changeset/lazy-falcons-divide.md
Normal file
5
.changeset/lazy-falcons-divide.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix cookies not being set by middleware
|
|
@ -1,4 +1,5 @@
|
||||||
import type {
|
import type {
|
||||||
|
AstroCookies,
|
||||||
ComponentInstance,
|
ComponentInstance,
|
||||||
Params,
|
Params,
|
||||||
Props,
|
Props,
|
||||||
|
@ -23,6 +24,7 @@ export interface RenderContext {
|
||||||
componentMetadata?: SSRResult['componentMetadata'];
|
componentMetadata?: SSRResult['componentMetadata'];
|
||||||
route?: RouteData;
|
route?: RouteData;
|
||||||
status?: number;
|
status?: number;
|
||||||
|
cookies?: AstroCookies;
|
||||||
params: Params;
|
params: Params;
|
||||||
props: Props;
|
props: Props;
|
||||||
}
|
}
|
||||||
|
|
|
@ -145,6 +145,7 @@ export async function renderPage({
|
||||||
scripts: renderContext.scripts,
|
scripts: renderContext.scripts,
|
||||||
ssr: env.ssr,
|
ssr: env.ssr,
|
||||||
status: renderContext.status ?? 200,
|
status: renderContext.status ?? 200,
|
||||||
|
cookies: apiContext?.cookies,
|
||||||
locals,
|
locals,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,7 @@ export interface CreateResultArgs {
|
||||||
request: Request;
|
request: Request;
|
||||||
status: number;
|
status: number;
|
||||||
locals: App.Locals;
|
locals: App.Locals;
|
||||||
|
cookies?: AstroCookies;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFunctionExpression(slot: any) {
|
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.
|
// 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();
|
let componentMetadata = args.componentMetadata ?? new Map();
|
||||||
|
|
||||||
// Create the result object that will be passed into the render function.
|
// Create the result object that will be passed into the render function.
|
||||||
|
|
|
@ -19,6 +19,10 @@ const first = defineMiddleware(async (context, next) => {
|
||||||
headers: response.headers,
|
headers: response.headers,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
if(context.url.pathname === '/') {
|
||||||
|
context.cookies.set('foo', 'bar');
|
||||||
|
}
|
||||||
|
|
||||||
context.locals.name = 'bar';
|
context.locals.name = 'bar';
|
||||||
}
|
}
|
||||||
return await next();
|
return await next();
|
||||||
|
|
|
@ -66,6 +66,11 @@ describe('Middleware in DEV mode', () => {
|
||||||
let $ = cheerio.load(html);
|
let $ = cheerio.load(html);
|
||||||
expect($('title').html()).to.equal('MiddlewareNoDataOrNextCalled');
|
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', () => {
|
describe('Middleware in PROD mode, SSG', () => {
|
||||||
|
|
Loading…
Reference in a new issue