Skip to content

Commit

Permalink
Merge pull request #547 from netlify/raees/redirects-headers
Browse files Browse the repository at this point in the history
Dev: Allow custom headers on rewrites
  • Loading branch information
RaeesBhatti committed Sep 19, 2019
2 parents 5632cbc + 292dae6 commit be6dc8b
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/commands/dev/index.js
Expand Up @@ -9,6 +9,7 @@ const waitPort = require('wait-port')
const getPort = require('get-port')
const chokidar = require('chokidar')
const proxyMiddleware = require('http-proxy-middleware')
const isEmpty = require('lodash.isempty')
const { serveFunctions } = require('../../utils/serve-functions')
const { serverSettings } = require('../../utils/detect-server')
const { detectFunctionsBuilder } = require('../../utils/detect-functions-builder')
Expand Down Expand Up @@ -133,7 +134,16 @@ function initializeProxy(port) {

req.url = dest.pathname + dest.search
console.log(`${NETLIFYDEVLOG} Rewrote URL to `, req.url)
return handlers.web(req, res, Object.assign({}, req.proxyOptions, { status: match.status } ))

if (isFunction({ functionsPort: req.proxyOptions.functionsPort }, req)) {
return proxy.web(req, res, { target: req.proxyOptions.functionsServer })
}
const addonUrl = addonUrl(req.proxyOptions.addonUrls, req)
if (addonUrl) {
return proxy.web(req, res, { target: addonUrl })
}

return proxy.web(req, res, Object.assign({}, req.proxyOptions, { status: match.status } ))
}
}
}
Expand Down Expand Up @@ -181,6 +191,10 @@ async function startProxy(settings, addonUrls) {
}

rewriter(req, res, (match) => {
if (match && !isEmpty(match.proxyHeaders)) {
Object.entries(match.proxyHeaders).forEach(([k,v]) => req.headers[k] = v)
}

if (isFunction(settings, req)) {
return proxy.web(req, res, { target: functionsServer })
}
Expand All @@ -189,7 +203,13 @@ async function startProxy(settings, addonUrls) {
return proxy.web(req, res, { target: url })
}

proxy.web(req, res, { target: `http://localhost:${settings.proxyPort}`, match, publicFolder: settings.dist })
proxy.web(req, res, {
target: `http://localhost:${settings.proxyPort}`,
match, publicFolder: settings.dist,
functionsServer,
functionsPort: settings.functionsPort,
addonUrls,
})
})
})

Expand Down

0 comments on commit be6dc8b

Please sign in to comment.