-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
27 lines (25 loc) · 879 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
import { NextApiRequest } from "next";
import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
const middleware = async (req: NextRequest, ev: NextFetchEvent) => {
if (req.nextUrl.pathname.split("/")[1].length !== 0) {
if (req.nextUrl.pathname.startsWith("/api/getUrl")) {
console.log("returning early");
return;
}
const fullPath = req.nextUrl.pathname.split("/")[1];
const data = await (
await fetch(`${req.nextUrl.origin}/api/${fullPath}`)
).json();
if (typeof data === "string") return NextResponse.redirect(data);
else {
const { msg } = data;
console.log(msg);
if (msg === "URL not found")
return NextResponse.redirect(req.nextUrl.origin);
}
}
};
export default middleware;
export const config = {
matcher: ["/((?!api|_next/static|favicon.ico|mockServiceWorker.js).*)"],
};