Spaces:
Running
Running
redirect
Browse files- middleware.ts +11 -0
- proxy.ts +0 -26
middleware.ts
CHANGED
|
@@ -2,6 +2,17 @@ import { NextResponse } from "next/server";
|
|
| 2 |
import type { NextRequest } from "next/server";
|
| 3 |
|
| 4 |
export function middleware(request: NextRequest) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
const headers = new Headers(request.headers);
|
| 6 |
headers.set("x-current-host", request.nextUrl.host);
|
| 7 |
|
|
|
|
| 2 |
import type { NextRequest } from "next/server";
|
| 3 |
|
| 4 |
export function middleware(request: NextRequest) {
|
| 5 |
+
const hostname = request.nextUrl.hostname;
|
| 6 |
+
const isLocalDev = hostname === "localhost" || hostname === "127.0.0.1" || hostname.startsWith("192.168.");
|
| 7 |
+
const isHuggingFace = hostname === "huggingface.co" || hostname.endsWith(".huggingface.co");
|
| 8 |
+
|
| 9 |
+
if (!isHuggingFace && !isLocalDev) {
|
| 10 |
+
const canonicalUrl = new URL(request.nextUrl.pathname + request.nextUrl.search, "https://huggingface.co");
|
| 11 |
+
canonicalUrl.pathname = request.nextUrl.pathname;
|
| 12 |
+
canonicalUrl.search = request.nextUrl.search;
|
| 13 |
+
return NextResponse.redirect(canonicalUrl, 301);
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
const headers = new Headers(request.headers);
|
| 17 |
headers.set("x-current-host", request.nextUrl.host);
|
| 18 |
|
proxy.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
| 1 |
-
import { NextResponse } from "next/server";
|
| 2 |
-
import type { NextRequest } from "next/server";
|
| 3 |
-
|
| 4 |
-
export function proxy(request: NextRequest) {
|
| 5 |
-
const hostname = request.headers.get("host") || "";
|
| 6 |
-
|
| 7 |
-
// Don't redirect if on localhost
|
| 8 |
-
if (hostname.includes("localhost") || hostname.includes("127.0.0.1")) {
|
| 9 |
-
return NextResponse.next();
|
| 10 |
-
}
|
| 11 |
-
|
| 12 |
-
// Check if user is on hf.co or huggingface.co domains
|
| 13 |
-
const isHuggingFace = hostname.includes("hf.co") || hostname.includes("huggingface.co");
|
| 14 |
-
|
| 15 |
-
// If not on HuggingFace domains, redirect to huggingface.co/deepsite
|
| 16 |
-
if (!isHuggingFace) {
|
| 17 |
-
return NextResponse.redirect("https://huggingface.co/deepsite", 301);
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
// Continue normally for HuggingFace domains
|
| 21 |
-
return NextResponse.next();
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
export const config = {
|
| 25 |
-
matcher: "/:path*", // Match all paths
|
| 26 |
-
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|