Repository navigation
Expand file tree
/
Copy pathmiddleware.ts
More file actions
executable file
·176 lines (145 loc) · 5.01 KB
/
Copy pathmiddleware.ts
File metadata and controls
executable file
·176 lines (145 loc) · 5.01 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { type NextRequest, NextResponse } from "next/server";
import { updateSession } from "@/utils/supabase/middleware";
import { createClient } from "@/utils/supabase/server";
const LOCAL_HOSTS = ["localhost", "127.0.0.1"];
const INTERNAL_PATH_PREFIXES = ["/_next", "/webcontainer"];
const LOCAL_PREVIEW_PREFIX_COOKIE = "coderocket_local_preview_prefix";
async function checkCustomDomain(hostname: string) {
try {
const supabase = await createClient();
const { data: customDomain } = await supabase
.from("custom_domains")
.select("chat_id, is_verified")
.eq("domain", hostname)
.eq("is_verified", true)
.maybeSingle();
if (customDomain) {
const { data: chat } = await supabase
.from("chats")
.select("deploy_subdomain, is_deployed")
.eq("id", customDomain.chat_id)
.eq("is_deployed", true)
.maybeSingle();
if (chat?.deploy_subdomain) {
return chat.deploy_subdomain;
}
}
} catch (error) {
console.error("Error checking custom domain:", error);
}
return null;
}
function isLocalHost(hostname: string | null): boolean {
if (!hostname) {
return false;
}
return LOCAL_HOSTS.some(
(localHost) =>
hostname === localHost || hostname.startsWith(`${localHost}:`),
);
}
function extractLocalWebcontainerPrefix(request: NextRequest): string | null {
const referer = request.headers.get("referer");
if (!referer) {
return null;
}
try {
const refererUrl = new URL(referer);
const requestHost = request.headers.get("host");
if (!requestHost || refererUrl.host !== requestHost) {
return null;
}
const parts = refererUrl.pathname.split("/").filter(Boolean);
if (parts[0] !== "webcontainer" || !parts[1]) {
return null;
}
return parts[1];
} catch {
return null;
}
}
function looksLikeStaticAssetPath(pathname: string): boolean {
if (pathname.startsWith("/assets/")) {
return true;
}
const lastSegment = pathname.split("/").pop() || "";
return lastSegment.includes(".") && !pathname.endsWith(".html");
}
export async function middleware(request: NextRequest) {
const response = await updateSession(request);
const hostname = request.headers.get("host");
if (hostname === "tailwindai.dev" || hostname?.endsWith(".tailwindai.dev")) {
const url = request.nextUrl.clone();
url.host = "coderocket.app";
return NextResponse.redirect(url, 301);
}
if (
isLocalHost(hostname) &&
!INTERNAL_PATH_PREFIXES.some((prefix) =>
request.nextUrl.pathname.startsWith(prefix),
)
) {
const cookiePrefix = request.cookies.get(LOCAL_PREVIEW_PREFIX_COOKIE)?.value;
if (cookiePrefix && looksLikeStaticAssetPath(request.nextUrl.pathname)) {
const rewrittenUrl = request.nextUrl.clone();
rewrittenUrl.pathname = `/webcontainer/${cookiePrefix}${request.nextUrl.pathname}`;
return NextResponse.rewrite(rewrittenUrl, response);
}
const localPreviewPrefix = extractLocalWebcontainerPrefix(request);
if (localPreviewPrefix) {
const rewrittenUrl = request.nextUrl.clone();
rewrittenUrl.pathname = `/webcontainer/${localPreviewPrefix}${request.nextUrl.pathname}`;
return NextResponse.rewrite(rewrittenUrl, response);
}
}
if (
hostname &&
!hostname.includes("coderocket.app") &&
!hostname.includes("localhost") &&
!hostname.includes("127.0.0.1")
) {
const customSubdomain = await checkCustomDomain(hostname);
if (customSubdomain) {
const pathname = request.nextUrl.pathname;
const newUrl = new URL(
`/webcontainer/_custom${pathname}${request.nextUrl.search}`,
request.nextUrl,
);
const customResponse = NextResponse.rewrite(newUrl, response);
customResponse.headers.set("x-custom-domain", hostname);
customResponse.headers.set("x-subdomain", customSubdomain);
return customResponse;
}
}
const subdomainConfig = {
"preview.coderocket.app": "webcontainer",
"webcontainer.coderocket.app": "webcontainer",
} as const;
for (const [domain, path] of Object.entries(subdomainConfig)) {
if (hostname?.endsWith(`.${domain}`)) {
const prefix = hostname.split(".")[0];
const pathname = request.nextUrl.pathname;
const newUrl = new URL(`/${path}/${prefix}${pathname}`, request.nextUrl);
return NextResponse.rewrite(newUrl, response);
}
}
if (
hostname?.endsWith(".coderocket.app") &&
hostname !== "www.coderocket.app" &&
hostname !== "coderocket.app" &&
!hostname.includes("preview.coderocket.app") &&
!hostname.includes("webcontainer.coderocket.app")
) {
const pathname = request.nextUrl.pathname;
const newUrl = new URL(
`/webcontainer/_custom${pathname}${request.nextUrl.search}`,
request.nextUrl,
);
return NextResponse.rewrite(newUrl, response);
}
return response;
}
// Applique le middleware sur toutes les routes sauf fichiers système Next.js
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};