forked from dubinc/dub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
41 lines (35 loc) · 1.03 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
35
36
37
38
39
40
41
import { NextRequest, NextFetchEvent, NextResponse } from "next/server";
import {
AppMiddleware,
LinkMiddleware,
RootMiddleware,
} from "@/lib/middleware";
import { parse } from "@/lib/middleware/utils";
import { HOME_HOSTNAMES, RESERVED_KEYS } from "@/lib/constants";
export const config = {
matcher: [
/*
* Match all paths except for:
* 1. /api routes
* 2. /_next (Next.js internals)
* 3. /static (inside /public)
* 4. all root files inside /public (e.g. /favicon.ico)
*/
"/((?!api|_next|static|[\\w-]+\\.\\w+).*)",
],
};
export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
const { hostname, key } = parse(req);
const home = HOME_HOSTNAMES.includes(hostname);
const app = hostname === "app.dub.sh" || hostname === "app.localhost:3000";
if (app) {
return AppMiddleware(req);
}
if (key.length === 0) {
return RootMiddleware(req, ev);
}
if (home && RESERVED_KEYS.includes(key)) {
return NextResponse.next();
}
return LinkMiddleware(req, ev);
}