2021-11-02 16:13:15 +01:00
|
|
|
import { stringifyRequest } from '../stringify-request'
|
2021-10-20 19:52:11 +02:00
|
|
|
|
|
|
|
export type MiddlewareLoaderOptions = {
|
|
|
|
absolutePagePath: string
|
|
|
|
page: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function middlewareLoader(this: any) {
|
2021-11-02 16:13:15 +01:00
|
|
|
const { absolutePagePath, page }: MiddlewareLoaderOptions = this.getOptions()
|
|
|
|
const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
|
2021-10-20 19:52:11 +02:00
|
|
|
|
|
|
|
return `
|
|
|
|
import { adapter } from 'next/dist/server/web/adapter'
|
|
|
|
|
|
|
|
var mod = require(${stringifiedPagePath})
|
|
|
|
var handler = mod.middleware || mod.default;
|
|
|
|
|
|
|
|
if (typeof handler !== 'function') {
|
|
|
|
throw new Error('The Middleware "pages${page}" must export a \`middleware\` or a \`default\` function');
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function (opts) {
|
|
|
|
return adapter({
|
|
|
|
...opts,
|
2021-10-26 17:03:39 +02:00
|
|
|
page: ${JSON.stringify(page)},
|
|
|
|
handler,
|
2021-10-20 19:52:11 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|