next-cochise/src/middleware.ts

35 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-02-12 10:42:17 +07:00
// middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// the following code is taken from : https://nextjs.org/docs/advanced-features/middleware#setting-headers
export function middleware(request: NextRequest) {
const protocol = request.headers.get("x-forwarded-proto") || "http"; // Default to 'http' if not provided
const host = request.headers.get("x-forwarded-host") || request.nextUrl.hostname;
const path = request.nextUrl.pathname + request.nextUrl.search;
// Construct the full URL
const fullUrl = `${protocol}://${host}${path}`;
request.headers.set("x-full-url", fullUrl);
return NextResponse.next({
request: {
// New request headers
headers: request.headers,
},
});
}
// the following code has been copied from https://nextjs.org/docs/advanced-features/middleware#matcher
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
*/
"/((?!api|_next/static|_next/image|assets|sw.js|favicon.ico|sitemap.xml|robots.txt).*)",
],
};