-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.js
More file actions
34 lines (32 loc) · 1.36 KB
/
middleware.js
File metadata and controls
34 lines (32 loc) · 1.36 KB
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 { rewrite } from "@vercel/functions";
export default function middleware(request) {
const url = new URL(request.url);
const hostname = request.headers.get("host"); // includes port
console.log(url, hostname);
let siteHost = process.env.PROD_SITE_HOSTNAME
let siteProtocol = "https://"
if (process.env.NODE_ENV == "development") {
console.log("development")
siteHost = "localhost:3000"
siteProtocol = "http://"
}
console.log(hostname, siteHost)
if (hostname != siteHost && !hostname.endsWith(".vercel.app")) {
const newHost = hostname.replace(`.${siteHost}`, "");
const finalURL = `/s/${newHost}${url.pathname}?${process.env.MIDDLEWARE_REWRITE_TOKEN}=true`;
console.log(finalURL);
return rewrite(siteProtocol + siteHost + finalURL);
} else {
console.log("host", hostname);
if (url.searchParams.has(process.env.MIDDLEWARE_REWRITE_TOKEN)) {
console.log("request has already been rewritten");
} else {
if (url.pathname.startsWith("/s/") && url.pathname.slice(3).length > 0) {
console.log("request has not been rewritten already");
console.log("redirecting to 404");
return Response.redirect(new URL(url.origin + "/404"));
}
}
console.log("doing nothing");
}
}