unjs / h3

⚡️ Minimal H(TTP) framework built for high performance and portability
https://h3.unjs.io/
MIT License
3.35k stars 188 forks source link

proxyRequest does not proxy the Accept header #709

Open StevenPewsey opened 3 months ago

StevenPewsey commented 3 months ago

Environment

Node 18.16.1

Reproduction

N/A

Describe the bug

Following https://github.com/unjs/h3/pull/646, proxyRequest ignores any Accept headers.

This is breaking my application (following a nuxt upgrade from 3.9 -> 3.11) as we proxy api requests via the server to a backend API, which requires us to send through an Accept header.

As a fix for the timebeing I can explicitly pass through the Accept header as follows

  return proxyRequest(event, targetPath, {
    headers: {
      Accept: event.headers.get('Accept') || ''
    }
  })

but this feels a bit unnecessary to me, I would have expected h3 to send through this header by default.

Additional context

No response

Logs

No response

brc-dd commented 3 months ago

I too believe this was a bit of breaking change and doesn't work as expected. I'm using nuxt's routeRules to specify proxies and I don't think there's any way to manually forward headers from there. For API responses, I can probably hard code it:

  routeRules: {
    '/api/**': {
      proxy: {
        to: '...',
        headers: { accept: 'application/json' }
      }
    },

But I also have stuff like:

    '/images/**': {
      proxy: {
        to: '...'
      }
    },

I want the backend to be able to determine what the browser supports. Currently it always responds with the fallback format instead of modern ones like avif even if the browser support them because that info is removed while proxying.

For the time being, creating a middleware for proxying and calling proxyRequest like OP wrote seems to work.

ahoiroman commented 3 weeks ago

I am facing this issue, too. I am missing the following headers when using routeRules:

which breaks my auth against Laravel Sanctum, which worked in the past using a routeRules-proxy.