@@ -21,6 +21,7 @@ import { prerelease, satisfies, lt as semverLowerThan, lte as semverLowerThanOrE
21
21
import type { RunConfig } from '../../run/config.js'
22
22
import { RUN_CONFIG_FILE } from '../../run/constants.js'
23
23
import type { PluginContext , RequiredServerFilesManifest } from '../plugin-context.js'
24
+ import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin.js'
24
25
25
26
const tracer = wrapTracer ( trace . getTracer ( 'Next runtime' ) )
26
27
@@ -324,23 +325,36 @@ export const copyNextDependencies = async (ctx: PluginContext): Promise<void> =>
324
325
}
325
326
326
327
/**
327
- * Generates a copy of the middleware manifest without any middleware in it . We
328
+ * Generates a copy of the middleware manifest that make all matchers never match on anything . We
328
329
* do this because we'll run middleware in an edge function, and we don't want
329
- * to run it again in the server handler.
330
+ * to run it again in the server handler. Additionally Next.js conditionally enable some handling
331
+ * depending if there is a middleware present, so we need to keep reference to middleware in server
332
+ * even if we don't actually want to ever run it there.
330
333
*/
331
334
const replaceMiddlewareManifest = async ( sourcePath : string , destPath : string ) => {
332
335
await mkdir ( dirname ( destPath ) , { recursive : true } )
333
336
334
337
const data = await readFile ( sourcePath , 'utf8' )
335
- const manifest = JSON . parse ( data )
338
+ const manifest = JSON . parse ( data ) as MiddlewareManifest
336
339
337
340
// TODO: Check for `manifest.version` and write an error to the system log
338
341
// when we find a value that is not equal to 2. This will alert us in case
339
342
// Next.js starts using a new format for the manifest and we're writing
340
343
// one with the old version.
341
344
const newManifest = {
342
345
...manifest ,
343
- middleware : { } ,
346
+ middleware : Object . fromEntries (
347
+ Object . entries ( manifest . middleware ) . map ( ( [ key , edgeFunctionDefinition ] ) => {
348
+ // we can mutate in place here is we have throw away copy of middleware manifest not used anywhere else
349
+ edgeFunctionDefinition . matchers = edgeFunctionDefinition . matchers . map ( matcher => {
350
+ return {
351
+ ...matcher ,
352
+ // matcher that won't match on anything
353
+ regexp : '(?!.*)'
354
+ }
355
+ } )
356
+ return [ key , edgeFunctionDefinition ]
357
+ }
344
358
}
345
359
const newData = JSON . stringify ( newManifest )
346
360
0 commit comments