In Vercel Edge, include cookies set by Astro.cookies.set (#11227)

This commit is contained in:
Matthew Phillips 2024-06-11 09:32:17 -04:00 committed by GitHub
parent dd0145d28b
commit 24ce8983e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 34 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---
In Vercel Edge, include cookies set by Astro.cookies.set

View file

@ -47,7 +47,7 @@
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"dev": "astro-scripts dev \"src/**/*.ts\"",
"test": "astro-scripts test --timeout 50000 \"test/**/!(hosted|edge-middleware).test.js\"",
"test": "astro-scripts test --timeout 50000 \"test/**/!(hosted).test.js\"",
"test:hosted": "astro-scripts test --timeout 30000 \"test/hosted/*.test.js\""
},
"dependencies": {

View file

@ -508,6 +508,7 @@ class VercelBuilder {
await generateEdgeMiddleware(
entry,
this.config.root,
new URL(VERCEL_EDGE_MIDDLEWARE_FILE, this.config.srcDir),
new URL('./middleware.mjs', functionFolder),
middlewareSecret,

View file

@ -22,6 +22,7 @@ import {
*/
export async function generateEdgeMiddleware(
astroMiddlewareEntryPointPath: URL,
root: URL,
vercelEdgeMiddlewareHandlerPath: URL,
outPath: URL,
middlewareSecret: string,
@ -39,7 +40,7 @@ export async function generateEdgeMiddleware(
await esbuild.build({
stdin: {
contents: code,
resolveDir: process.cwd(),
resolveDir: fileURLToPath(root),
},
target: 'es2020',
platform: 'browser',
@ -96,18 +97,28 @@ export default async function middleware(request, context) {
});
ctx.locals = { vercel: { edge: context }, ...${handlerTemplateCall} };
const { origin } = new URL(request.url);
const next = () => {
const next = async () => {
const { vercel, ...locals } = ctx.locals;
return fetch(new URL('/${NODE_PATH}', request.url), {
const response = await fetch(new URL('/${NODE_PATH}', request.url), {
headers: {
...Object.fromEntries(request.headers.entries()),
'${ASTRO_MIDDLEWARE_SECRET_HEADER}': '${middlewareSecret}',
'${ASTRO_PATH_HEADER}': request.url.replace(origin, ''),
'${ASTRO_LOCALS_HEADER}': trySerializeLocals(locals)
}
})
}
});
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
};
return onRequest(ctx, next);
const response = await onRequest(ctx, next);
// Append cookies from Astro.cookies
for(const setCookieHeaderValue of ctx.cookies.headers()) {
response.headers.append('set-cookie', setCookieHeaderValue);
}
return response;
}`;
}

View file

@ -30,6 +30,15 @@ describe('Vercel edge middleware', () => {
);
});
it('edge sets Set-Cookie headers', async () => {
let entry = new URL('../.vercel/output/functions/_middleware.func/middleware.mjs', build.config.outDir);
const module = await import(entry);
const request = new Request('http://example.com/foo');
const response = await module.default(request, {});
assert.equal(response.headers.get('set-cookie'), 'foo=bar');
assert.ok((await response.text()).length, 'Body is included');
});
// TODO: The path here seems to be inconsistent?
it.skip('with edge handle file, should successfully build the middleware', async () => {
const fixture = await loadFixture({

View file

@ -3,6 +3,7 @@
*/
export const onRequest = async (context, next) => {
const test = 'something';
context.cookies.set('foo', 'bar');
const response = await next();
return response;
};