Skip to content

Commit b3aaed6

Browse files
fix: concatenate all edge chunks (#319)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent f0edaba commit b3aaed6

File tree

8 files changed

+309
-311
lines changed

8 files changed

+309
-311
lines changed

src/build/functions/edge.ts

+29-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
2-
import { dirname, join, relative } from 'node:path'
2+
import { dirname, join } from 'node:path'
33

44
import { glob } from 'fast-glob'
55
import type { EdgeFunctionDefinition as NextDefinition } from 'next/dist/build/webpack/plugins/middleware-plugin.js'
@@ -116,44 +116,34 @@ const copyHandlerDependencies = async (
116116
const srcDir = join(ctx.standaloneDir, ctx.nextDistDir)
117117
const destDir = join(ctx.edgeFunctionsDir, getHandlerName({ name }))
118118

119-
await Promise.all(
120-
files.map(async (file) => {
121-
if (file === `server/${name}.js`) {
122-
const edgeRuntimeDir = join(ctx.pluginDir, 'edge-runtime')
123-
const shimPath = join(edgeRuntimeDir, 'shim/index.js')
124-
const shim = await readFile(shimPath, 'utf8')
125-
126-
const importsDir = relative(dirname(join(srcDir, file)), join(srcDir, 'server'))
127-
const importsSrc = `${importsDir || '.'}/edge-runtime-webpack.js`
128-
const imports = `import '${importsSrc}';`
129-
130-
const exports = `export default _ENTRIES["middleware_${name}"].default;`
131-
132-
const parts = [shim, imports]
133-
134-
if (wasm?.length) {
135-
parts.push(
136-
`import { decode as _base64Decode } from "../edge-runtime/vendor/deno.land/[email protected]/encoding/base64.ts";`,
137-
)
138-
for (const wasmChunk of wasm ?? []) {
139-
const data = await readFile(join(srcDir, wasmChunk.filePath))
140-
parts.push(
141-
`const ${wasmChunk.name} = _base64Decode(${JSON.stringify(
142-
data.toString('base64'),
143-
)}).buffer`,
144-
)
145-
}
146-
}
147-
148-
const entrypoint = await readFile(join(srcDir, file), 'utf8')
149-
150-
await mkdir(dirname(join(destDir, file)), { recursive: true })
151-
await writeFile(join(destDir, file), [...parts, entrypoint, exports].join('\n;'))
152-
} else {
153-
await cp(join(srcDir, file), join(destDir, file))
154-
}
155-
}),
156-
)
119+
const edgeRuntimeDir = join(ctx.pluginDir, 'edge-runtime')
120+
const shimPath = join(edgeRuntimeDir, 'shim/index.js')
121+
const shim = await readFile(shimPath, 'utf8')
122+
123+
const parts = [shim]
124+
125+
if (wasm?.length) {
126+
parts.push(
127+
`import { decode as _base64Decode } from "../edge-runtime/vendor/deno.land/[email protected]/encoding/base64.ts";`,
128+
)
129+
for (const wasmChunk of wasm ?? []) {
130+
const data = await readFile(join(srcDir, wasmChunk.filePath))
131+
parts.push(
132+
`const ${wasmChunk.name} = _base64Decode(${JSON.stringify(
133+
data.toString('base64'),
134+
)}).buffer`,
135+
)
136+
}
137+
}
138+
139+
for (const file of files) {
140+
const entrypoint = await readFile(join(srcDir, file), 'utf8')
141+
parts.push(`;// Concatenated file: ${file} \n`, entrypoint)
142+
}
143+
const exports = `export default _ENTRIES["middleware_${name}"].default;`
144+
await mkdir(dirname(join(destDir, `server/${name}.js`)), { recursive: true })
145+
146+
await writeFile(join(destDir, `server/${name}.js`), [...parts, exports].join('\n'))
157147
}
158148

159149
const createEdgeHandler = async (ctx: PluginContext, definition: NextDefinition): Promise<void> => {

tests/fixtures/middleware-pages/package-lock.json

+49-67
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/middleware-pages/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "next build"
99
},
1010
"dependencies": {
11-
"next": "^14.0.4",
11+
"next": "^14.1.1",
1212
"react": "18.2.0",
1313
"react-dom": "18.2.0"
1414
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const runtime = 'edge'
2+
3+
export default (req) => {
4+
return Response.json(Object.fromEntries(req.headers.entries()), {
5+
headers: {
6+
'headers-from-edge-function': '1',
7+
},
8+
})
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export default function handler(req, res) {
2+
res.headers = { 'headers-from-function': '1' }
23
res.json({ url: req.url, headers: req.headers })
34
}

0 commit comments

Comments
 (0)