Next.js Discord

Discord Forum

Middleware not working on production

Unanswered
British Shorthair posted this in #help-forum
Open in Discord
Avatar
British ShorthairOP
I have this middleware code, which checks if the user is on the main domain, if he isn't it redirects him to the main one. However this doesn't seem to be working. here is the code:
import { NextRequest, NextResponse } from "next/server"
export async function middleware(request: NextRequest) {
  const url = request.nextUrl.clone();

  const isProduction = process.env.NODE_ENV === "production";
  const requestedHost = request.headers.get("X-Forwarded-Port");

  if (isProduction && requestedHost && !requestedHost.match(/domain.com/)) {
    const host = `domain.com`;

    const requestedPort = request.headers.get('X-Forwarded-Port');
    const requestedProto = request.headers.get('X-Forwarded-Proto');

    url.host = host;
    url.protocol = requestedProto || url.protocol;
    url.port = requestedPort || url.port;

    return NextResponse.redirect(url);
  };

  return NextResponse.next();
}

export const config = {
  matcher: [
    '/((?!_next).*)',
  ]


Can someone tell me why this isn't working and where is the mistake? Thanks!

33 Replies

Avatar
Ray
where is your middleware.ts at?
Avatar
British ShorthairOP
it's on the root level: /
Avatar
Ray
do you use src folder
Avatar
British ShorthairOP
no
Avatar
Ray
try add a console.log and check the server console
export async function middleware(request: NextRequest) {
  const url = request.nextUrl.clone();

  const isProduction = process.env.NODE_ENV === "production";
  const requestedHost = request.headers.get("X-Forwarded-Port");

  console.log('middleware')

  if (isProduction && requestedHost && !requestedHost.match(/domain.com/)) {
    const host = `domain.com`;

    const requestedPort = request.headers.get('X-Forwarded-Port');
    const requestedProto = request.headers.get('X-Forwarded-Proto');

    url.host = host;
    url.protocol = requestedProto || url.protocol;
    url.port = requestedPort || url.port;

    return NextResponse.redirect(url);
  };

  return NextResponse.next();
}

export const config = {
  matcher: [
    '/((?!_next).*)',
  ]
}
Avatar
British ShorthairOP
you mean the runtime logs?
because I actually added a console.log inside the if statement but nothing was showing up
Avatar
Ray
maybe it doesn't match the if statement?
I added outside the if statement
Avatar
British ShorthairOP
yeah I see
thanks
I will try to add it and will let you know later
Avatar
British ShorthairOP
hi @Ray, I think I found where the problem is, apperantly the header X-Forwarded-Port is returning null
Avatar
Ray
cool
Avatar
British ShorthairOP
any idea how I can fix that?
Because I cannot find the header that returns the domain
Avatar
Ray
where do you host your site
Avatar
British ShorthairOP
on vercel
Avatar
Ray
try request.headers.get('Origin');?
Avatar
British ShorthairOP
ok
still return null
Avatar
Ray
request.headers.get('x-forwarded-host')
Avatar
British ShorthairOP
still it's not working
Avatar
Ray
what does it return
Avatar
British ShorthairOP
null
I tried url.host
from this const url = request.nextUrl.clone();
Avatar
Ray
console.log(Object.fromEntries(request.headers));
Avatar
British ShorthairOP
I'll try that
url.host
is working
thanks a lot
Avatar
Ray
no prob