Skip to content

Commit

Permalink
forward request pathname as a special header: x-pathname
Browse files Browse the repository at this point in the history
  • Loading branch information
a-hariti committed Apr 24, 2024
1 parent fb2d14d commit 9fff81a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@ export const config = {

// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
return handleRedirects(request);
const redirect = handleRedirects(request);
if (redirect) {
return redirect;
}

// Clone the request headers and forward the pathname to be consumed elsewhere using next/headers
const requestHeaders = new Headers(request.headers);
requestHeaders.set('x-pathname', request.nextUrl.pathname);

const response = NextResponse.next({
request: {
headers: requestHeaders,
},
});
return response;
}

const handleRedirects = (request: NextRequest) => {
Expand Down

0 comments on commit 9fff81a

Please sign in to comment.