-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
34 lines (30 loc) · 1 KB
/
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
29
30
31
32
33
34
import { NextRequest, NextResponse } from "next/server";
import logger from "./logger";
const isURLImage = (path: string) => {
const types = ["png", "svg", "ico", "jpeg", "jpg", "webp", "gif"];
return types.some((type) => path.endsWith(`.${type}`));
};
const hasNextJsMiddlewareHeader = (headers: Headers) => {
return (
headers.has("x-middleware-prefetch") &&
headers.get("x-middleware-prefetch") === "1"
);
};
export function middleware(request: NextRequest) {
const LOGGING_ENABLED = process.env.LOGGING_ENABLED === "true";
if (LOGGING_ENABLED) {
const path = request.nextUrl.pathname;
const headers = request.headers;
const log = {
method: request.method,
path: request.nextUrl.pathname,
params: request.nextUrl.search || null,
ua: request.headers.get("user-agent") || null,
isPrefetch: hasNextJsMiddlewareHeader(headers),
};
if (!path.startsWith("/_next") && !isURLImage(path)) {
logger.info(log);
}
}
return NextResponse.next();
}