On one project https://dev.testproject.com we have tons of proxied pages with upstream http://upstream.testproject.com
Few of pages return location
header with absolute url starts with http://upstream.testproject.com
How could we change location
header to relative one in proxy
rule for router?
Seems like we have solution for that
.match({ path: '/your-path-here' }, async res => {
cacheResponse(NO_CACHE)
await res.proxy('legacy', {
transformResponse: res => {
const locationHeader = res.getHeaders().location
if (res.statusCode === 302 && locationHeader) {
res.setHeader(
'location',
locationHeader.replace(
/^(?:https?:\/\/)?(?:www\.)?((?:(?!www\.|\.).)+\.[a-zA-Z0-9.]+)/g,
''
)
)
}
},
})
})
@freewayspb That code will run every request through serverless, which will be unnecessary and costly. Instead you can use a regex to refine the location header at the edge. See https://docs.layer0.co/docs/api/core/classes/router_responsewriter.responsewriter.html#updateresponseheader
new Router()
.get('/some/path', ({ updateResponseHeader, proxy }) => {
proxy('origin')
updateResponseHeader('some-header', /some-.*-part/gi, 'some-replacement')
})