@@ -238,10 +238,7 @@ export class MapService implements IMapService {
238238 const regions = ( await this . listRegions ( ) ) . files
239239 const sources = this . generateSourcesArray ( regions )
240240
241- const localUrl = env . get ( 'URL' )
242- const withProtocol = localUrl . startsWith ( 'http' ) ? localUrl : `http://${ localUrl } `
243- const baseUrlPath = urlJoin ( this . mapStoragePath , this . basemapsAssetsDir )
244- const baseUrl = new URL ( baseUrlPath , withProtocol ) . toString ( )
241+ const baseUrl = this . getPublicFileBaseUrl ( this . basemapsAssetsDir )
245242
246243 const styles = await this . generateStylesFile (
247244 rawStyles ,
@@ -317,17 +314,14 @@ export class MapService implements IMapService {
317314 }
318315
319316 private generateSourcesArray ( regions : FileEntry [ ] ) : BaseStylesFile [ 'sources' ] [ ] {
320- const localUrl = env . get ( 'URL' )
321317 const sources : BaseStylesFile [ 'sources' ] [ ] = [ ]
318+ const baseUrl = this . getPublicFileBaseUrl ( 'pmtiles' )
322319
323320 for ( const region of regions ) {
324321 if ( region . type === 'file' && region . name . endsWith ( '.pmtiles' ) ) {
325322 const regionName = region . name . replace ( '.pmtiles' , '' )
326323 const source : BaseStylesFile [ 'sources' ] = { }
327- const sourceUrl = new URL (
328- urlJoin ( this . mapStoragePath , 'pmtiles' , region . name ) ,
329- localUrl . startsWith ( 'http' ) ? localUrl : `http://${ localUrl } `
330- ) . toString ( )
324+ const sourceUrl = urlJoin ( baseUrl , region . name )
331325
332326 source [ regionName ] = {
333327 type : 'vector' ,
@@ -388,4 +382,29 @@ export class MapService implements IMapService {
388382
389383 await deleteFileIfExists ( fullPath )
390384 }
385+
386+ /*
387+ * Gets the appropriate public URL for a map asset depending on environment
388+ */
389+ private getPublicFileBaseUrl ( childPath : string ) : string {
390+ function getHost ( ) {
391+ try {
392+ const localUrlRaw = env . get ( 'URL' )
393+ if ( ! localUrlRaw ) return 'localhost'
394+
395+ const localUrl = new URL ( localUrlRaw )
396+ return localUrl . host
397+ } catch ( error ) {
398+ return 'localhost'
399+ }
400+ }
401+
402+ const host = getHost ( )
403+ const withProtocol = host . startsWith ( 'http' ) ? host : `http://${ host } `
404+ const baseUrlPath =
405+ process . env . NODE_ENV === 'production' ? childPath : urlJoin ( this . mapStoragePath , childPath )
406+
407+ const baseUrl = new URL ( baseUrlPath , withProtocol ) . toString ( )
408+ return baseUrl
409+ }
391410}
0 commit comments