diff --git a/src/lib/functions/registry.mjs b/src/lib/functions/registry.mjs index 788f7ea0414..fb1b5040026 100644 --- a/src/lib/functions/registry.mjs +++ b/src/lib/functions/registry.mjs @@ -24,7 +24,7 @@ import { getPathInProject } from '../settings.mjs' import NetlifyFunction from './netlify-function.mjs' import runtimes from './runtimes/index.mjs' -const DEFAULT_URL_EXPRESSION = /^\/.netlify\/(functions|builders)\/([^/]+).*/ +export const DEFAULT_FUNCTION_URL_EXPRESSION = /^\/.netlify\/(functions|builders)\/([^/]+).*/ const TYPES_PACKAGE = '@netlify/functions' const ZIP_EXTENSION = '.zip' @@ -238,7 +238,7 @@ export class FunctionsRegistry { // the incoming URL. It doesn't really matter that we don't have the actual // local URL with the correct port. const urlPath = new URL(url, 'http://localhost').pathname - const defaultURLMatch = urlPath.match(DEFAULT_URL_EXPRESSION) + const defaultURLMatch = urlPath.match(DEFAULT_FUNCTION_URL_EXPRESSION) if (defaultURLMatch) { const func = this.get(defaultURLMatch[2]) diff --git a/src/utils/proxy.mjs b/src/utils/proxy.mjs index cbf20a17afc..e04df689fab 100644 --- a/src/utils/proxy.mjs +++ b/src/utils/proxy.mjs @@ -27,6 +27,7 @@ import { isEdgeFunctionsRequest, } from '../lib/edge-functions/proxy.mjs' import { fileExistsAsync, isFileAsync } from '../lib/fs.mjs' +import { DEFAULT_FUNCTION_URL_EXPRESSION } from '../lib/functions/registry.mjs' import renderErrorTemplate from '../lib/render-error-template.mjs' import { NETLIFYDEVLOG, NETLIFYDEVWARN, log, chalk } from './command-helpers.mjs' @@ -87,7 +88,7 @@ function isInternal(url) { * @param {string} url */ function isFunction(functionsPort, url) { - return functionsPort && url.match(/^\/.netlify\/(functions|builders)\/.+/) + return functionsPort && url.match(DEFAULT_FUNCTION_URL_EXPRESSION) } /** @@ -328,13 +329,12 @@ const serveRedirect = async function ({ env, functionsRegistry, match, options, return proxy.web(req, res, { target: options.functionsServer }) } - const functionWithCustomRoute = - functionsRegistry && (await functionsRegistry.getFunctionForURLPath(destURL, req.method)) + const matchingFunction = functionsRegistry && (await functionsRegistry.getFunctionForURLPath(destURL, req.method)) const destStaticFile = await getStatic(dest.pathname, options.publicFolder) let statusValue if ( match.force || - (!staticFile && ((!options.framework && destStaticFile) || isInternal(destURL) || functionWithCustomRoute)) + (!staticFile && ((!options.framework && destStaticFile) || isInternal(destURL) || matchingFunction)) ) { req.url = destStaticFile ? destStaticFile + dest.search : destURL const { status } = match @@ -342,10 +342,11 @@ const serveRedirect = async function ({ env, functionsRegistry, match, options, console.log(`${NETLIFYDEVLOG} Rewrote URL to`, req.url) } - if (isFunction(options.functionsPort, req.url) || functionWithCustomRoute) { - const functionHeaders = functionWithCustomRoute - ? { [NFFunctionName]: functionWithCustomRoute.func.name, [NFFunctionRoute]: functionWithCustomRoute.route } - : {} + if (matchingFunction) { + const functionHeaders = { + [NFFunctionName]: matchingFunction.func.name, + [NFFunctionRoute]: matchingFunction.route, + } const url = reqToURL(req, originalURL) req.headers['x-netlify-original-pathname'] = url.pathname req.headers['x-netlify-original-search'] = url.search