@@ -234,7 +234,7 @@ export class MapService implements IMapService {
234234 }
235235 }
236236
237- async generateStylesJSON ( ) {
237+ async generateStylesJSON ( host : string | null = null ) : Promise < BaseStylesFile > {
238238 if ( ! ( await this . checkBaseAssetsExist ( ) ) ) {
239239 throw new Error ( 'Base map assets are missing from storage/maps' )
240240 }
@@ -248,9 +248,15 @@ export class MapService implements IMapService {
248248 const rawStyles = JSON . parse ( baseStyle . toString ( ) ) as BaseStylesFile
249249
250250 const regions = ( await this . listRegions ( ) ) . files
251- const sources = this . generateSourcesArray ( regions )
252251
253- const baseUrl = this . getPublicFileBaseUrl ( this . basemapsAssetsDir )
252+ /** If we have the host, use it to build public URLs, otherwise we'll fallback to defaults
253+ * This is mainly useful because we need to know what host the user is accessing from in order to
254+ * properly generate URLs in the styles file
255+ * e.g. user is accessing from "example.com", but we would by default generate "localhost:8080/..." so maps would
256+ * fail to load.
257+ */
258+ const sources = this . generateSourcesArray ( host , regions )
259+ const baseUrl = this . getPublicFileBaseUrl ( host , this . basemapsAssetsDir )
254260
255261 const styles = await this . generateStylesFile (
256262 rawStyles ,
@@ -341,9 +347,9 @@ export class MapService implements IMapService {
341347 return await listDirectoryContentsRecursive ( this . baseDirPath )
342348 }
343349
344- private generateSourcesArray ( regions : FileEntry [ ] ) : BaseStylesFile [ 'sources' ] [ ] {
350+ private generateSourcesArray ( host : string | null , regions : FileEntry [ ] ) : BaseStylesFile [ 'sources' ] [ ] {
345351 const sources : BaseStylesFile [ 'sources' ] [ ] = [ ]
346- const baseUrl = this . getPublicFileBaseUrl ( 'pmtiles' )
352+ const baseUrl = this . getPublicFileBaseUrl ( host , 'pmtiles' )
347353
348354 for ( const region of regions ) {
349355 if ( region . type === 'file' && region . name . endsWith ( '.pmtiles' ) ) {
@@ -414,7 +420,7 @@ export class MapService implements IMapService {
414420 /*
415421 * Gets the appropriate public URL for a map asset depending on environment
416422 */
417- private getPublicFileBaseUrl ( childPath : string ) : string {
423+ private getPublicFileBaseUrl ( specifiedHost : string | null , childPath : string ) : string {
418424 function getHost ( ) {
419425 try {
420426 const localUrlRaw = env . get ( 'URL' )
@@ -427,7 +433,7 @@ export class MapService implements IMapService {
427433 }
428434 }
429435
430- const host = getHost ( )
436+ const host = specifiedHost || getHost ( )
431437 const withProtocol = host . startsWith ( 'http' ) ? host : `http://${ host } `
432438 const baseUrlPath =
433439 process . env . NODE_ENV === 'production' ? childPath : urlJoin ( this . mapStoragePath , childPath )
0 commit comments