-
Notifications
You must be signed in to change notification settings - Fork 9
/
middleware.ts
28 lines (22 loc) · 946 Bytes
/
middleware.ts
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 { redirectTournamentsMiddleware } from "./src/middlewares/redirect-tournaments.middleware";
import { routeVisitMiddleware } from "./src/middlewares/route-visit.middleware";
// Only return the response when you need a redirect or something
const middlewareList = [routeVisitMiddleware, redirectTournamentsMiddleware];
type MiddlewareFn = (
request: NextRequest,
response: NextResponse,
) => NextResponse | void;
function withMiddlewares(middlewares: MiddlewareFn[]) {
return async (request: NextRequest) => {
const response = NextResponse.next();
for (const middleware of middlewares) {
const result = await middleware(request, response);
if (result instanceof NextResponse) {
return result;
}
}
return response;
};
}
export const middleware = withMiddlewares(middlewareList);