-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
middleware.js
55 lines (44 loc) · 1.41 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { NextResponse } from "next/server";
import { default as appConfig } from "@/config/index";
String.prototype.countChar = function (c) {
return this.split(c).length - 1;
};
export const config = {
matcher: ["/", "/_sites/:path"],
};
export default async function middleware(req) {
const url = req.nextUrl;
const hostname = req.headers.get("host");
const isDomain = hostname.indexOf(appConfig.frontendUrl) === -1;
const isSubdomain =
!isDomain &&
hostname.countChar(".") ===
(process.env.NODE_ENV === "development" ? 1 : 2);
if (isDomain) {
const host =
hostname.indexOf("www.") !== -1
? hostname.slice(hostname.indexOf("www.") + 4)
: hostname;
const site = await fetch(
`${appConfig.serverUrl}/api/website/getWebsiteByDomain?domain=${host}`,
{
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `bearer ${process.env.CREATE_WEBSITE_TOKEN}`,
},
},
);
const siteRes = await site.json();
if (!siteRes) return NextResponse.redirect("/404");
url.pathname = `/_sites/${siteRes.route}${url.pathname}`;
}
if (isSubdomain) {
const subpath = hostname.slice(0, hostname.indexOf("."));
if (!isDomain && subpath !== "www") {
url.pathname = `/_sites/${subpath}${url.pathname}`;
}
}
return NextResponse.rewrite(url);
}