Skip to content

Commit

Permalink
fix(nextjs): additional experimental HTTPS flags
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed May 17, 2024
1 parent d289134 commit 33a03e9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
12 changes: 12 additions & 0 deletions docs/generated/packages/next/executors/server.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
12 changes: 12 additions & 0 deletions packages/next/src/executors/server/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
46 changes: 27 additions & 19 deletions packages/next/src/executors/server/server.impl.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
}
3 changes: 3 additions & 0 deletions packages/next/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export interface NextServeBuilderOptions {
keepAliveTimeout?: number;
turbo?: boolean;
experimentalHttps?: boolean;
experimentalHttpsKey?: string;
experimentalHttpsCert?: string;
experimentalHttpsCa?: string;
customServerHttps?: boolean;
}

Expand Down

0 comments on commit 33a03e9

Please sign in to comment.