Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix for server actions headers problems #119

Closed
wants to merge 8 commits into from
Closed
5 changes: 4 additions & 1 deletion examples/next-auth/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ const middlewares = {
},
async ({ request }: MiddlewareFunctionProps) => {
console.log('Middleware for /page2', request.nextUrl.pathname);
console.log('Authenticated header value:', request.headers.get('x-authenticated'));
console.log(
'Authenticated header value:',
request.headers.get('x-authenticated'),
);
return NextResponse.redirect('http://localhost:3002/page1');
},
],
Expand Down
36 changes: 34 additions & 2 deletions packages/nemo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export type NextMiddleware = (

export type MiddlewareContext = Map<string, unknown>;

interface NEMOConfig {
excludedHeaders: string[];
}

/**
* Properties passed to middleware functions.
*/
Expand All @@ -35,6 +39,22 @@ export type MiddlewareConfig = Record<
MiddlewareFunction | MiddlewareFunction[]
>;

const defaultExcludedHeaders = ['content-type'];

/**
* Filters out excluded headers from the response.
* @param response - The response to filter headers from.
* @param excludedHeaders - The list of headers to exclude.
*/
function filterHeaders(
response: NextResponse,
excludedHeaders: string[],
): void {
excludedHeaders.forEach((header) => {
response.headers.delete(header);
});
}

/**
* Checks if the given middleware function is a legacy middleware.
* @param middleware - The middleware function to check.
Expand Down Expand Up @@ -80,12 +100,14 @@ async function executeMiddleware(
* Creates a middleware function that executes the given middleware functions.
* @param pathMiddlewareMap - The map of paths to middleware functions.
* @param globalMiddleware - The global middleware functions to execute
* @param config - The configuration for the NEMO.
*/
export function createMiddleware(
pathMiddlewareMap: MiddlewareConfig,
globalMiddleware?: Partial<
Record<'before' | 'after', MiddlewareFunction | MiddlewareFunction[]>
>,
config?: NEMOConfig,
): NextMiddleware {
return async (
request: NextRequest,
Expand Down Expand Up @@ -136,10 +158,20 @@ export function createMiddleware(
}
},
});
if (middlewareResponse instanceof Response) return middlewareResponse;
if (middlewareResponse instanceof Response) {
filterHeaders(
middlewareResponse as NextResponse,
config?.excludedHeaders ?? defaultExcludedHeaders,
);
return middlewareResponse;
}
}

return NextResponse.next({ request, headers: request.headers });
return NextResponse.next({
request: {
headers: new Headers(request.headers),
},
});
};
}

Expand Down
Loading