Empty Location header using njs subrequest

Hi All!

My issue:
I need send some request to the /app location before using it and redirect browser to Location from reply of that request in case of status = 302.

The problem is Location header is not filled during subrequest handling.

The reply.headersOut has Location with proper value, both Foo and Content-Type headers work as expected, but not Location one.

How I encountered the problem:

$curl -I myhost.local
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.26.2
Location: <--empty
Foo: Bar <--ok
Content-Type: text/html <--ok

Solutions I’ve tried:
njs version: 0.8.7
lib.js:

async function check(r) {
 let reply = await r.subrequest('/app')
 if (reply.status == 302) {
   r.headersOut['Location'] = reply.headersOut['Location'] <--doesn't work 
   r.headersOut['Foo'] = 'Bar' <--works      
 }
 r.headersOut['Content-Type'] = reply.headersOut['Content-Type'] <--works 
 r.return(reply.status, reply.responseText) 
}

My config:

server {
    server_name myhost.local;
    js_import 'lib.js';
    location / {
      js_content lib.check;
    }
    location = /app {
      internal;
      proxy_pass http://localhost:3000$request_uri;
    }
  }

Thanks in advance!

I found the solution here.
The second argument of r.return can be a redirect URL in case of 302 code:

if (reply.status == 302) {
  r.return(reply.status, reply.headersOut['Location'])
  return
}
r.return(reply.status, reply.responseText)