From 33a03e94a6960664843ee40588f069be4f99d136 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Mon, 13 May 2024 09:40:18 -0400 Subject: [PATCH] fix(nextjs): additional experimental HTTPS flags --- .../packages/next/executors/server.json | 12 +++++ .../next/src/executors/server/schema.json | 12 +++++ .../next/src/executors/server/server.impl.ts | 46 +++++++++++-------- packages/next/src/utils/types.ts | 3 ++ 4 files changed, 54 insertions(+), 19 deletions(-) diff --git a/docs/generated/packages/next/executors/server.json b/docs/generated/packages/next/executors/server.json index f63fca1e2daf76..ac191a5d16d9da 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 4dc1b5b3d14f94..4b0a3a3e1610f0 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 68e298a97d258b..b9b2e7cb139032 100644 --- a/packages/next/src/executors/server/server.impl.ts +++ b/packages/next/src/executors/server/server.impl.ts @@ -1,14 +1,7 @@ -import { - ExecutorContext, - parseTargetString, - readTargetOptions, -} from '@nx/devkit'; -import { join, resolve } from 'path'; +import { ExecutorContext, parseTargetString, readTargetOptions } from '@nx/devkit'; +import { resolve } from 'path'; -import { - NextBuildBuilderOptions, - NextServeBuilderOptions, -} from '../../utils/types'; +import { NextBuildBuilderOptions, NextServeBuilderOptions } from '../../utils/types'; import { fork } from 'child_process'; import customServer from './custom-server.impl'; import { createCliOptions } from '../../utils/create-cli-options'; @@ -39,8 +32,8 @@ export default async function* serveExecutor( (process.env as any).NODE_ENV = process.env.NODE_ENV ? process.env.NODE_ENV : options.dev - ? 'development' - : 'production'; + ? 'development' + : 'production'; // Setting port that the custom server should use. process.env.PORT = options.port ? `${options.port}` : process.env.PORT; @@ -54,16 +47,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) { @@ -87,8 +82,21 @@ export default async function* serveExecutor( next({ success: true, - baseUrl: `http://${options.hostname ?? 'localhost'}:${options.port}`, + baseUrl: `http://${options.hostname ?? 'localhost'}:${options.port}` }); } ); } + +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 00be1509a87d9e..63696f6007cdb5 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; }