Skip to content

Commit efd5909

Browse files
Feat: Detect if a Next.js project is running on the specified port before opening a tunnel (#214)
* Feat: Detect nextjs project * Add check before opening tunnel * Update dev.ts * Add changeset
1 parent 463cbed commit efd5909

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

.changeset/blue-monkeys-speak.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+
Detect if a nextjs project is running on the specified port before opening tunnel

packages/cli/src/commands/dev.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { logger } from "../utils/logger.js";
99
import { resolvePath } from "../utils/parseNameAndPath.js";
1010
import { TriggerApi } from "../utils/triggerApi.js";
1111
import dotenv from "dotenv";
12+
import fetch from "node-fetch";
1213

1314
export const DevCommandOptionsSchema = z.object({
1415
port: z.coerce.number(),
@@ -48,7 +49,7 @@ export async function devCommand(path: string, anyOptions: any) {
4849
logger.success(`✔️ [trigger.dev] Detected TriggerClient id: ${endpointId}`);
4950

5051
// Read from .env.local or .env to get the TRIGGER_API_KEY and TRIGGER_API_URL
51-
const { apiUrl, envFile } = await getTriggerApiDetails(
52+
const { apiUrl, envFile, apiKey } = await getTriggerApiDetails(
5253
resolvedPath,
5354
options.envFile
5455
);
@@ -59,9 +60,24 @@ export async function devCommand(path: string, anyOptions: any) {
5960
` [trigger.dev] Looking for Next.js site on port ${options.port}`
6061
);
6162

63+
const localEndpointHandlerUrl = `http://localhost:${options.port}${options.handlerPath}`;
64+
65+
try {
66+
await fetch(localEndpointHandlerUrl, {
67+
method: "HEAD",
68+
headers: {
69+
"x-trigger-api-key": apiKey,
70+
"x-trigger-action": "PING",
71+
"x-trigger-endpoint-id": endpointId,
72+
},
73+
});
74+
} catch (err) {
75+
logger.error(`❌ [trigger.dev] No server found on port ${options.port}.`);
76+
process.exit(1);
77+
}
78+
6279
// Setup tunnel
6380
const endpointUrl = await resolveEndpointUrl(apiUrl, options.port);
64-
6581
const endpointHandlerUrl = `${endpointUrl}${options.handlerPath}`;
6682

6783
const connectingSpinner = ora(

0 commit comments

Comments
 (0)