Skip to content

Commit

Permalink
fix(vercel): update middleware esbuild settings (#519)
Browse files Browse the repository at this point in the history
* fix(vercel): update middleware esbuild settings

* Update target
  • Loading branch information
ascorbic authored Jan 23, 2025
1 parent e4d41ce commit 641d7d5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-phones-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---

Updates edge middleware to support esnext syntax
8 changes: 6 additions & 2 deletions packages/vercel/src/serverless/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ export async function generateEdgeMiddleware(
contents: code,
resolveDir: fileURLToPath(root),
},
target: 'es2020',
// Vercel Edge runtime targets ESNext, because Cloudflare Workers update v8 weekly
// https://github.com/vercel/vercel/blob/1006f2ae9d67ea4b3cbb1073e79d14d063d42436/packages/next/scripts/build-edge-function-template.js
target: 'esnext',
platform: 'browser',
// esbuild automatically adds the browser, import and default conditions
// https://esbuild.github.io/api/#conditions
// https://runtime-keys.proposal.wintercg.org/#edge-light
conditions: ['edge-light', 'worker', 'browser'],
conditions: ['edge-light', 'workerd', 'worker'],
outfile: bundledFilePath,
allowOverwrite: true,
format: 'esm',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export const hello = async () => 'hello world';

const message = await hello();

/**
* @type {import("astro").MiddlewareResponseHandler}
*/
export const onRequest = async (context, next) => {
const test = 'something';
context.cookies.set('foo', 'bar');
const response = await next();
response.headers.set('x-message', message);
return response;
};

0 comments on commit 641d7d5

Please sign in to comment.