Skip to content

Commit

Permalink
fix: use original event.path when redirecting to Functions (#3316)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Sep 10, 2021
1 parent d9b2ae7 commit 26a20a7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/utils/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const serveRedirect = async function ({ req, res, proxy, match, options }) {
return handleAddonUrl({ req, res, addonUrl: urlForAddons })
}

const originalURL = req.url
if (match.exceptions && match.exceptions.JWT) {
// Some values of JWT can start with :, so, make sure to normalize them
const expectedRoles = new Set(
Expand All @@ -140,7 +141,6 @@ const serveRedirect = async function ({ req, res, proxy, match, options }) {
const token = cookieValues.nf_jwt

// Serve not found by default
const originalURL = req.url
req.url = '/.netlify/non-existent-path'

if (token) {
Expand Down Expand Up @@ -173,12 +173,7 @@ const serveRedirect = async function ({ req, res, proxy, match, options }) {
}
}

const reqUrl = new URL(
req.url,
`${req.protocol || (req.headers.scheme && `${req.headers.scheme}:`) || 'http:'}//${
req.headers.host || req.hostname
}`,
)
const reqUrl = reqToURL(req, req.url)

const staticFile = await getStatic(decodeURIComponent(reqUrl.pathname), options.publicFolder)
if (staticFile) {
Expand Down Expand Up @@ -247,7 +242,7 @@ const serveRedirect = async function ({ req, res, proxy, match, options }) {
}

if (isFunction(options.functionsPort, req.url)) {
req.headers['x-netlify-original-pathname'] = reqUrl.pathname
req.headers['x-netlify-original-pathname'] = reqToURL(req, originalURL).pathname
return proxy.web(req, res, { target: options.functionsServer })
}
const addonUrl = getAddonUrl(options.addonsUrls, req)
Expand All @@ -261,6 +256,15 @@ const serveRedirect = async function ({ req, res, proxy, match, options }) {
return proxy.web(req, res, options)
}

const reqToURL = function (req, pathname) {
return new URL(
pathname,
`${req.protocol || (req.headers.scheme && `${req.headers.scheme}:`) || 'http:'}//${
req.headers.host || req.hostname
}`,
)
}

const MILLISEC_TO_SEC = 1e3

const initializeProxy = async function ({ port, distDir, projectDir, configPath }) {
Expand Down

1 comment on commit 26a20a7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 329 MB

Please sign in to comment.