Open aakash19here opened 4 months ago
@aakash19here you can stack middleware in nextjs
// middleware-factory.ts
import { NextResponse, type NextMiddleware } from "next/server";
export type MiddlewareFactory = (middleware: NextMiddleware) => NextMiddleware;
export function stackMiddlewares(functions: MiddlewareFactory[] = [], index = 0): NextMiddleware {
const current = functions[index];
if (current) {
const next = stackMiddlewares(functions, index + 1);
return current(next);
}
return () => NextResponse.next();
}
// i18n.ts
const I18nMiddleware = createI18nMiddleware({
locales: ["en", "ja"],
defaultLocale: "en",
urlMappingStrategy: "rewrite",
});
export const withI18n: MiddlewareFactory = (next: NextMiddleware) => {
return async (request: NextRequest, _next: NextFetchEvent) => {
await next(request, _next);
return I18nMiddleware(request);
};
};
// middleware.ts
const middlewares = [withI18n, withExample];
export default stackMiddlewares(middlewares);
export const config = {
....
};
Will try and revert back , Thanks for the help !
@aakash19here you can stack middleware in
nextjs
...
Hello!
I would be happy to chain the Next International middleware exactly as I chain the NextAuth one here: https://github.com/Tirraa/dashboard_rtm/blob/main/src/middlewares/withProtectedRoutes.ts#L14
Calling next()
before calling the i18n middleware for real breaks the order of the chain. Since we'll first call whatever is next
, and then go back to the i18n middleware.
By doing so, writing:
[withI18n, withAuth]
Would result in:
withAuth -> withI18n
This bothers me: it's not the expected behavior. :/
I'd prefer to have something more naive and predictable:
Also, I'd prefer to call the i18n middleware before the Auth middleware to make sure that the auth gets the request with the most up-to-date language flag. Otherwise, the Auth middleware might make a mistake and return the wrong language (i.e.: the second-to-last).
Describe the bug With the all new v5 of next-auth , how do we actually chain the createi18nMiddleware with auth middleware from it ?
About (please complete the following information):