-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcurrent-path.ts
More file actions
26 lines (23 loc) · 836 Bytes
/
current-path.ts
File metadata and controls
26 lines (23 loc) · 836 Bytes
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
import type { MiddlewareFactory } from './utils';
import type { NextFetchEvent, NextMiddleware, NextRequest } from 'next/server';
/**
* Middleware function for Next.js
*
* This middleware adds a custom header 'x-current-path' to the response,
* which contains the current pathname of the request.
*
* @todo This middleware is not used in the current implementation. It may be deprecated in the future.
*
* @param {NextMiddleware} next Next middleware.
*
* @return The response object with modified headers
*/
export const currentPath: MiddlewareFactory = ( next: NextMiddleware ) => {
return async ( request: NextRequest, _next: NextFetchEvent ) => {
const response = await next( request, _next );
if ( response ) {
response.headers.set( 'x-current-path', request.nextUrl.pathname );
}
return response;
};
};