-
Notifications
You must be signed in to change notification settings - Fork 2
/
next.js
30 lines (30 loc) · 1.19 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
29
30
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 } : {}),
mode: config.mode ?? "full",
logged: config.logged ?? true,
});
return async (req, context) => {
const response = await edgeMiddleware(req, context);
return new NextResponse(response.body, response);
};
};