forked from redirectionio/vercel-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.js
28 lines (28 loc) · 1.11 KB
/
next.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { NextRequest, NextResponse } from "next/server";
import { createRedirectionIoMiddleware as createEdgeMiddleware } from "./middleware";
export const createRedirectionIoMiddleware = (config) => {
let previousMiddleware;
let nextMiddleware;
const configPreviousMiddleware = config.previousMiddleware;
const configNextMiddleware = config.nextMiddleware;
const configMatcherRegex = config.matcherRegex;
if (configPreviousMiddleware) {
previousMiddleware = (req, context) => {
return configPreviousMiddleware(new NextRequest(req.url, req), context);
};
}
if (configNextMiddleware) {
nextMiddleware = (req, context) => {
return configNextMiddleware(new NextRequest(req.url, req), context);
};
}
const edgeMiddleware = createEdgeMiddleware({
previousMiddleware,
nextMiddleware,
...(configMatcherRegex ? { matcherRegex: configMatcherRegex } : {}),
});
return async (req, context) => {
const response = await edgeMiddleware(req, context);
return new NextResponse(response.body, response);
};
};