diff --git a/src/build/functions/edge.ts b/src/build/functions/edge.ts index 4c95ad1353..6d9919c11f 100644 --- a/src/build/functions/edge.ts +++ b/src/build/functions/edge.ts @@ -125,7 +125,7 @@ const copyHandlerDependencies = async ( const entrypoint = await readFile(join(srcDir, file), 'utf8') parts.push(`;// Concatenated file: ${file} \n`, entrypoint) } - const exports = `export default _ENTRIES["middleware_${name}"].default;` + const exports = `const middlewareEntryKey = Object.keys(_ENTRIES).find(entryKey => entryKey.startsWith("middleware_${name}")); export default _ENTRIES[middlewareEntryKey].default;` await mkdir(dirname(join(destDir, `server/${name}.js`)), { recursive: true }) await writeFile(join(destDir, `server/${name}.js`), [...parts, exports].join('\n')) diff --git a/tests/fixtures/middleware/next.config.js b/tests/fixtures/middleware/next.config.js index 9d94510be1..28875fd694 100644 --- a/tests/fixtures/middleware/next.config.js +++ b/tests/fixtures/middleware/next.config.js @@ -4,6 +4,13 @@ const nextConfig = { eslint: { ignoreDuringBuilds: true, }, + webpack: (config) => { + // this is a trigger to generate multiple `.next/server/middleware-[hash].js` files instead of + // single `.next/server/middleware.js` file + config.optimization.splitChunks.maxSize = 100_000 + + return config + }, } module.exports = nextConfig