diff --git a/src/lib/edge-functions/proxy.js b/src/lib/edge-functions/proxy.js index f8a0ce7f805..3a4ecc2faed 100644 --- a/src/lib/edge-functions/proxy.js +++ b/src/lib/edge-functions/proxy.js @@ -5,7 +5,7 @@ const { cwd, env } = require('process') const getAvailablePort = require('get-port') const { v4: generateUUID } = require('uuid') -const { NETLIFYDEVERR, NETLIFYDEVWARN, chalk, error, log } = require('../../utils/command-helpers') +const { NETLIFYDEVERR, NETLIFYDEVWARN, chalk, error: printError, log } = require('../../utils/command-helpers') const { getGeoLocation } = require('../geo-location') const { getPathInProject } = require('../settings') const { startSpinner, stopSpinner } = require('../spinner') @@ -77,24 +77,18 @@ const initializeProxy = async ({ projectDir, }) const hasEdgeFunctions = userFunctionsPath !== undefined || internalFunctions.length !== 0 - let hasServerError = false return async (req) => { - if (req.headers[headers.Passthrough] !== undefined || !hasEdgeFunctions || hasServerError) { + if (req.headers[headers.Passthrough] !== undefined || !hasEdgeFunctions) { return } - let promiseResult + const [geoLocation, registry] = await Promise.all([ + getGeoLocation({ mode: geolocationMode, offline, state }), + server, + ]) - try { - promiseResult = await Promise.all([getGeoLocation({ mode: geolocationMode, offline, state }), server]) - } catch (error_) { - error(error_.message, { exit: false }) - hasServerError = true - return - } - - const [geoLocation, { registry }] = promiseResult + if (!registry) return // Setting header with geolocation. req.headers[headers.Geo] = JSON.stringify(geoLocation) @@ -152,34 +146,39 @@ const prepareServer = async ({ port, projectDir, }) => { - const bundler = await import('@netlify/edge-bundler') - const distImportMapPath = getPathInProject([DIST_IMPORT_MAP_PATH]) - const runIsolate = await bundler.serve({ - ...getDownloadUpdateFunctions(), - certificatePath, - debug: env.NETLIFY_DENO_DEBUG === 'true', - distImportMapPath, - formatExportTypeError: (name) => - `${NETLIFYDEVERR} ${chalk.red('Failed')} to load Edge Function ${chalk.yellow( - name, - )}. The file does not seem to have a function as the default export.`, - formatImportError: (name) => `${NETLIFYDEVERR} ${chalk.red('Failed')} to run Edge Function ${chalk.yellow(name)}:`, - importMaps, - inspectSettings, - port, - }) - const registry = new EdgeFunctionsRegistry({ - bundler, - config, - configPath, - directories, - getUpdatedConfig, - internalFunctions, - projectDir, - runIsolate, - }) + try { + const bundler = await import('@netlify/edge-bundler') + const distImportMapPath = getPathInProject([DIST_IMPORT_MAP_PATH]) + const runIsolate = await bundler.serve({ + ...getDownloadUpdateFunctions(), + certificatePath, + debug: env.NETLIFY_DENO_DEBUG === 'true', + distImportMapPath, + formatExportTypeError: (name) => + `${NETLIFYDEVERR} ${chalk.red('Failed')} to load Edge Function ${chalk.yellow( + name, + )}. The file does not seem to have a function as the default export.`, + formatImportError: (name) => + `${NETLIFYDEVERR} ${chalk.red('Failed')} to run Edge Function ${chalk.yellow(name)}:`, + importMaps, + inspectSettings, + port, + }) + const registry = new EdgeFunctionsRegistry({ + bundler, + config, + configPath, + directories, + getUpdatedConfig, + internalFunctions, + projectDir, + runIsolate, + }) - return { registry, runIsolate } + return registry + } catch (error) { + printError(error.message, { exit: false }) + } } module.exports = { handleProxyRequest, initializeProxy, isEdgeFunctionsRequest }