Skip to content

Commit

Permalink
deprecation(vercel): vercel-edge-middleware.js file (#10476)
Browse files Browse the repository at this point in the history
* deprecation(vercel): `vercel-edge-middleware.js` file

* add changeset

* `Astro.locals.waitUntil()` -> `Astro.locals.vercel.edge.waitUntil()`

* export edge locals interface
  • Loading branch information
lilnasy authored Mar 20, 2024
1 parent 8e4e554 commit cfbaa8a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-oranges-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/vercel": minor
---

The special-case handling of `src/vercel-edge-middleware.js` file is now deprecated. This file allowed you to access the edge runtime's `RequestContext` object, and create the middleware `locals` from its fields. However, this object includes only one field - the `waitUntil()` function - which is now available directly as `ctx.locals.vercel.edge.waitUntil()`.
9 changes: 4 additions & 5 deletions packages/integrations/vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"bugs": "https://github.com/withastro/astro/issues",
"homepage": "https://docs.astro.build/en/guides/integrations-guide/vercel/",
"exports": {
".": { "types": "./types.d.ts" },
"./serverless": "./dist/serverless/adapter.js",
"./serverless/entrypoint": "./dist/serverless/entrypoint.js",
"./static": "./dist/static/adapter.js",
Expand All @@ -28,9 +29,6 @@
},
"typesVersions": {
"*": {
"edge": [
"dist/edge/adapter.d.ts"
],
"serverless": [
"dist/serverless/adapter.d.ts"
],
Expand All @@ -40,7 +38,8 @@
}
},
"files": [
"dist"
"dist",
"types.d.ts"
],
"scripts": {
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
Expand All @@ -52,6 +51,7 @@
"dependencies": {
"@astrojs/internal-helpers": "workspace:*",
"@vercel/analytics": "^1.0.2",
"@vercel/edge": "^1.1.1",
"@vercel/nft": "^0.26.4",
"esbuild": "^0.19.6",
"fast-glob": "^3.3.2",
Expand All @@ -63,7 +63,6 @@
},
"devDependencies": {
"@types/set-cookie-parser": "^2.4.6",
"@vercel/edge": "^1.1.1",
"astro": "workspace:*",
"astro-scripts": "workspace:*",
"cheerio": "1.0.0-rc.12"
Expand Down
3 changes: 2 additions & 1 deletion packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ class VercelBuilder {
entry,
new URL(VERCEL_EDGE_MIDDLEWARE_FILE, this.config.srcDir),
new URL('./middleware.mjs', functionFolder),
middlewareSecret
middlewareSecret,
this.logger
);

await writeJson(new URL(`./.vc-config.json`, functionFolder), {
Expand Down
21 changes: 14 additions & 7 deletions packages/integrations/vercel/src/serverless/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ASTRO_PATH_HEADER,
NODE_PATH,
} from './adapter.js';
import type { AstroIntegrationLogger } from 'astro';

/**
* It generates the Vercel Edge Middleware file.
Expand All @@ -23,12 +24,14 @@ export async function generateEdgeMiddleware(
astroMiddlewareEntryPointPath: URL,
vercelEdgeMiddlewareHandlerPath: URL,
outPath: URL,
middlewareSecret: string
middlewareSecret: string,
logger: AstroIntegrationLogger
): Promise<URL> {
const code = edgeMiddlewareTemplate(
astroMiddlewareEntryPointPath,
vercelEdgeMiddlewareHandlerPath,
middlewareSecret
middlewareSecret,
logger
);
// https://vercel.com/docs/concepts/functions/edge-middleware#create-edge-middleware
const bundledFilePath = fileURLToPath(outPath);
Expand Down Expand Up @@ -64,7 +67,8 @@ export async function generateEdgeMiddleware(
function edgeMiddlewareTemplate(
astroMiddlewareEntryPointPath: URL,
vercelEdgeMiddlewareHandlerPath: URL,
middlewareSecret: string
middlewareSecret: string,
logger: AstroIntegrationLogger
) {
const middlewarePath = JSON.stringify(
fileURLToPath(astroMiddlewareEntryPointPath).replace(/\\/g, '/')
Expand All @@ -73,6 +77,7 @@ function edgeMiddlewareTemplate(
let handlerTemplateImport = '';
let handlerTemplateCall = '{}';
if (existsSync(filePathEdgeMiddleware + '.js') || existsSync(filePathEdgeMiddleware + '.ts')) {
logger.warn('Usage of `vercel-edge-middleware.js` is deprecated. You can now use the `waitUntil(promise)` function directly as `ctx.locals.waitUntil(promise)`.')
const stringified = JSON.stringify(filePathEdgeMiddleware.replace(/\\/g, '/'));
handlerTemplateImport = `import handler from ${stringified}`;
handlerTemplateCall = `await handler({ request, context })`;
Expand All @@ -87,17 +92,19 @@ export default async function middleware(request, context) {
request,
params: {}
});
ctx.locals = ${handlerTemplateCall};
ctx.locals = { vercel: { edge: context }, ...${handlerTemplateCall} };
const { origin } = new URL(request.url);
const next = () =>
fetch(new URL('/${NODE_PATH}', request.url), {
const next = () => {
const { vercel, ...locals } = ctx.locals;
return 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(ctx.locals)
'${ASTRO_LOCALS_HEADER}': trySerializeLocals(locals)
}
})
}
return onRequest(ctx, next);
}`;
Expand Down
5 changes: 5 additions & 0 deletions packages/integrations/vercel/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface EdgeLocals {
vercel: {
edge: import("@vercel/edge").RequestContext;
}
}
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cfbaa8a

Please sign in to comment.