Skip to content

Commit 276bddd

Browse files
fix(webapp): refactor dev server URL detection @W-21331028@ (#30)
* fix: refactor dev server URL detection * fix: add synchronize to pr-validation trigger
1 parent fc1eb1e commit 276bddd

9 files changed

Lines changed: 371 additions & 358 deletions

File tree

.github/workflows/validate-pr.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ name: pr-validation
22

33
on:
44
pull_request:
5-
types: [opened, reopened, edited]
6-
# only applies to PRs that want to merge to main
5+
types: [opened, reopened, edited, synchronize]
76
branches: [main]
87

98
jobs:

README.md

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ sf plugins
129129

130130
Start a local development proxy server for webapp development with Salesforce authentication.
131131

132+
**Two operating modes:**
133+
134+
- **Command mode** (default): When `dev.command` is set in `webapplication.json` (or default `npm run dev`), the CLI starts the dev server. URL defaults to `http://localhost:5173`; override with `dev.url` or `--url` if needed.
135+
- **URL-only mode**: When only `dev.url` or `--url` is provided (no command), the CLI assumes the dev server is already running and does not start it. Proxy only.
136+
132137
```bash
133138
USAGE
134139
$ sf webapp dev --name <webapp-name> --target-org <org-alias> [options]
@@ -138,50 +143,29 @@ REQUIRED FLAGS
138143
-o, --target-org=<value> Salesforce org to authenticate against
139144

140145
OPTIONAL FLAGS
141-
-u, --url=<value> Dev server URL (overrides webapplication.json)
146+
-u, --url=<value> Dev server URL. Command mode: override default 5173. URL-only: required (server must be running)
142147
-p, --port=<value> Proxy server port (default: 4545)
143148
--open Open browser automatically
144149

145-
GLOBAL FLAGS
146-
--flags-dir=<value> Import flag values from a directory
147-
--json Format output as json
148-
149150
DESCRIPTION
150-
Start a local development proxy server for webapp development.
151-
152-
This command starts a local HTTP proxy server that handles Salesforce
153-
authentication and routes requests between your local dev server and
154-
Salesforce APIs. It automatically spawns and monitors your dev server,
155-
detects the URL, and provides health monitoring.
151+
Starts a local HTTP proxy that injects Salesforce authentication and routes
152+
requests between your dev server and Salesforce APIs. In command mode,
153+
spawns and monitors the dev server (default URL: localhost:5173). In
154+
URL-only mode, connects to an already-running dev server.
156155

157156
EXAMPLES
158-
Start proxy with automatic dev server management:
157+
Command mode (CLI starts dev server, default port 5173):
159158

160159
$ sf webapp dev --name myapp --target-org myorg --open
161160

162-
Use existing dev server:
161+
URL-only mode (dev server already running):
163162

164163
$ sf webapp dev --name myapp --target-org myorg --url http://localhost:5173 --open
165164

166-
Use custom proxy port:
165+
Custom proxy port:
167166

168167
$ sf webapp dev --name myapp --target-org myorg --port 8080 --open
169168

170-
SUPPORTED DEV SERVERS
171-
- Vite
172-
- Create React App (Webpack)
173-
- Next.js
174-
- Any server that outputs http://localhost:PORT
175-
176-
FEATURES
177-
- Automatic Salesforce authentication injection
178-
- Intelligent request routing (Salesforce vs dev server)
179-
- WebSocket support for Hot Module Replacement (HMR)
180-
- Beautiful HTML error pages with auto-refresh
181-
- Periodic health monitoring (every 5s)
182-
- Configuration file watching (webapplication.json)
183-
- Graceful shutdown on Ctrl+C
184-
185169
SEE ALSO
186170
- Complete Guide: SF_WEBAPP_DEV_GUIDE.md
187171
```

SF_WEBAPP_DEV_GUIDE.md

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -234,38 +234,43 @@ Browser → Proxy → [Auth Headers Injected] → Salesforce → Response
234234

235235
## Configuration
236236

237-
### webapplication.json Schema
237+
### Dev Server URL Resolution
238238

239-
The `webapplication.json` file is **optional**. All fields are also optional - missing fields use defaults.
239+
The command operates in two distinct modes based on configuration:
240240

241-
#### Dev Configuration
241+
| Mode | Configuration | Behavior |
242+
|------|---------------|----------|
243+
| **Command mode** | `dev.command` is set (or default `npm run dev`) | CLI starts the dev server. URL defaults to `http://localhost:5173`. Override with `dev.url` or `--url` if your dev server uses a different port. |
244+
| **URL-only mode** | `dev.url` or `--url` only (no `dev.command`) | CLI assumes the dev server is already running. Does **not** start the dev server. Starts proxy only and forwards to the given URL. |
242245

243-
| Field | Type | Description | Default |
244-
| ------------- | ------ | ------------------------------------- | ------------------------- |
245-
| `dev.command` | string | Command to start dev server | `npm run dev` |
246-
| `dev.url` | string | Dev server URL (when already running) | `http://localhost:5173` |
246+
**URL precedence:** `--url` flag > `dev.url` in manifest > default `http://localhost:5173` (when command is used)
247247

248-
All fields are optional. Only specify what you need to override.
248+
### webapplication.json Schema
249249

250-
**Option A: No manifest (uses defaults)**
250+
The `webapplication.json` file is **optional**. All fields are also optional - missing fields use defaults.
251251

252-
If no `webapplication.json` exists:
252+
#### Dev Configuration
253253

254-
- Dev command: `npm run dev`
255-
- Name: folder name
256-
- Manifest watching: disabled
254+
| Field | Type | Description | Default |
255+
| ------------- | ------ | ----------- | ------- |
256+
| `dev.command` | string | Command to start the dev server (e.g., `npm run dev`). When set, the CLI starts the dev server and uses default URL `http://localhost:5173` unless overridden. | `npm run dev` |
257+
| `dev.url` | string | Dev server URL. **Command mode**: Override the default 5173 port if needed. **URL-only mode**: Required—the CLI assumes the server is already running and does not start it. | `http://localhost:5173` |
257258

258-
**Option B: Minimal manifest**
259+
**Command mode (CLI starts dev server):**
259260

260261
```json
261262
{
262263
"dev": {
263-
"command": "npm start"
264+
"command": "npm run dev"
264265
}
265266
}
266267
```
267268

268-
**Option C: Explicit URL (dev server already running)**
269+
- CLI runs `npm run dev` and waits for the server to be ready
270+
- Default URL: `http://localhost:5173`
271+
- Override port: add `"url": "http://localhost:3000"` if your dev server uses a different port
272+
273+
**URL-only mode (proxy only, server already running):**
269274

270275
```json
271276
{
@@ -275,7 +280,15 @@ If no `webapplication.json` exists:
275280
}
276281
```
277282

278-
Use this when you want to start the dev server yourself.
283+
- No `dev.command` — CLI does **not** start the dev server
284+
- You must start the dev server yourself (e.g., `npm run dev` in another terminal)
285+
- CLI starts only the proxy and forwards to the given URL
286+
287+
**No manifest (uses defaults):**
288+
289+
- Dev command: `npm run dev`
290+
- Default URL: `http://localhost:5173`
291+
- Manifest watching: disabled
279292

280293
#### Routing Configuration (Optional)
281294

@@ -375,19 +388,19 @@ Automatically detects Salesforce Code Builder environment and binds to `0.0.0.0`
375388

376389
## The `--url` Flag
377390

378-
The `--url` flag provides control over which dev server URL the proxy uses. It has smart behavior depending on whether the URL is already available.
391+
The `--url` flag overrides the dev server URL. Behavior depends on whether you have a command configured:
379392

380-
### Behavior
393+
| Scenario | Command in manifest? | `--url` behavior |
394+
|----------|----------------------|------------------|
395+
| URL-only mode | No | Required. CLI assumes the server is already running and does not start it. Use when you run the dev server yourself. |
396+
| Command mode | Yes | Optional override. Default is `http://localhost:5173`. Use `--url` to point to a different port. |
397+
| URL reachable | Either | Proxy-only: skips starting dev server, starts proxy only |
398+
| URL not reachable | Yes (command) | Starts dev server and warns if actual URL differs from `--url` |
399+
| URL not reachable | No (URL-only) | Error: server must be running at the given URL |
381400

382-
| Scenario | What Happens |
383-
| ------------------------ | ----------------------------------------------------------------- |
384-
| `--url` is reachable | **Proxy-only mode**: Skips starting dev server, only starts proxy |
385-
| `--url` is NOT reachable | Starts dev server, warns if actual URL differs from `--url` |
386-
| No `--url` provided | Starts dev server automatically, detects URL |
401+
### Connect to Existing Dev Server (Proxy-Only Mode)
387402

388-
### Use Case 1: Connect to Existing Dev Server (Proxy-Only Mode)
389-
390-
If you prefer to manage your dev server separately:
403+
When you run the dev server yourself:
391404

392405
```bash
393406
# Terminal 1: Start your dev server manually
@@ -404,36 +417,18 @@ sf webapp dev --url http://localhost:5173 --target-org myOrg
404417
```
405418
✅ URL http://localhost:5173 is already available, skipping dev server startup (proxy-only mode)
406419
✅ Ready for development!
407-
→ Proxy: http://localhost:4545
408-
→ Dev server: http://localhost:5173
420+
→ http://localhost:4545 (open this URL in your browser)
409421
```
410422

411-
### Use Case 2: URL Mismatch Warning
423+
### Override Default Port (Command Mode)
412424

413-
If you specify a `--url` that doesn't match where the dev server actually starts:
425+
When using `dev.command`, the default URL is `http://localhost:5173`. Override with `--url` if your dev server uses a different port:
414426

415427
```bash
416-
# No dev server running, specify wrong port
417-
sf webapp dev --url http://localhost:9999 --target-org myOrg
428+
sf webapp dev --url http://localhost:3000 --target-org myOrg
418429
```
419430

420-
**Output:**
421-
422-
```
423-
Warning: ⚠️ The --url flag (http://localhost:9999) does not match the actual dev server URL (http://localhost:5173/).
424-
The proxy will use the actual dev server URL.
425-
```
426-
427-
The command continues working with the actual dev server URL.
428-
429-
### Important Notes
430-
431-
- The `--url` flag checks **only** the URL you specify, not other ports
432-
- If you have a dev server on port 5173 but specify `--url http://localhost:9999`:
433-
- Command checks 9999 → not available
434-
- Starts a NEW dev server → may get port 5174 (if 5173 is taken)
435-
- Warns about mismatch (9999 ≠ 5174)
436-
- To use an existing dev server, specify its **exact** URL with `--url`
431+
If the URL is not reachable, the CLI starts the dev server and uses the actual URL (with a warning if it differs).
437432

438433
---
439434

messages/webapp.dev.md

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,44 @@ Proxy URL: %s (open this URL in your browser)
9696

9797
# info.press-ctrl-c
9898

99-
Press Ctrl+C to stop the proxy server
99+
Press Ctrl+C to stop.
100+
101+
# info.press-ctrl-c-target
102+
103+
Press Ctrl+C to stop the %s.
104+
105+
# info.stopped-target
106+
107+
✅ Stopped %s.
108+
109+
# info.stop-target-dev
110+
111+
dev server
112+
113+
# info.stop-target-proxy
114+
115+
proxy server
116+
117+
# info.stop-target-both
118+
119+
dev and proxy servers
100120

101121
# info.server-running
102122

103123
Dev server is running. Stop it by running "SFDX: Close Live Preview" from the VS Code command palette.
104124

125+
# info.server-running-target-dev
126+
127+
Dev server is running. Stop it by running "SFDX: Close Live Preview" from the VS Code command palette.
128+
129+
# info.server-running-target-proxy
130+
131+
Proxy server is running. Stop it by running "SFDX: Close Live Preview" from the VS Code command palette.
132+
133+
# info.server-running-target-both
134+
135+
Dev and proxy servers are running. Stop them by running "SFDX: Close Live Preview" from the VS Code command palette.
136+
105137
# info.dev-server-healthy
106138

107139
✓ Dev server is responding at: %s
@@ -139,6 +171,15 @@ dev.command changed to "%s" - restart the command to apply this change.
139171

140172
Failed to watch manifest: %s
141173

174+
# error.dev-url-unreachable
175+
176+
Dev server unreachable at %s.
177+
Start your dev server manually at that URL, or add dev.command to webapplication.json to start it automatically.
178+
179+
# error.port-in-use
180+
181+
Port %s is already in use. Try specifying a different port with the --port flag or stopping the service that's using the port.
182+
142183
# error.dev-server-failed
143184

144185
%s
@@ -192,14 +233,3 @@ The proxy will use the actual dev server URL.
192233

193234
Vite WebApp proxy detected at %s - using Vite's built-in proxy (standalone proxy skipped)
194235

195-
# info.stopped-proxy-only
196-
197-
✅ Stopped proxy server.
198-
199-
# info.stopped-dev-only
200-
201-
✅ Stopped dev server.
202-
203-
# info.stopped-dev-and-proxy
204-
205-
✅ Stopped dev & proxy servers.

0 commit comments

Comments
 (0)