fix(Maps): ensure proper parsing of hostnames (#640)

This commit is contained in:
Jake Turner 2026-04-03 12:00:09 -07:00 committed by GitHub
parent 6cd11b8ccb
commit 8277e793ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 7 deletions

View File

@ -511,8 +511,18 @@ export class MapService implements IMapService {
} }
} }
/* /**
* Gets the appropriate public URL for a map asset depending on environment * Gets the appropriate public URL for a map asset depending on environment. The host and protocol that the user
* is accessing the maps from must match the host and protocol used in the generated URLs, otherwise maps will fail to load.
* If you make changes to this function, you need to ensure it handles all the following cases correctly:
* - No host provided (should default to localhost or env URL)
* - Host provided as full URL (e.g. "http://example.com:8080")
* - Host provided as host:port (e.g. "example.com:8080")
* - Host provided as bare hostname (e.g. "example.com")
* @param specifiedHost - the host as provided by the user/request, can be null or in various formats (full URL, host:port, bare hostname)
* @param childPath - the path to append to the base URL (e.g. "basemaps-assets", "pmtiles")
* @param protocol - the protocol to use in the generated URL (e.g. "http", "https"), defaults to "http"
* @returns the public URL for the map asset
*/ */
private getPublicFileBaseUrl(specifiedHost: string | null, childPath: string, protocol: string = 'http'): string { private getPublicFileBaseUrl(specifiedHost: string | null, childPath: string, protocol: string = 'http'): string {
function getHost() { function getHost() {
@ -528,16 +538,21 @@ export class MapService implements IMapService {
} }
function specifiedHostOrDefault() { function specifiedHostOrDefault() {
if(specifiedHost === null) { if (specifiedHost === null) {
return getHost() return getHost()
} }
// Try as a full URL first (e.g. "http://example.com:8080")
try { try {
const specifiedUrl = new URL(specifiedHost) const specifiedUrl = new URL(specifiedHost)
return specifiedUrl.host if (specifiedUrl.host) return specifiedUrl.host
} catch (error) { } catch {}
// Try as a bare host or host:port (e.g. "nomad-box:8080", "192.168.1.1:8080", "example.com")
try {
const specifiedUrl = new URL(`http://${specifiedHost}`)
if (specifiedUrl.host) return specifiedUrl.host
} catch {}
return getHost() return getHost()
} }
}
const host = specifiedHostOrDefault(); const host = specifiedHostOrDefault();
const withProtocol = `${protocol}://${host}` const withProtocol = `${protocol}://${host}`

View File

@ -18,4 +18,7 @@ export default defineConfig({
'~/': `${getDirname(import.meta.url)}/inertia/`, '~/': `${getDirname(import.meta.url)}/inertia/`,
}, },
}, },
server: {
allowedHosts: true // This is for dev environments only. Can be overriden with `__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS` if needed.
}
}) })