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

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

Merged
merged 1 commit into from
Jun 11, 2024
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/shaggy-camels-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---

In Vercel Edge, include cookies set by Astro.cookies.set
2 changes: 1 addition & 1 deletion packages/integrations/vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 17 additions & 6 deletions packages/integrations/vercel/src/serverless/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
*/
export async function generateEdgeMiddleware(
astroMiddlewareEntryPointPath: URL,
root: URL,
vercelEdgeMiddlewareHandlerPath: URL,
outPath: URL,
middlewareSecret: string,
Expand All @@ -39,7 +40,7 @@ export async function generateEdgeMiddleware(
await esbuild.build({
stdin: {
contents: code,
resolveDir: process.cwd(),
resolveDir: fileURLToPath(root),
},
target: 'es2020',
platform: 'browser',
Expand Down Expand Up @@ -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;
}`;
}
9 changes: 9 additions & 0 deletions packages/integrations/vercel/test/edge-middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
export const onRequest = async (context, next) => {
const test = 'something';
context.cookies.set('foo', 'bar');
const response = await next();
return response;
};
Loading