mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-10 04:22:12 +00:00
[next] use middleware manifest worker environments (#11390)
This PR adds changes to the Next.js builder to pass the per-worker environment emitted from the Next.js middleware manifest to the created `EdgeFunction` resource.
This commit is contained in:
5
.changeset/wicked-pillows-notice.md
Normal file
5
.changeset/wicked-pillows-notice.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'@vercel/next': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Add support for edge function environment variables
|
||||||
@@ -2753,7 +2753,10 @@ export type FunctionsConfigManifestV1 = {
|
|||||||
>;
|
>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type MiddlewareManifest = MiddlewareManifestV1 | MiddlewareManifestV2;
|
type MiddlewareManifest =
|
||||||
|
| MiddlewareManifestV1
|
||||||
|
| MiddlewareManifestV2
|
||||||
|
| MiddlewareManifestV3;
|
||||||
|
|
||||||
interface MiddlewareManifestV1 {
|
interface MiddlewareManifestV1 {
|
||||||
version: 1;
|
version: 1;
|
||||||
@@ -2769,6 +2772,13 @@ interface MiddlewareManifestV2 {
|
|||||||
functions?: { [page: string]: EdgeFunctionInfoV2 };
|
functions?: { [page: string]: EdgeFunctionInfoV2 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface MiddlewareManifestV3 {
|
||||||
|
version: 3;
|
||||||
|
sortedMiddleware: string[];
|
||||||
|
middleware: { [page: string]: EdgeFunctionInfoV3 };
|
||||||
|
functions?: { [page: string]: EdgeFunctionInfoV3 };
|
||||||
|
}
|
||||||
|
|
||||||
type Regions = 'home' | 'global' | 'auto' | string[] | 'all' | 'default';
|
type Regions = 'home' | 'global' | 'auto' | string[] | 'all' | 'default';
|
||||||
|
|
||||||
interface BaseEdgeFunctionInfo {
|
interface BaseEdgeFunctionInfo {
|
||||||
@@ -2788,6 +2798,11 @@ interface EdgeFunctionInfoV2 extends BaseEdgeFunctionInfo {
|
|||||||
matchers: EdgeFunctionMatcher[];
|
matchers: EdgeFunctionMatcher[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface EdgeFunctionInfoV3 extends BaseEdgeFunctionInfo {
|
||||||
|
matchers: EdgeFunctionMatcher[];
|
||||||
|
environments: Record<string, string>;
|
||||||
|
}
|
||||||
|
|
||||||
interface EdgeFunctionMatcher {
|
interface EdgeFunctionMatcher {
|
||||||
regexp: string;
|
regexp: string;
|
||||||
has?: HasField;
|
has?: HasField;
|
||||||
@@ -3020,6 +3035,7 @@ export async function getMiddlewareBundle({
|
|||||||
slug: 'nextjs',
|
slug: 'nextjs',
|
||||||
version: nextVersion,
|
version: nextVersion,
|
||||||
},
|
},
|
||||||
|
environment: edgeFunction.environments,
|
||||||
});
|
});
|
||||||
})(),
|
})(),
|
||||||
routeMatchers: getRouteMatchers(edgeFunction, routesManifest),
|
routeMatchers: getRouteMatchers(edgeFunction, routesManifest),
|
||||||
@@ -3160,7 +3176,7 @@ export async function getFunctionsConfigManifest(
|
|||||||
export async function getMiddlewareManifest(
|
export async function getMiddlewareManifest(
|
||||||
entryPath: string,
|
entryPath: string,
|
||||||
outputDirectory: string
|
outputDirectory: string
|
||||||
): Promise<MiddlewareManifestV2 | undefined> {
|
): Promise<MiddlewareManifestV3 | undefined> {
|
||||||
const middlewareManifestPath = path.join(
|
const middlewareManifestPath = path.join(
|
||||||
entryPath,
|
entryPath,
|
||||||
outputDirectory,
|
outputDirectory,
|
||||||
@@ -3179,19 +3195,27 @@ export async function getMiddlewareManifest(
|
|||||||
const manifest = (await fs.readJSON(
|
const manifest = (await fs.readJSON(
|
||||||
middlewareManifestPath
|
middlewareManifestPath
|
||||||
)) as MiddlewareManifest;
|
)) as MiddlewareManifest;
|
||||||
return manifest.version === 1
|
|
||||||
? upgradeMiddlewareManifest(manifest)
|
if (manifest.version === 1) {
|
||||||
: manifest;
|
return upgradeMiddlewareManifestV1(manifest);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (manifest.version === 2) {
|
||||||
|
return upgradeMiddlewareManifestV2(manifest);
|
||||||
|
}
|
||||||
|
|
||||||
|
return manifest;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function upgradeMiddlewareManifest(
|
export function upgradeMiddlewareManifestV1(
|
||||||
v1: MiddlewareManifestV1
|
v1: MiddlewareManifestV1
|
||||||
): MiddlewareManifestV2 {
|
): MiddlewareManifestV3 {
|
||||||
function updateInfo(v1Info: EdgeFunctionInfoV1): EdgeFunctionInfoV2 {
|
function updateInfo(v1Info: EdgeFunctionInfoV1): EdgeFunctionInfoV3 {
|
||||||
const { regexp, ...rest } = v1Info;
|
const { regexp, ...rest } = v1Info;
|
||||||
return {
|
return {
|
||||||
...rest,
|
...rest,
|
||||||
matchers: [{ regexp }],
|
matchers: [{ regexp }],
|
||||||
|
environments: {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3206,7 +3230,35 @@ export function upgradeMiddlewareManifest(
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
...v1,
|
...v1,
|
||||||
version: 2,
|
version: 3,
|
||||||
|
middleware,
|
||||||
|
functions,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function upgradeMiddlewareManifestV2(
|
||||||
|
v2: MiddlewareManifestV2
|
||||||
|
): MiddlewareManifestV3 {
|
||||||
|
function updateInfo(v2Info: EdgeFunctionInfoV2): EdgeFunctionInfoV3 {
|
||||||
|
const { ...rest } = v2Info;
|
||||||
|
return {
|
||||||
|
...rest,
|
||||||
|
environments: {},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const middleware = Object.fromEntries(
|
||||||
|
Object.entries(v2.middleware).map(([p, info]) => [p, updateInfo(info)])
|
||||||
|
);
|
||||||
|
const functions = v2.functions
|
||||||
|
? Object.fromEntries(
|
||||||
|
Object.entries(v2.functions).map(([p, info]) => [p, updateInfo(info)])
|
||||||
|
)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...v2,
|
||||||
|
version: 3,
|
||||||
middleware,
|
middleware,
|
||||||
functions,
|
functions,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user