Spaces:
Running
Running
File size: 1,727 Bytes
c10f8f8 4b80e35 bdde20c 4b80e35 404f370 bdde20c 404f370 bdde20c c10f8f8 8bce833 0698bd6 8bce833 6cca3b1 8bce833 c10f8f8 |
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 |
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
// Get the actual hostname from headers (important for proxied environments like HF Spaces)
const forwardedHost = request.headers.get("x-forwarded-host");
const host = request.headers.get("host");
const hostname = forwardedHost || host || request.nextUrl.hostname;
console.log("[Middleware] x-forwarded-host:", forwardedHost, "host:", host, "hostname:", hostname);
const isLocalDev = hostname === "localhost" || hostname === "127.0.0.1" || hostname.startsWith("localhost:") || hostname.startsWith("127.0.0.1:");
const isHuggingFace = hostname === "huggingface.co" || hostname.endsWith(".huggingface.co");
console.log("[Middleware] isHuggingFace:", isHuggingFace, "isLocalDev:", isLocalDev);
if (!isHuggingFace && !isLocalDev) {
console.log("[Middleware] Redirecting to huggingface.co");
const canonicalUrl = new URL("https://huggingface.co/deepsite");
canonicalUrl.pathname = request.nextUrl.pathname;
canonicalUrl.search = request.nextUrl.search;
return NextResponse.redirect(canonicalUrl, 301);
}
const headers = new Headers(request.headers);
headers.set("x-current-host", request.nextUrl.host);
const response = NextResponse.next({ headers });
if (request.nextUrl.pathname.startsWith('/_next/static')) {
response.headers.set('Cache-Control', 'public, max-age=31536000, immutable');
}
response.headers.set('X-Canonical-URL', `https://huggingface.co/deepsite${request.nextUrl.pathname}`);
return response;
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};
|