-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
35 lines (30 loc) · 1008 Bytes
/
middleware.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
31
32
33
34
35
import { NextResponse } from "next/server";
export default function middleware(req) {
const { pathname } = req.nextUrl;
const hostname = req.headers.get("host");
const currentHost =
process.env.NODE_ENV === "production"
? hostname?.replace(`.allyner.com`, "")
: hostname?.replace(`.localhost:3000`, "");
if (pathname.startsWith(`/_sites`)) {
return new Response(null, { status: 404 });
}
if (!pathname.includes(".") && !pathname.startsWith("/api")) {
// For clients
if (
pathname.startsWith("/app") ||
hostname === "allyner.com" ||
hostname === "localhost:3000"
) {
const nextUrl = req.nextUrl.clone();
const newPathname = `${pathname}`;
nextUrl.pathname = newPathname;
return NextResponse.rewrite(nextUrl);
}
// For users
const newPathname = `/_sites/${currentHost}${pathname}`;
const nextUrl = req.nextUrl.clone();
nextUrl.pathname = newPathname;
return NextResponse.rewrite(nextUrl);
}
}