| 4142 | } |
| 4143 | |
| 4144 | export async function getMiddlewareBundle({ |
| 4145 | entryPath, |
| 4146 | outputDirectory, |
| 4147 | routesManifest, |
| 4148 | isCorrectMiddlewareOrder, |
| 4149 | prerenderBypassToken, |
| 4150 | nextVersion, |
| 4151 | appPathRoutesManifest, |
| 4152 | }: { |
| 4153 | config: Config; |
| 4154 | entryPath: string; |
| 4155 | outputDirectory: string; |
| 4156 | prerenderBypassToken: string; |
| 4157 | routesManifest: RoutesManifest; |
| 4158 | isCorrectMiddlewareOrder: boolean; |
| 4159 | nextVersion: string; |
| 4160 | appPathRoutesManifest: Record<string, string>; |
| 4161 | }): Promise<{ |
| 4162 | staticRoutes: Route[]; |
| 4163 | dynamicRouteMap: ReadonlyMap<string, RouteWithSrc>; |
| 4164 | edgeFunctions: Record<string, EdgeFunction>; |
| 4165 | }> { |
| 4166 | const middlewareManifest = await getMiddlewareManifest( |
| 4167 | entryPath, |
| 4168 | outputDirectory |
| 4169 | ); |
| 4170 | const sortedFunctions = [ |
| 4171 | ...(!middlewareManifest |
| 4172 | ? [] |
| 4173 | : middlewareManifest.sortedMiddleware.map(key => ({ |
| 4174 | key, |
| 4175 | edgeFunction: middlewareManifest?.middleware[key], |
| 4176 | type: 'middleware' as const, |
| 4177 | }))), |
| 4178 | |
| 4179 | ...Object.entries(middlewareManifest?.functions ?? {}).map( |
| 4180 | ([key, edgeFunction]) => { |
| 4181 | return { |
| 4182 | key, |
| 4183 | edgeFunction, |
| 4184 | type: 'function' as const, |
| 4185 | }; |
| 4186 | } |
| 4187 | ), |
| 4188 | ]; |
| 4189 | |
| 4190 | if (middlewareManifest && sortedFunctions.length > 0) { |
| 4191 | const workerConfigs = await Promise.all( |
| 4192 | sortedFunctions.map(async ({ key, edgeFunction, type }) => { |
| 4193 | try { |
| 4194 | const wrappedModuleSource = await getNextjsEdgeFunctionSource( |
| 4195 | edgeFunction.files, |
| 4196 | { |
| 4197 | name: edgeFunction.name, |
| 4198 | staticRoutes: routesManifest.staticRoutes, |
| 4199 | dynamicRoutes: routesManifest.dynamicRoutes.filter( |
| 4200 | r => !('isMiddleware' in r) |
| 4201 | ), |