HoloMap needs three things from the application that uses it: the Elixir
dependency, the maplibre-gl npm package, and two static files served at a URL
it can reach. Only the first is automatic.
The reason is structural. Hologram resolves bare import specifiers such as "maplibre-gl"
against the host application's assets/package.json, and its compiler
npm-installs only its own. A library has no way to reach into either.
Prerequisites
Hologram 0.12 sets the floor, and it is a high one:
| Minimum | |
|---|---|
| Elixir | 1.19 |
| OTP | 28.1 |
| Node.js | 20 |
| A working Phoenix application | any |
With asdf:
$ asdf install erlang 28.5
$ asdf install elixir 1.19.5-otp-28
Live reload also needs a filesystem watcher. On Linux that is inotify-tools,
and without it mix holo dies at startup with a MatchError on :ignore:
$ sudo apt install inotify-tools # Debian/Ubuntu
1. Add the dependency
def deps do
[
{:hologram, "~> 0.12.11"},
{:holo_map, "~> 0.2"}
]
endHoloMap supports Hologram 0.12.11, the version it is tested on, and later 0.12
releases. Hologram is not yet at 1.0, and a minor release can change behaviour
the library depends on, as 0.12 did for prop validation and attribute escaping.
The requirement therefore stops below 0.13.
Follow Hologram's own installation guide
for the compiler entry, the router plug and the formatter, all of which belong
to the application that owns the Phoenix endpoint, not to HoloMap. Hologram
0.12 serves its bundles from its router, so the guide no longer has a static
path step. An application set up with Hologram 0.11's guide can remove
"hologram" from its endpoint's Plug.Static only: list, as
Serving the bundles explains.
2. Install MapLibre
$ npm install --prefix assets maplibre-gl@^6.19.0
HoloMap is written against MapLibre GL JS 6.19.0, the lowest release this
requirement accepts, and its browser suites run on 6.19.0 and 6.20.0. A later
6.x release also satisfies the requirement.
Add it to a setup alias so a fresh checkout does not silently skip it:
defp aliases do
[
setup: ["deps.get", "assets.install", "compile", "assets.copy"],
"assets.install": &install_npm_deps/1,
"assets.copy": ©_static_assets/1
]
end
defp install_npm_deps(_args) do
{_output, 0} = System.cmd("npm", ["install", "--prefix", "assets"], into: IO.stream())
end3. Serve MapLibre's worker
This step is not optional.
MapLibre parses tiles in a Web Worker that it starts from
maplibre-gl-worker.mjs, resolved relative to its own module URL. Hologram
bundles MapLibre into a page bundle under /hologram/, so that resolution lands
on a file which is not there. What you see is a map that renders its background
and never draws a single tile. MapLibre reports the failed worker through the
map's error event: on_error receives it with the message "Worker failed to
load. Check that the worker URL is correct.", and a map without on_error logs
it to the browser console. The Events guide describes the
payload.
Copy the worker into your static assets:
defp copy_static_assets(_args) do
dist = "assets/node_modules/maplibre-gl/dist"
File.mkdir_p!("priv/static/assets")
for file <- ~w(maplibre-gl.css maplibre-gl-worker.mjs) do
File.cp!(Path.join(dist, file), Path.join("priv/static/assets", file))
end
endHoloMap.Map defaults worker_url to "/assets/maplibre-gl-worker.mjs",
with the version of the MapLibre the page loaded as a v query parameter:
/assets/maplibre-gl-worker.mjs?v=6.19.0 on 6.19.0, as MapLibre's own default
URL has since 6.13.0. Upgrading maplibre-gl changes the URL, so no browser or
proxy cache can hand the new library the old worker. Plug.Static ignores the
query and serves the file; a CDN in front of it has to keep the query in its
cache key.
If the worker lives elsewhere, set the prop:
<HoloMap.Map cid="explorer" style={@style} worker_url="/static/js/maplibre-gl-worker.mjs" />A URL you set is used as it is, with nothing added, so keeping a stale worker
out of caches is then up to you: put a version or a content hash in the URL,
or serve the file with headers that make browsers revalidate it, as the
ETag and the cache-control: public without a max-age that Plug.Static
sends by default do.
MapLibre keeps one worker URL per page, so set the same worker_url on every
map. A map that asks for another one runs on the page's worker, and HoloMap
logs a warning in the browser console.
MapLibre releases before 6.13.0 split the worker in two: it imports
maplibre-gl-shared.mjs by relative path, so an application pinned to one of
them copies that file too and serves it next to the worker. From 6.13.0 the
worker is self-contained, and maplibre-gl-shared.mjs is an empty file that
MapLibre keeps only so that existing copy steps do not fail.
4. Load the stylesheet
MapLibre's controls, markers and popups are unstyled without it. In your layout:
<link rel="stylesheet" href="/assets/maplibre-gl.css" />Or, if your application has a CSS pipeline, import it there instead:
@import "maplibre-gl/dist/maplibre-gl.css";Serving the bundles
MapLibre is a big library, and Hologram builds one bundle per page. A page
that uses HoloMap therefore carries its own copy, around 1.1 MB minified and
0.28 MB gzipped, and five such pages are five distinct downloads, not one cached
download reused five times. That is inherent to Hologram's page-bundle model,
not something this library can avoid.
Hologram 0.12 takes care of most of what that implies. Hologram's router serves
the bundles from Hologram's own build directory, as the gzip copies its compiler
writes next to them, and with cache-control: public, max-age=31536000, immutable, which is safe because every bundle is named by its content hash. The
application needs no Plug.Static or compression step of its own for them.
Hologram 0.11 wrote the bundles to the application's priv/static/hologram and
its installation guide added "hologram" to the endpoint's Plug.Static
only: list. Hologram 0.12 removes that directory when it compiles, so an
application upgrading from 0.11 can remove the entry.
Do not serve sourcemaps in production
esbuild writes a sourcemap beside every bundle, and they are about four times the size of the code, 61 MB across this demo. Only a browser with devtools open requests one, so they cost nothing in normal use and everything the moment someone opens the network tab. Hologram's router serves them along with the bundles, so refuse them in a plug that comes before it in the endpoint, and keep them in development:
plug :reject_sourcemaps
# ...
plug Hologram.Router
defp reject_sourcemaps(conn, _opts) do
if String.ends_with?(conn.request_path, ".map") and
not Application.get_env(:my_app, :serve_sourcemaps, false) do
conn |> send_resp(404, "") |> halt()
else
conn
end
enddemo/lib/demo_web/endpoint.ex has this wired up if you want a reference.
Umbrella projects
npm packages live in an assets directory at the umbrella root, and are
resolved from there for every child app. The child that owns the endpoint takes
the Hologram compiler entry; a child that only defines pages and components
takes the dependency alone.
Checking it worked
defmodule MyAppWeb.MapPage do
use Hologram.Page
route "/map"
layout MyAppWeb.Layout
def init(_params, component, _server) do
put_state(component, :style, "https://demotiles.maplibre.org/style.json")
end
def template do
~HOLO"""
<HoloMap.Map cid="check" style={@style} center={{0.0, 20.0}} zoom={2} height="400px">
<HoloMap.Control.Navigation />
</HoloMap.Map>
"""
end
endYou should get a world map with zoom buttons. If you get a coloured rectangle with no countries in it, step 3 is the one to revisit.
Troubleshooting
| Symptom | Cause |
|---|---|
Background renders, no tiles ever, and "Worker failed to load" in on_error or the console | The worker is not being served at worker_url, see step 3 |
| Nothing at all, container has zero height | height was set to nil without CSS taking over |
boolean expected, string found from MapLibre | A boolean prop this library does not yet coerce. Pass ={true} explicitly and please report it |
Hologram.CompileError naming a HoloMap component, at build time | A template call site misses a required prop, or gives a prop such as a control's position a literal value the component does not accept. The Hologram compiler checks this in a production build or with HOLOGRAM_START=1 |
Hologram.PropError naming a HoloMap component, as a page renders | The same mistake where the compiler cannot see it: a spread, a dynamic tag, a value computed as the template renders, a component rendered programmatically, or a render in development or test without the compiler |
a HoloMap definition component was rendered outside a <HoloMap.Map> | A source, layer, marker, popup or control is not nested inside a map |
mix holo exits with MatchError on :ignore | The filesystem watcher is missing, see Prerequisites |
Browser cannot reach localhost but curl can | The endpoint is listening on IPv4 only while localhost resolves to ::1 |