2022-06-13 20:17:44 +02:00
|
|
|
import { getModuleBuildInfo } from './get-module-build-info'
|
|
|
|
import { stringifyRequest } from '../stringify-request'
|
|
|
|
|
|
|
|
export type EdgeFunctionLoaderOptions = {
|
|
|
|
absolutePagePath: string
|
|
|
|
page: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function middlewareLoader(this: any) {
|
|
|
|
const { absolutePagePath, page }: EdgeFunctionLoaderOptions =
|
|
|
|
this.getOptions()
|
|
|
|
const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
|
|
|
|
const buildInfo = getModuleBuildInfo(this._module)
|
|
|
|
buildInfo.nextEdgeApiFunction = {
|
|
|
|
page: page || '/',
|
|
|
|
}
|
|
|
|
|
|
|
|
return `
|
2022-07-20 16:53:27 +02:00
|
|
|
import { adapter, enhanceGlobals } from 'next/dist/server/web/adapter'
|
2022-06-13 20:17:44 +02:00
|
|
|
|
2022-07-20 16:53:27 +02:00
|
|
|
enhanceGlobals()
|
2022-06-13 20:17:44 +02:00
|
|
|
|
|
|
|
var mod = require(${stringifiedPagePath})
|
|
|
|
var handler = mod.middleware || mod.default;
|
|
|
|
|
|
|
|
if (typeof handler !== 'function') {
|
|
|
|
throw new Error('The Edge Function "pages${page}" must export a \`default\` function');
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function (opts) {
|
|
|
|
return adapter({
|
|
|
|
...opts,
|
|
|
|
page: ${JSON.stringify(page)},
|
|
|
|
handler,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|