diff --git a/src/commands/dev/index.js b/src/commands/dev/index.js index 7ad45c5778a..a6fb472e6bd 100644 --- a/src/commands/dev/index.js +++ b/src/commands/dev/index.js @@ -136,7 +136,7 @@ const FRAMEWORK_PORT_TIMEOUT = 6e5 const startProxyServer = async ({ flags, settings, site, log, exit, addonsUrls }) => { let url - if (flags.trafficMesh) { + if (flags.edgeHandlers || flags.trafficMesh) { url = await startForwardProxy({ port: settings.port, frameworkPort: settings.frameworkPort, @@ -223,6 +223,12 @@ class DevCommand extends Command { ...flags, } + if (flags.trafficMesh) { + warn( + '--trafficMesh and -t are deprecated and will be removed in the near future. Please use --edgeHandlers or -e instead.', + ) + } + await injectEnvVariables({ env: this.netlify.cachedConfig.env, log, site, warn }) const { addonsUrls, siteUrl, capabilities } = await getSiteInformation({ @@ -311,10 +317,15 @@ DevCommand.flags = { char: 'l', description: 'start a public live session', }), + edgeHandlers: flagsLib.boolean({ + char: 'e', + hidden: true, + description: 'activates the Edge Handlers runtime', + }), trafficMesh: flagsLib.boolean({ char: 't', hidden: true, - description: 'uses Traffic Mesh for proxying requests', + description: '(DEPRECATED: use --edgeHandlers or -e instead) uses Traffic Mesh for proxying requests', }), locationDb: flagsLib.string({ description: 'specify the path to a local GeoIP location database in MMDB format', diff --git a/tests/command.dev.test.js b/tests/command.dev.test.js index 137b6f097fc..2285b3c4651 100644 --- a/tests/command.dev.test.js +++ b/tests/command.dev.test.js @@ -32,7 +32,7 @@ const testMatrix = [ { args: [] }, // some tests are still failing with this enabled - // { args: ['--trafficMesh'] } + // { args: ['--edgeHandlers'] } ] const testName = (title, args) => (args.length <= 0 ? title : `${title} - ${args.join(' ')}`) @@ -1210,7 +1210,61 @@ testMatrix.forEach(({ args }) => { const version = Number.parseInt(process.version.slice(1).split('.')[0]) const EDGE_HANDLER_MIN_VERSION = 10 if (version >= EDGE_HANDLER_MIN_VERSION) { - test(testName('should serve edge handlers', args), async (t) => { + test(testName('should serve edge handlers with --edgeHandlers flag', args), async (t) => { + await withSiteBuilder('site-with-fully-qualified-redirect-rule', async (builder) => { + const publicDir = 'public' + builder + .withNetlifyToml({ + config: { + build: { publish: publicDir }, + redirects: [ + { + from: '/edge-handler', + to: 'index.html', + status: 200, + edge_handler: 'smoke', + force: true, + }, + ], + }, + }) + .withContentFiles([ + { + path: path.join(publicDir, 'index.html'), + content: 'index', + }, + ]) + .withEdgeHandlers({ + fileName: 'smoke.js', + handlers: { + onRequest: (event) => { + event.replaceResponse( + // eslint-disable-next-line no-undef + new Response(null, { + headers: { + Location: 'https://google.com/', + }, + status: 301, + }), + ) + }, + }, + }) + + await builder.buildAsync() + + await withDevServer({ cwd: builder.directory, args: [...args, '--edgeHandlers'] }, async (server) => { + const response = await got(`${server.url}/edge-handler`, { + followRedirect: false, + }) + + t.is(response.statusCode, 301) + t.is(response.headers.location, 'https://google.com/') + }) + }) + }) + + test(testName('should serve edge handlers with deprecated --trafficMesh flag', args), async (t) => { await withSiteBuilder('site-with-fully-qualified-redirect-rule', async (builder) => { const publicDir = 'public' builder @@ -1282,7 +1336,7 @@ testMatrix.forEach(({ args }) => { await builder.buildAsync() - await withDevServer({ cwd: builder.directory, args: [...args, '--trafficMesh'] }, async (server) => { + await withDevServer({ cwd: builder.directory, args: [...args, '--edgeHandlers'] }, async (server) => { const response = await got(`${server.url}/index.html`) t.is(response.statusCode, 200)