Next.js Discord

Discord Forum

Middleware not running in 'yarn build' mode but working in dev mode

Unanswered
Cane di Oropa posted this in #help-forum
Open in Discord
Cane di OropaOP
I'm using page router with next 13.4 , I have the middleware file in the same level as pages directory and I have added some console statements inside the middleware function. In dev mode, these console statements does print but when i do yarn build and yarn start , these console statements does not come up

3 Replies

Cane di OropaOP
My middleware function :
export function middleware(request) {
  console.log("************************MIDDLEWARE************************");

  console.log("************************MIDDLEWARE************************");
  const host = request.headers.get("host");
  const nextResponse = NextResponse.next();
  const url = request.nextUrl;
  const token = url.searchParams.get("token");

  let changed;
  if (isNotNil(token)) {
    url.searchParams.delete("token");
    changed = true;
  }

  // Avoid infinite loop by only redirecting if the query params were changed
  if (changed) {
    const response = NextResponse.redirect(url);
    response.cookies.set(AUTH_TOKEN_NAME, token);

    return response;
  }

  if (url.pathname.includes("_next/static")) {
    const requestHeaders = new Headers(request.headers);
    const protocol = request.headers.get("x-forwarded-proto").split(",")[0];
    const origin = `${protocol}://${host}`;

    requestHeaders.set("Access-Control-Allow-Origin", origin);

    return nextResponse;
  }

  const isOldSafariVersion = checkOldSafariVersion(request);
  if (isOldSafariVersion) {
    return getRedirectToHomeResponse(request);
  }

  return nextResponse;
}
Cane di OropaOP
I found the issue why middleware was not getting built, went through every deployment logs to see when middleware stopped getting built, found that this line caused the issue:
module.exports = setupHoneybadger(
  nextTranslate(withYAML(withImages(nextConfig))),
  honeybadgerNextJsConfig
);

when I remove the setupHoneybadger part, middleware is getting built. Does anyone have this issue with middleware and honeybadger?
Gharial
facing the same issue, but I dont have nextTranslate at all