From 666fe5a6f54c1134fe80054adcf6aac4aec1ec58 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Mon, 26 Aug 2024 17:31:39 +0200 Subject: [PATCH] fix: handle cases of chunked middleware (#2574) * test: make middleware fixture produce chunked middleware * fix: handle cases of chunked middleware --- src/build/functions/edge.ts | 2 +- tests/fixtures/middleware/next.config.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) 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