#NextJS Middleware rewrite

13 messages · Page 1 of 1 (latest)

faint sluice
#

I wrote a middleware function to customize the headers to a proxied response. The server I am proxying to returns headers that I want to remove.
No matter how I do it, the response headers from the proxied service are always present on the request. I tried overwriting the header, and tried removing it

const headers = new Headers(); headers.set("x-robots-tag", "foo"); headers.set("foo", "bar"); return NextResponse.rewrite(new URL(path, pantheonURL), { headers, });
In this example, I will see the "foo" header correctly, but the "x-robots-tag" header is overwritten by the proxy server. Is there anything I can do?

faint sluice
#

Does anyone know if this is an issue or expected behavior? It seems a bit weird there is no way to change the headers from a proxied response

jolly yoke
#

this is how I used to do it in workers

  let response = await fetch(destinationURL, request)

  response = new Response(response.body, response)
  response.headers.delete("headerName")
  
  return response
faint sluice
#

@jolly yoke the header is coming from the server being proxied to. Ideally I can delete it but it seems like when rewriting you aren’t able to delete headers from server being proxied to

#

Would I have to make a new response maybe? Not sure I can do that with a rewrite

#

But yeah if I delete it, the header from the server is still applied in the end

shell shard
#

You need to create a new response in middleware

faint sluice
#

Am I able to do that with a rewrite?

shell shard
#

Hmm, the rewrite function already returns a new response, and you need to return that one

faint sluice
#

That’s what I’m doing, but no matter what the headers from the server are applied

#

It must be the last step in the process or something

#

I also tried setting the rewrite function to a variable and deleting the header that way. No dice