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

deprecation(vercel): vercel-edge-middleware.js file #10476

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/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.waitUntil()`.
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 = { ...context, ...${handlerTemplateCall} };
const { origin } = new URL(request.url);
const next = () =>
fetch(new URL('/${NODE_PATH}', request.url), {
const next = () => {
const { waitUntil, ...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
Loading