Skip to content

Commit f49b9ab

Browse files
committed
fix(Maps): static path resolution
1 parent d5db024 commit f49b9ab

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

admin/app/services/map_service.ts

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

admin/providers/map_static_provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class MapStaticProvider {
1818
register() {
1919
this.app.container.singleton(MapsStaticMiddleware, () => {
2020
const path = join(process.cwd(), '/storage/maps')
21-
logger.debug(`Maps static files will be served from ${path}`)
21+
logger.info(`Maps static files will be served from ${path}`)
2222
const config = this.app.config.get<any>('static', defineConfig({}))
2323
return new MapsStaticMiddleware(path, config)
2424
})

admin/start/routes.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ transmit.registerRoutes()
2424
router.get('/', [HomeController, 'index'])
2525
router.get('/home', [HomeController, 'home'])
2626
router.on('/about').renderInertia('about')
27+
router.get('/maps', [MapsController, 'index'])
2728

2829
router.get('/easy-setup', [EasySetupController, 'index'])
2930
router.get('/easy-setup/complete', [EasySetupController, 'complete'])
@@ -55,8 +56,6 @@ router
5556
})
5657
.prefix('/docs')
5758

58-
router.get('/maps', [MapsController, 'index'])
59-
6059
router
6160
.group(() => {
6261
router.get('/regions', [MapsController, 'listRegions'])

0 commit comments

Comments
 (0)