Is there a way to remove the X-NextJs-Rewrite header?

256 Views Asked by At

We currently have a condition setup within NextJS middleware to rewrite the URL when the pathname starts with a particular value. The rewrite constructs a new URL object with a different host then the current request, but appends the current pathname and any query params. I can see the response headers have a X-middleware-rewrite header, however, when removing this, the rewrite fails completed. Allowing this through, I can see a response header of X-NextJs-Rewrite within the browser's devtools network tab that contains the value of the full URL that I constructed in the the function described above. However, I need to remove this. Does anyone know a way?

This is my code that does the rewrite;

  if(url?.pathname.startsWith('/law/view')){
    url?.searchParams.delete('path');
    let pathNameAndParams = url?.pathname + url.search;
    const res = NextResponse.rewrite(new URL(process.env.LAW_HOSTNAME + pathNameAndParams, url));
    res.headers.append('X-law-rewrite', 'true');
 
    return res;
  }

I have tried to setup a condition to detect the header on a request and response objects within NextJS middleware and within next.config.js but I don't seem to be able to detect the header key of 'X-NextJS-Rewrite'.

0

There are 0 best solutions below