@@ -9,6 +9,31 @@ import type { EnvironmentConfig } from './environment.js';
99
1010export type Application = ReturnType < typeof application > ;
1111
12+ /**
13+ * Resolves the server URL for a dev/serve process, ensuring the runtime port
14+ * is always reflected in the URL. Uses the URL constructor to detect whether
15+ * an explicit port is present (avoiding false positives from the scheme colon).
16+ */
17+ export const resolveServerUrl = (
18+ optsServerUrl : string | undefined ,
19+ fallbackServerUrl : string | undefined ,
20+ port : number ,
21+ ) : string => {
22+ if ( optsServerUrl ) {
23+ try {
24+ const parsed = new URL ( optsServerUrl ) ;
25+ if ( ! parsed . port ) {
26+ parsed . port = String ( port ) ;
27+ }
28+ return parsed . origin ;
29+ } catch {
30+ // Bare host (e.g. "localhost"), not a full URL
31+ return `${ optsServerUrl } :${ port } ` ;
32+ }
33+ }
34+ return fallbackServerUrl || `http://localhost:${ port } ` ;
35+ } ;
36+
1237export const application = (
1338 config : ApplicationConfig ,
1439 appDirPath : string ,
@@ -58,13 +83,7 @@ export const application = (
5883 dev : async ( opts : { port ?: number ; manualStart ?: boolean ; detached ?: boolean ; serverUrl ?: string } = { } ) => {
5984 const log = logger . child ( { prefix : 'dev' } ) . info ;
6085 const port = opts . port || ( await getPort ( ) ) ;
61- const getServerUrl = ( ) => {
62- if ( opts . serverUrl ) {
63- return opts . serverUrl . includes ( ':' ) ? opts . serverUrl : `${ opts . serverUrl } :${ port } ` ;
64- }
65- return serverUrl || `http://localhost:${ port } ` ;
66- } ;
67- const runtimeServerUrl = getServerUrl ( ) ;
86+ const runtimeServerUrl = resolveServerUrl ( opts . serverUrl , serverUrl , port ) ;
6887 log ( `Will try to serve app at ${ runtimeServerUrl } ` ) ;
6988 if ( opts . manualStart ) {
7089 // for debugging, you can start the dev server manually by cd'ing into the temp dir
@@ -147,13 +166,7 @@ export const application = (
147166 serve : async ( opts : { port ?: number ; manualStart ?: boolean ; detached ?: boolean ; serverUrl ?: string } = { } ) => {
148167 const log = logger . child ( { prefix : 'serve' } ) . info ;
149168 const port = opts . port || ( await getPort ( ) ) ;
150- const getServerUrl = ( ) => {
151- if ( opts . serverUrl ) {
152- return opts . serverUrl . includes ( ':' ) ? opts . serverUrl : `${ opts . serverUrl } :${ port } ` ;
153- }
154- return serverUrl || `http://localhost:${ port } ` ;
155- } ;
156- const runtimeServerUrl = getServerUrl ( ) ;
169+ const runtimeServerUrl = resolveServerUrl ( opts . serverUrl , serverUrl , port ) ;
157170 log ( `Will try to serve app at ${ runtimeServerUrl } ` ) ;
158171
159172 if ( opts . manualStart ) {
0 commit comments