Skip to content

Commit 770b08a

Browse files
committed
fix: disable instead of removing middleware from lambda
1 parent b25ee4a commit 770b08a

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/build/content/server.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { prerelease, satisfies, lt as semverLowerThan, lte as semverLowerThanOrE
2121
import type { RunConfig } from '../../run/config.js'
2222
import { RUN_CONFIG_FILE } from '../../run/constants.js'
2323
import type { PluginContext, RequiredServerFilesManifest } from '../plugin-context.js'
24+
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin.js'
2425

2526
const tracer = wrapTracer(trace.getTracer('Next runtime'))
2627

@@ -324,23 +325,37 @@ export const copyNextDependencies = async (ctx: PluginContext): Promise<void> =>
324325
}
325326

326327
/**
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
328329
* 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.
330333
*/
331334
const replaceMiddlewareManifest = async (sourcePath: string, destPath: string) => {
332335
await mkdir(dirname(destPath), { recursive: true })
333336

334337
const data = await readFile(sourcePath, 'utf8')
335-
const manifest = JSON.parse(data)
338+
const manifest = JSON.parse(data) as MiddlewareManifest
336339

337340
// TODO: Check for `manifest.version` and write an error to the system log
338341
// when we find a value that is not equal to 2. This will alert us in case
339342
// Next.js starts using a new format for the manifest and we're writing
340343
// one with the old version.
341344
const newManifest = {
342345
...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+
}),
358+
),
344359
}
345360
const newData = JSON.stringify(newManifest)
346361

0 commit comments

Comments
 (0)