Skip to content

Commit fa8300b

Browse files
committed
fix(Maps): ensure asset urls resolve correctly
1 parent 42568a9 commit fa8300b

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

admin/app/controllers/maps_controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default class MapsController {
6969
return await this.mapService.listRegions()
7070
}
7171

72-
async styles({ response }: HttpContext) {
72+
async styles({ request, response }: HttpContext) {
7373
// Automatically ensure base assets are present before generating styles
7474
const baseAssetsExist = await this.mapService.ensureBaseAssets()
7575
if (!baseAssetsExist) {
@@ -79,7 +79,7 @@ export default class MapsController {
7979
})
8080
}
8181

82-
const styles = await this.mapService.generateStylesJSON()
82+
const styles = await this.mapService.generateStylesJSON(request.host())
8383
return response.json(styles)
8484
}
8585

admin/app/services/map_service.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)