Things that do not work, and why. Most trace back to Hologram being a young framework with hooks a map library would like and does not have.
Slot content is cloned
HoloMap.Marker and HoloMap.Popup accept slot content, and MapLibre receives a
clone of it rather than the node Hologram rendered.
MapLibre moves a marker's element into its own overlay container. Handing it the real node would take that node out from under the virtual DOM and corrupt the next patch, so the markup is cloned instead.
The consequence: Hologram event bindings inside the slot do not fire.
<HoloMap.Marker id="office" lng_lat={{-70.66, 19.45}}>
<button $click={:clicked}>Details</button> <!-- will not fire -->
</HoloMap.Marker>Use the marker's own on_click prop instead:
<HoloMap.Marker id="office" lng_lat={{-70.66, 19.45}} on_click={:clicked}>
<div class="chip">Head office</div>
</HoloMap.Marker>The markup still renders and restyles normally on re-render. Only the bindings are inert. For genuinely interactive content, put it outside the map and drive it from state.
Untrusted content in popups
A popup shows one of three things, and only one of them keeps markup out whatever the value holds:
| Content | Reaches MapLibre through | A value the application does not control |
|---|---|---|
HoloMap.Popup's text, HoloMap.Marker's popup_text | setText, which puts it in a text node | Shows as it is, markup and all, as characters |
HoloMap.Popup's html, HoloMap.Marker's popup_html | setHTML, which inserts it as markup | Must be escaped before it is interpolated |
HoloMap.Popup's slot content | setHTML, with the markup Hologram rendered in the slot | Safe where Hologram rendered it as text, such as {@name} |
For a value that only needs showing, such as a place name, a review or a
feature property, reach for text:
<HoloMap.Popup id="review" lng_lat={@coords} text={@review.body} />
<HoloMap.Marker id="shop" lng_lat={@coords} popup_text={@shop.name} />A @review.body of <img src=x onerror=alert(1)> shows those characters,
and no img element is created.
text wins over html, and html over slot content. A popup given both
text and html, or a marker given both popup_text and popup_html, shows
the text and logs a console warning naming the two props: showing the html
instead would silently drop the choice of text, the one that keeps markup out
of the page. Each updates the popup in place when it changes.
html and popup_html are inserted as markup
HoloMap.Popup's html and HoloMap.Marker's popup_html reach MapLibre's
setHTML, which inserts them as markup. Hologram escapes none of the three ways
to write them:
<HoloMap.Popup id="p" lng_lat={@coords} html="<b>hi</b>" />
<HoloMap.Popup id="p" lng_lat={@coords} html="<b>{@name}</b>" />
<HoloMap.Popup id="p" lng_lat={@coords} html={@html} />- A plain literal arrives with its character references decoded, as a
browser decodes them, so
html="<b>"is the markup<b>. - A literal with an inserted value arrives as the literal text and the value
joined, neither of them escaped. A
@nameof<img src=x onerror=alert(1)>is inserted as animgelement with anonerrorhandler. - An expression arrives as it is. An escaped string passed this way, such as
html={"<b>"}, stays escaped and shows the characters<b>.
The application must therefore escape any value it does not control, such as
user input or a field from an external service, before it interpolates it.
Plug.HTML.html_escape/1, which comes with Phoenix, does that. Call it on the
server, in init/3 or a command, where the value arrives:
def init(_params, component, _server) do
place = MyApp.Places.get!(1)
name = Plug.HTML.html_escape(place.name)
put_state(component, coords: place.coords, html: "<b>#{name}</b>")
endtext and popup_text arrive the same three ways, literal text decoded and
inserted values joined unescaped, and whatever arrives shows as text.
Hologram 0.11 escaped both the literal text and the inserted value, so there
html="<b>hi</b>" rendered the characters <b>hi</b>.
Slot content
Slot content goes through setHTML as well: the runtime reads the markup
Hologram rendered in the slot and hands MapLibre that string. The tags written
in the template are markup, which is what a slot is for, and a value Hologram
rendered as text arrives escaped, so it shows as text:
<HoloMap.Popup id="p" lng_lat={@coords}><b>{@name}</b></HoloMap.Popup>A value in an attribute is as safe as it is anywhere else on the page: an
href={@url} holding a javascript: URL runs when the link is clicked, in a
popup or out of one. HoloMap.Marker's slot content is cloned the same way
into the marker's element.
Bare boolean attributes
In a HOLO template a bare attribute is an empty string, not true. HoloMap
coerces this for every boolean prop it knows about, so <... cluster /> works.
A boolean prop it has missed would reach MapLibre as "" and fail style
validation with boolean expected, string found. Pass ={true} explicitly if
you hit one, and it is worth reporting.
Controls are replaced, not updated
Changing any prop on a control removes and re-adds it, in its place in its
corner. That is invisible for the built-ins, but it means a control is not the
place to put a rapidly changing value. A HoloMap.Control.Custom whose control
has an update(options) method takes an options change in place.
The definition key includes the control's position, because MapLibre cannot
move a control between corners in place.
Structural source changes tear down layers
A GeoJSON source's data, cluster, cluster_radius and cluster_max_zoom,
a vector, raster or raster-dem source's tiles and url, and an image
source's url/coordinates update in place. Any other source key (zoom
ranges, bounds, tile_size, cluster_min_points, cluster_properties) is
structural, and so is taking tiles, url, cluster_radius or
cluster_max_zoom away, which MapLibre's setters cannot do. MapLibre refuses
to remove a source that still has layers attached, so the reconciler detaches
the layers it owns, rebuilds the source, and re-adds them where they were in
the stack.
It works, and it is not free. Put frequently-changing data in data.
An update in place keeps what MapLibre does with it. A new url keeps the
bounds and zoom range the previous TileJSON gave where the new one leaves them
out, where a rebuilt source would start from MapLibre's defaults. After a
cluster change, MapLibre's getStyle still reports the cluster options the
source was added with.
MapLibre is bundled once per page
Hologram builds one JavaScript bundle per page, and a page that uses HoloMap
carries its own copy of MapLibre, around 1.1 MB minified and 0.28 MB gzipped.
Five map pages are five distinct downloads.
This is Hologram's bundling model, not something the library can change. It is also not as bad as it sounds: a visitor only downloads the pages they actually visit, and Hologram serves each one compressed and cached for good, since every bundle is named by its content hash. See Installation.
No HoloMap styling of its own
The library ships no CSS. MapLibre's own stylesheet is a required install step, and everything else (sizing, borders, marker chrome) is yours. That is deliberate: a component library that imposed a look would be fought rather than used.
One map per cid
The registry is keyed by cid, which is also the DOM container's id. Two
HoloMap.Map components with the same cid on one page is undefined behaviour
though Hologram itself already requires cids to be unique.
Hologram gaps this library works around
These are upstream, not HoloMap's, and are worth knowing if you write your own
interop. This section was last checked against Hologram 0.12.11.
Float.round/2 rejects integers. MapLibre reports a whole-number coordinate
as an integer, and Float.round/2 only accepts floats. Widen with * 1.0
first. This is standard Elixir behaviour, but it bites more often here because
the values come from JavaScript, where there is no distinction.
The interop reads an atom as the global it names. An atom handed to
Hologram.JS.call/2 becomes the JavaScript global of that name when there is
one, or a binding the module imports with js_import, and a string
otherwise. So :top, :name and :status arrive as window.top,
window.name and window.status. HoloMap.Runtime sends every call and
request as one JSON string encoded with HoloMap.JSON, in which an atom is
its name; do the same in your own interop with values you do not control.
Not all of the standard library is ported. Hologram's client runtime reference tracks what is available. Anything in a template expression or an action runs in the browser and is subject to it.
Hologram 0.11.0's client-side port of :maps.find/2 returned the bare value
instead of {:ok, value}, which broke Map.fetch/2 in the browser. Hologram
0.12 returns {:ok, value}, checked in a browser on 0.12.11, and HoloMap no
longer works around it.
Things Hologram does not offer yet
As of Hologram 0.12.11, a component has init, action, command and
template, and nothing else.
- No "props changed" hook. This is the whole reason for the DOM definition protocol described in Architecture.
- No unmount hook. Disposal is inferred from the container going stale.
Hologram's roadmap mentions a
setup(pre-init) hook; an unmount counterpart would letHoloMapdrop a good deal of machinery. - Interop only runs in actions. A map cannot be constructed during SSR, so the first paint is always a blank container.
A sprite that changes while it loads
MapLibre loads a sprite in the background, and nothing cancels the load when
the sprite is removed (_loadSprite in its src/style/style.ts registers
whatever arrives). A HoloMap.Sprite taken out of the template before its
icons have arrived leaves them on the map once they do, until a style is
built from scratch, and one whose url changes in that time can end up with
the icons of the URL whose load finished last. A sprite that stays as it is
long enough for its icons to arrive, about one round trip, is not affected.
A style that fails to load
MapLibre reports a style it cannot load through on_error, or the browser
console, and the browser runtime ends the change that asked for it (see
"Changing the style" in HoloMap.Map). What it does not do:
- It does not ask again. The
styleprop keeps the value the page rendered, so rendering the same value again asks for nothing. To try a failed URL again, set the prop to another value first, or callHoloMap.API.set_style/3, which asks every time it is called. - It does not give up on a request that never ends. MapLibre sets no time limit on the style's request, so a server that never answers holds the change, and the work waiting for it, until the browser gives up on the request.
- A map left with no style answers no request. A style built from
scratch, or the map's first style, that fails to load leaves the map
without one:
HoloMap.API.request/4answers with an:erroruntil the map is asked for another style, and the components and the otherHoloMap.APIcalls wait for that style. - A context lost while a style loads still leaves nothing to wait for.
MapLibre drops a style it was loading when the browser takes the WebGL
context away, and reports nothing more of it, so the map waits until its
stylechanges, as "Losing the WebGL context" inHoloMap.Mapsays.
A light MapLibre refuses reaches only the console
MapLibre validates the light HoloMap.Light sets, and leaves the light as
it was for a value it refuses, such as an anchor that is neither "map"
nor "viewport". Its light has no evented parent (_load in its
src/style/style.ts), so the error reaches no listener of the map:
MapLibre logs it to the browser console, and on_error does not hear it.
Request rules
HoloMap.Map's request_rules change the requests MapLibre makes, through
its request transform. What they do not do:
- They do not reach a request MapLibre does not make. A protocol's load
function is handed the headers and credentials with the rest of the
request, and the requests it makes itself are its own: PMTiles' protocol
fetches its archive without them. Nor do they reach what the browser loads
for the page's own elements, such as an
<img>in a marker's slot. - A change does not load anything again. It applies from MapLibre's next request. Tiles MapLibre has loaded stay, and so does the style, until MapLibre requests them again.
- They are in the page. Every value is written into the page's HTML, so
a secret that must stay on the server cannot be a rule's value (see
"Request rules" in
HoloMap.Map).
Registered JavaScript
The Registering JavaScript guide covers how it works. What it does not do:
- A protocol registered late does not retry. A request MapLibre made for
a URL whose scheme had no protocol yet has failed, and MapLibre reported it
through
on_error. It is not made again when the protocol is registered, so register protocols in a script the layout loads, before the first map boots. - A
js_importmodule registers again on every page. Hologram builds one bundle per page, and each carries its own copy of an imported module, which runs once per page the browser loads. Each run replaces the names it registers, with a console warning. Keep registrations in a script the layout loads. - A custom layer is not in the style MapLibre describes. MapLibre cannot
serialize a custom layer, so
getStyle(), andHoloMap.API.request/4with"getStyle", leave it out, andgetLayerfinds it withtype"custom". It has no paint, layout, filter or zoom range, and no events: MapLibre finds no features on it. - MapLibre warns about custom layers at a WebGL context loss. It logs that it cannot restore a custom layer, and that the application has to add it again. The browser runtime does, once the context is back; the warning is MapLibre's own, and cannot be turned off.
- A custom layer's projection is the application's. The frame's
defaultProjectionData.mainMatrixsuits a mercator map. On a globe, the layer has to use the projection code MapLibre hands it inframe.shaderData. - No
transformStyleof the application's. MapLibre'ssetStyletakes onetransformStyle, and the browser runtime uses it on every style change to carry the components' sources, layers and sprites into the new style. It decides which ids are the new style's from the style it is handed, so an application's transform run before it, which typically copies sources and layers from the previous style into the next, would copy the components' own, and the runtime would then take them for the new style's and stop managing them. Run after it, the application's transform could remove or replace what the components declared while the runtime goes on believing it is there. So there is no registeredtransformStyle: build the style you want in Elixir, or in the JSON thestyleprop points at.
Not implemented yet
- Nested
HoloMap.Popupinside aHoloMap.Markerslot; use the marker'spopup_htmlprop - Server-side rendering of a static map image