fix(Maps): static path resolution

This commit is contained in:
Jake Turner 2026-01-23 22:13:03 +00:00 committed by Jake Turner
parent d5db024eee
commit f49b9abb81
3 changed files with 30 additions and 12 deletions

View File

@ -238,10 +238,7 @@ export class MapService implements IMapService {
const regions = (await this.listRegions()).files const regions = (await this.listRegions()).files
const sources = this.generateSourcesArray(regions) const sources = this.generateSourcesArray(regions)
const localUrl = env.get('URL') const baseUrl = this.getPublicFileBaseUrl(this.basemapsAssetsDir)
const withProtocol = localUrl.startsWith('http') ? localUrl : `http://${localUrl}`
const baseUrlPath = urlJoin(this.mapStoragePath, this.basemapsAssetsDir)
const baseUrl = new URL(baseUrlPath, withProtocol).toString()
const styles = await this.generateStylesFile( const styles = await this.generateStylesFile(
rawStyles, rawStyles,
@ -317,17 +314,14 @@ export class MapService implements IMapService {
} }
private generateSourcesArray(regions: FileEntry[]): BaseStylesFile['sources'][] { private generateSourcesArray(regions: FileEntry[]): BaseStylesFile['sources'][] {
const localUrl = env.get('URL')
const sources: BaseStylesFile['sources'][] = [] const sources: BaseStylesFile['sources'][] = []
const baseUrl = this.getPublicFileBaseUrl('pmtiles')
for (const region of regions) { for (const region of regions) {
if (region.type === 'file' && region.name.endsWith('.pmtiles')) { if (region.type === 'file' && region.name.endsWith('.pmtiles')) {
const regionName = region.name.replace('.pmtiles', '') const regionName = region.name.replace('.pmtiles', '')
const source: BaseStylesFile['sources'] = {} const source: BaseStylesFile['sources'] = {}
const sourceUrl = new URL( const sourceUrl = urlJoin(baseUrl, region.name)
urlJoin(this.mapStoragePath, 'pmtiles', region.name),
localUrl.startsWith('http') ? localUrl : `http://${localUrl}`
).toString()
source[regionName] = { source[regionName] = {
type: 'vector', type: 'vector',
@ -388,4 +382,29 @@ export class MapService implements IMapService {
await deleteFileIfExists(fullPath) await deleteFileIfExists(fullPath)
} }
/*
* Gets the appropriate public URL for a map asset depending on environment
*/
private getPublicFileBaseUrl(childPath: string): string {
function getHost() {
try {
const localUrlRaw = env.get('URL')
if (!localUrlRaw) return 'localhost'
const localUrl = new URL(localUrlRaw)
return localUrl.host
} catch (error) {
return 'localhost'
}
}
const host = getHost()
const withProtocol = host.startsWith('http') ? host : `http://${host}`
const baseUrlPath =
process.env.NODE_ENV === 'production' ? childPath : urlJoin(this.mapStoragePath, childPath)
const baseUrl = new URL(baseUrlPath, withProtocol).toString()
return baseUrl
}
} }

View File

@ -18,7 +18,7 @@ export default class MapStaticProvider {
register() { register() {
this.app.container.singleton(MapsStaticMiddleware, () => { this.app.container.singleton(MapsStaticMiddleware, () => {
const path = join(process.cwd(), '/storage/maps') const path = join(process.cwd(), '/storage/maps')
logger.debug(`Maps static files will be served from ${path}`) logger.info(`Maps static files will be served from ${path}`)
const config = this.app.config.get<any>('static', defineConfig({})) const config = this.app.config.get<any>('static', defineConfig({}))
return new MapsStaticMiddleware(path, config) return new MapsStaticMiddleware(path, config)
}) })

View File

@ -24,6 +24,7 @@ transmit.registerRoutes()
router.get('/', [HomeController, 'index']) router.get('/', [HomeController, 'index'])
router.get('/home', [HomeController, 'home']) router.get('/home', [HomeController, 'home'])
router.on('/about').renderInertia('about') router.on('/about').renderInertia('about')
router.get('/maps', [MapsController, 'index'])
router.get('/easy-setup', [EasySetupController, 'index']) router.get('/easy-setup', [EasySetupController, 'index'])
router.get('/easy-setup/complete', [EasySetupController, 'complete']) router.get('/easy-setup/complete', [EasySetupController, 'complete'])
@ -55,8 +56,6 @@ router
}) })
.prefix('/docs') .prefix('/docs')
router.get('/maps', [MapsController, 'index'])
router router
.group(() => { .group(() => {
router.get('/regions', [MapsController, 'listRegions']) router.get('/regions', [MapsController, 'listRegions'])