From 5c70c37751c1e6eca1d484a99f82e627ffb176d7 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Fri, 17 May 2024 14:50:47 -0400 Subject: [PATCH] fix(nextjs): additional experimental HTTPS options (#23334) There are three additional flags if user wishes to generate their own key, cert, ca files rather than the auto-generated ones by Next.js. ## Current Behavior Cannot pass additional CLI options. ## Expected Behavior Can pass additional CLI options for custom files. ## Related Issue(s) Closes https://github.com/nrwl/nx/discussions/23331 (cherry picked from commit 53345f22418c04b52dbe1f9ddb2cc577572c38c2) --- .../packages/next/executors/server.json | 12 ++++++++ .../next/src/executors/server/schema.json | 12 ++++++++ .../next/src/executors/server/server.impl.ts | 29 ++++++++++++++----- packages/next/src/utils/types.ts | 3 ++ 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/docs/generated/packages/next/executors/server.json b/docs/generated/packages/next/executors/server.json index f63fca1e2daf7..ac191a5d16d9d 100644 --- a/docs/generated/packages/next/executors/server.json +++ b/docs/generated/packages/next/executors/server.json @@ -60,6 +60,18 @@ "type": "boolean", "description": "Enable HTTPS support for the Next.js development server." }, + "experimentalHttpsKey": { + "type": "string", + "description": "Path to a HTTPS key file." + }, + "experimentalHttpsCert": { + "type": "string", + "description": "Path to a HTTPS certificate file." + }, + "experimentalHttpsCa": { + "type": "string", + "description": "Path to a HTTPS certificate authority file." + }, "customServerHttps:": { "type": "boolean", "description": "Enable HTTPS support for the custom server." diff --git a/packages/next/src/executors/server/schema.json b/packages/next/src/executors/server/schema.json index 4dc1b5b3d14f9..4b0a3a3e1610f 100644 --- a/packages/next/src/executors/server/schema.json +++ b/packages/next/src/executors/server/schema.json @@ -57,6 +57,18 @@ "type": "boolean", "description": "Enable HTTPS support for the Next.js development server." }, + "experimentalHttpsKey": { + "type": "string", + "description": "Path to a HTTPS key file." + }, + "experimentalHttpsCert": { + "type": "string", + "description": "Path to a HTTPS certificate file." + }, + "experimentalHttpsCa": { + "type": "string", + "description": "Path to a HTTPS certificate authority file." + }, "customServerHttps:": { "type": "boolean", "description": "Enable HTTPS support for the custom server." diff --git a/packages/next/src/executors/server/server.impl.ts b/packages/next/src/executors/server/server.impl.ts index 68e298a97d258..1745b09f71074 100644 --- a/packages/next/src/executors/server/server.impl.ts +++ b/packages/next/src/executors/server/server.impl.ts @@ -3,7 +3,7 @@ import { parseTargetString, readTargetOptions, } from '@nx/devkit'; -import { join, resolve } from 'path'; +import { resolve } from 'path'; import { NextBuildBuilderOptions, @@ -54,16 +54,18 @@ export default async function* serveExecutor( const mode = options.dev ? 'dev' : 'start'; const turbo = options.turbo && options.dev ? '--turbo' : ''; - const experimentalHttps = - options.experimentalHttps && options.dev ? '--experimental-https' : ''; const nextBin = require.resolve('next/dist/bin/next'); yield* createAsyncIterable<{ success: boolean; baseUrl: string }>( async ({ done, next, error }) => { - const server = fork(nextBin, [mode, ...args, turbo, experimentalHttps], { - cwd: options.dev ? projectRoot : nextDir, - stdio: 'inherit', - }); + const server = fork( + nextBin, + [mode, ...args, turbo, ...getExperimentalHttpsFlags(options)], + { + cwd: options.dev ? projectRoot : nextDir, + stdio: 'inherit', + } + ); server.once('exit', (code) => { if (code === 0) { @@ -92,3 +94,16 @@ export default async function* serveExecutor( } ); } + +function getExperimentalHttpsFlags(options: NextServeBuilderOptions): string[] { + if (!options.dev) return []; + const flags: string[] = []; + if (options.experimentalHttps) flags.push('--experimental-https'); + if (options.experimentalHttpsKey) + flags.push(`--experimental-https-key=${options.experimentalHttpsKey}`); + if (options.experimentalHttpsCert) + flags.push(`--experimental-https-cert=${options.experimentalHttpsCert}`); + if (options.experimentalHttpsCa) + flags.push(`--experimental-https-ca=${options.experimentalHttpsCa}`); + return flags; +} diff --git a/packages/next/src/utils/types.ts b/packages/next/src/utils/types.ts index 00be1509a87d9..63696f6007cdb 100644 --- a/packages/next/src/utils/types.ts +++ b/packages/next/src/utils/types.ts @@ -53,6 +53,9 @@ export interface NextServeBuilderOptions { keepAliveTimeout?: number; turbo?: boolean; experimentalHttps?: boolean; + experimentalHttpsKey?: string; + experimentalHttpsCert?: string; + experimentalHttpsCa?: string; customServerHttps?: boolean; }