Skip to content

Commit dd10717

Browse files
hugomnericallam
andauthored
feat: add hostname option to the dev command (#370)
* feat: add hostname option to the dev command - This commit adds a `hostname` option to the cli `dev` command, to allow the cli to point to nextjs applications running on different hostnames other than `localhost`. Example: the nextjs app was started using a --hostname 0.0.0.0 option to be able to be visible from inside docker containers. So adding --hostname 0.0.0.0 to `trigger-cli dev` would make it work. * docs: add dev cli command hostname option documentation * feat: add hostname option to the dev command - This commit adds a `hostname` option to the cli `dev` command, to allow the cli to point to nextjs applications running on different hostnames other than `localhost`. Example: the nextjs app was started using a --hostname 0.0.0.0 option to be able to be visible from inside docker containers. So adding --hostname 0.0.0.0 to `trigger-cli dev` would make it work. * docs: add dev cli command hostname option documentation * Update hip-coins-reply.md --------- Co-authored-by: Eric Allam <[email protected]>
1 parent 8bd1ca8 commit dd10717

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

.changeset/hip-coins-reply.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/cli": patch
3+
---
4+
5+
Added hostname option to the cli dev command

docs/documentation/guides/cli.mdx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,13 @@ yarn dlx @trigger.dev/cli@latest init
6060

6161
<Accordion title="Enter your development API key">
6262
To locate your development API key, login to the [Trigger.dev
63-
dashboard](https://cloud.trigger.dev) and select the Project you want to
64-
connect to. Then click on the Environments & API Keys tab in the left menu.
65-
You can copy your development API Key from the field at the top of this page.
66-
(Your development key will start with `tr_dev_`).
63+
dashboard](https://cloud.trigger.dev) and select the Project you want to connect to. Then click on
64+
the Environments & API Keys tab in the left menu. You can copy your development API Key from the
65+
field at the top of this page. (Your development key will start with `tr_dev_`).
6766
</Accordion>
6867

6968
<Accordion title="Enter a unique ID for your endpoint">
70-
Enter a custom ID or use the default by hitting enter. You can learn more
71-
about endpoints
69+
Enter a custom ID or use the default by hitting enter. You can learn more about endpoints
7270
[here](/documentation/concepts/environments-endpoints#endpoints).
7371
</Accordion>
7472

@@ -81,8 +79,8 @@ Once you're running your Next.js project locally, you can then execute the `dev`
8179
![Your first Job](/images/cli-dev.gif)
8280

8381
<Warning>
84-
Make sure your Next.js site is running locally before continuing. You must
85-
also leave this `dev` terminal command running while you develop.
82+
Make sure your Next.js site is running locally before continuing. You must also leave this `dev`
83+
terminal command running while you develop.
8684
</Warning>
8785

8886
In a **new terminal window or tab** run:
@@ -107,3 +105,7 @@ yarn dlx @trigger.dev/cli@latest dev
107105
You can optionally pass the port if you're not running on 3000 by adding
108106
`--port 3001` to the end
109107
</Note>
108+
<Note>
109+
You can optionally pass the hostname if you're not running on localhost by adding
110+
`--hostname <host>`. Example, in case your Next.js is running on 0.0.0.0: `--hostname 0.0.0.0`.
111+
</Note>

docs/documentation/guides/manual.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,9 @@ yarn dlx @trigger.dev/cli@latest dev
258258
You can optionally pass the port if you're not running on 3000 by adding
259259
`--port 3001` to the end
260260
</Note>
261+
<Note>
262+
You can optionally pass the hostname if you're not running on localhost by adding
263+
`--hostname <host>`. Example, in case your Next.js is running on 0.0.0.0: `--hostname 0.0.0.0`.
264+
</Note>
261265

262266
<Tip>If your existing Next.js project utilizes middleware and you encounter any issues, such as potential conflicts with Trigger.dev, it's recommended to refer to the troubleshooting guide at [Middleware](/documentation/guides/platforms/nextjs#middleware) for assistance. This guide can help you address any concerns related to middleware conflicts and ensure the smooth functioning of your project with Trigger.dev.</Tip>

packages/cli/src/cli/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ program
4242
.description("Tunnel your local Next.js project to Trigger.dev and start running jobs")
4343
.argument("[path]", "The path to the project", ".")
4444
.option("-p, --port <port>", "The local port your server is on", "3000")
45+
.option("-H, --hostname <hostname>", "Hostname on which the application is served", "localhost")
4546
.option("-e, --env-file <name>", "The name of the env file to load", ".env.local")
4647
.option(
4748
"-i, --client-id <name>",

packages/cli/src/commands/dev.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const asyncExecFile = util.promisify(childProcess.execFile);
1919

2020
export const DevCommandOptionsSchema = z.object({
2121
port: z.coerce.number(),
22+
hostname: z.string(),
2223
envFile: z.string(),
2324
handlerPath: z.string(),
2425
clientId: z.string().optional(),
@@ -72,7 +73,7 @@ export async function devCommand(path: string, anyOptions: any) {
7273

7374
logger.info(` [trigger.dev] Looking for Next.js site on port ${options.port}`);
7475

75-
const localEndpointHandlerUrl = `http://localhost:${options.port}${options.handlerPath}`;
76+
const localEndpointHandlerUrl = `http://${options.hostname}:${options.port}${options.handlerPath}`;
7677

7778
try {
7879
await fetch(localEndpointHandlerUrl, {
@@ -94,7 +95,7 @@ export async function devCommand(path: string, anyOptions: any) {
9495
telemetryClient.dev.serverRunning(path, options);
9596

9697
// Setup tunnel
97-
const endpointUrl = await resolveEndpointUrl(apiUrl, options.port);
98+
const endpointUrl = await resolveEndpointUrl(apiUrl, options.port, options.hostname);
9899
if (!endpointUrl) {
99100
telemetryClient.dev.failed("failed_to_create_tunnel", options);
100101
return;
@@ -279,11 +280,11 @@ export async function getTriggerApiDetails(path: string, envFile: string) {
279280
return { apiKey, apiUrl: apiUrl ?? CLOUD_API_URL, envFile: resolvedEnvFile.fileName };
280281
}
281282

282-
async function resolveEndpointUrl(apiUrl: string, port: number) {
283+
async function resolveEndpointUrl(apiUrl: string, port: number, hostname: string) {
283284
const apiURL = new URL(apiUrl);
284285

285286
if (apiURL.hostname === "localhost") {
286-
return `http://localhost:${port}`;
287+
return `http://${hostname}:${port}`;
287288
}
288289

289290
// Setup tunnel

0 commit comments

Comments
 (0)