Skip to content

Commit 8911f1b

Browse files
committed
Docs for custom build command
1 parent ef30f3c commit 8911f1b

File tree

3 files changed

+76
-20
lines changed

3 files changed

+76
-20
lines changed

README.md

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ When calling `open-next build`, OpenNext **runs `next build`** to build the Next
4444

4545
#### Building the Next.js app
4646

47-
OpenNext runs the `build` script in your `package.json` file. Depending on the lock file found in the app, the corresponding packager manager will be used. Either `npm run build`, `yarn build`, or `pnpm build` will be run.
47+
OpenNext runs the `build` script in your `package.json` file. Depending on the lock file found in the app, the corresponding packager manager will be used. Either `npm run build`, `yarn build`, or `pnpm build` will be run. For more on customizing the build command, see [overriding the build command](#custom-build-command).
4848

4949
#### Transforming the build output
5050

@@ -430,44 +430,62 @@ Vercel link: https://open-next.vercel.app
430430

431431
## Advanced usage
432432

433-
#### OPEN_NEXT_MINIFY
433+
#### Custom build command
434434

435-
Enabling this option will minimize all `.js` and `.json` files in the server function bundle using the [node-minify](https://github.com/srod/node-minify) library. This can reduce the size of the server function bundle by about 40%, depending on the size of your app. To enable it, simply run:
435+
OpenNext runs the `build` script in your `package.json` by default. However, you can specify a custom build command if required.
436436

437437
```bash
438-
OPEN_NEXT_MINIFY=true open-next build
438+
# CLI
439+
open-next build --build-command "pnpm custom:build"
439440
```
440441

441-
Enabling this option can significantly help to reduce the cold start time of the server function. However, it's an **experimental feature**, and you need to opt-in to use it. Once this option is thoroughly tested and found to be stable, it will be enabled by default.
442+
```ts
443+
// JS
444+
import { build } from "open-next/build.js";
442445

443-
## Debugging
446+
await build({
447+
buildCommand: "pnpm custom:build",
448+
});
449+
```
444450

445-
#### Function logs
451+
#### Minify server function
446452

447-
To find the **server, image optimization, and warmer log**, go to the AWS CloudWatch console in the **region you deployed to**.
453+
Enabling this option will minimize all `.js` and `.json` files in the server function bundle using the [node-minify](https://github.com/srod/node-minify) library. This can reduce the size of the server function bundle by about 40%, depending on the size of your app.
448454

449-
If the server function is **deployed to Lambda@Edge**, the logs will appear in the **region you are physically close to**. For example, if you deployed your app to `us-east-1` and you are visiting the app from in London, the logs are likely to be in `eu-west-2`.
450-
451-
#### Warmer function logs
455+
```bash
456+
# CLI
457+
open-next build --minify
458+
```
452459

453-
The logs from the warmer function provide insights into the results of the warming process.
460+
```ts
461+
// JS
462+
import { build } from "open-next/build.js";
454463

455-
```
456-
{ event: 'warmer result', sent: 2, success: 2, uniqueServersWarmed: 2 }
464+
await build({
465+
minify: true,
466+
});
457467
```
458468

459-
- `sent` — The number of times the warmer invoked the server function using the Lambda SDK. This value should correspond to the `CONCURRENCY` set in the warmer function.
460-
- `success` — The number of SDK calls that returned a 200 status code, indicating successful invocations.
461-
- `uniqueServersWarmed` — This helps track any instances that responded unusually quickly and served multiple warming requests. As all SDK calls are made concurrently using `await Promise.all()`, this metric is useful for monitoring the number of unique warmed instances.
469+
This feature is currently **experimental** and needs to be opted into. It can significantly decrease the server function's cold start time. Once it is thoroughly tested and its stability is confirmed, it will be enabled by default.
462470

463471
#### Debug mode
464472

465-
You can run OpenNext in debug mode by setting the `OPEN_NEXT_DEBUG` environment variable:
473+
OpenNext can be executed in debug mode for bug tracking purposes.
466474

467475
```bash
476+
# CLI
468477
OPEN_NEXT_DEBUG=true npx open-next@latest build
469478
```
470479

480+
```ts
481+
// JS
482+
import { build } from "open-next/build.js";
483+
484+
await build({
485+
debug: true,
486+
});
487+
```
488+
471489
This does a few things:
472490

473491
1. Lambda handler functions in the build output will not be minified.
@@ -479,6 +497,26 @@ It is recommended to **turn off debug mode when building for production** becaus
479497
1. Un-minified function code is 2-3X larger than minified code. This will result in longer Lambda cold start times.
480498
1. Logging the event object on each request can result in a lot of logs being written to AWS CloudWatch. This will result in increased AWS costs.
481499

500+
## Debugging
501+
502+
#### Function logs
503+
504+
To find the **server, image optimization, and warmer log**, go to the AWS CloudWatch console in the **region you deployed to**.
505+
506+
If the server function is **deployed to Lambda@Edge**, the logs will appear in the **region you are physically close to**. For example, if you deployed your app to `us-east-1` and you are visiting the app from in London, the logs are likely to be in `eu-west-2`.
507+
508+
#### Warmer function logs
509+
510+
The logs from the warmer function provide insights into the results of the warming process.
511+
512+
```
513+
{ event: 'warmer result', sent: 2, success: 2, uniqueServersWarmed: 2 }
514+
```
515+
516+
- `sent` — The number of times the warmer invoked the server function using the Lambda SDK. This value should correspond to the `CONCURRENCY` set in the warmer function.
517+
- `success` — The number of SDK calls that returned a 200 status code, indicating successful invocations.
518+
- `uniqueServersWarmed` — This helps track any instances that responded unusually quickly and served multiple warming requests. As all SDK calls are made concurrently using `await Promise.all()`, this metric is useful for monitoring the number of unique warmed instances.
519+
482520
#### Opening an issue
483521

484522
To help diagnose issues, it's always helpful to provide a reproducible setup when opening an issue. One easy way to do this is to create a pull request (PR) and add a new page to the [benchmark app](#example) located in the `example` folder, which reproduces the issue. The PR will automatically deploy the app to AWS.

packages/open-next/src/build.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,26 @@ import { buildSync, BuildOptions as ESBuildOptions } from "esbuild";
77
import { createRequire as topLevelCreateRequire } from "node:module";
88

99
interface BuildOptions {
10+
/**
11+
* Minify the server bundle.
12+
* @default false
13+
*/
1014
minify?: boolean;
15+
/**
16+
* Print debug information.
17+
* @default false
18+
*/
1119
debug?: boolean;
12-
appPath?: string;
20+
/**
21+
* The command to build the Next.js app.
22+
* @default `npm run build`, `yarn build`, or `pnpm build` based on the lock file found in the app's directory or any of its parent directories.
23+
* @example
24+
* ```ts
25+
* build({
26+
* buildCommand: "pnpm custom:build",
27+
* });
28+
* ```
29+
*/
1330
buildCommand?: string;
1431
}
1532

@@ -49,7 +66,7 @@ export async function build(opts: BuildOptions = {}) {
4966
}
5067

5168
function normalizeOptions(opts: BuildOptions) {
52-
const appPath = opts.appPath ?? process.cwd();
69+
const appPath = process.cwd();
5370
const outputDir = ".open-next";
5471
return {
5572
appPath,

packages/open-next/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ if (Object.keys(args).includes("--help")) printHelp();
1010

1111
build({
1212
buildCommand: args["--build-command"],
13+
minify: Object.keys(args).includes("--minify"),
1314
});
1415

1516
function parseArgs() {

0 commit comments

Comments
 (0)