StaticMaps.Plug serves maps straight from signed URLs and lets the HTTP caches in front
of your application do the storing, so you never keep an image yourself. It is an
optional layer over render/1, compiled only when :plug is in your dependencies.
Mounting the Plug
# router.ex — outside any pipeline that fetches the session
forward "/maps", StaticMaps.Plug,
secret: {MyApp.Config, :map_secret, []},
source: StaticMaps.Source.OSM,
icons: %{office: File.read!("priv/office.svg")},
cache: [max_age: 86_400, s_maxage: 2_592_000, stale_while_revalidate: 86_400]# anywhere you build a page
src = StaticMaps.Plug.path("/maps", [center: {14.5547, 121.0244}, zoom: 15], secret: secret)
# => "/maps/0eba24287dc2c888.png?c=14.5547%2C121.0244&z=15&sig=JGwV1b3F…":secret is a binary of at least 32 bytes, or an {module, function, args} tuple that
returns one, for a secret read from runtime configuration.
The same options always produce the same URL, so the URL itself is the cache key. The
first request for a URL renders the image; after that the CDN answers every request until
the entry expires. The start of the path is a hash of the query, so even a CDN
configured to ignore query strings keys each map separately. format: :jpeg in the third
argument gives a .jpg URL.
What a URL can carry
A URL carries :center, :zoom, :bounds, :size, :scale, :padding,
:background and :markers; path/3 raises ArgumentError for anything else. The
output format is the :format option of path/3 itself. :source, :attribution and
:on_tile_error come from the Plug's configuration, never from a URL. A :background
given to the Plug is a default, which a URL's :background overrides.
A marker in a URL may have :color, :label and :icon. Its :icon is the name of one
of the Plug's :icons, since a URL cannot carry an image; {lat, lng, icon: :office}
draws the icon registered as :office.
Cache headers
:cache option | Default | Header it sets |
|---|---|---|
:max_age | 86_400 (1 day) | max-age, how long browsers keep it |
:s_maxage | 2_592_000 (30 days) | s-maxage, how long shared caches (CDNs) keep it |
:stale_while_revalidate | 86_400 | stale-while-revalidate, so an expired entry is served while the CDN refetches |
:immutable | false | immutable, for when you never want a URL to change |
cache: false sends Cache-Control: no-store instead, which is useful in development.
Every cacheable response also says no-transform, so proxies, CDNs and the server's own
compression leave the image alone rather than spending time recompressing a PNG.
The lifetimes are shorter than immutable by default because the tiles under a map do
change. Roads get built and labels get fixed, and a month-old map is fine but a
year-old one may not be.
Each response also carries a weak ETag derived from the URL, the Plug's configuration
and the library version. A browser revalidating an expired image gets a 304 Not Modified
straight after the signature check, without a render or a single tile fetch. It is weak on
purpose: when the tile provider updates its tiles, the pixels change but the map still
shows the same place, which is what a weak validator means. The library version is in it
so that a release that draws differently replaces old images.
Signed URLs and limits
The URL is signed with an HMAC of :secret, and the Plug rejects any request whose
signature does not match with 403. Without that, anyone could use your server to render
arbitrary maps, and every one of those renders is tile traffic to your provider, billed to
you or, with Source.OSM, taken from the volunteers' servers. The signature also stops
people busting your CDN cache with endless variations of one URL.
path/3 writes the parameters in a fixed order before signing, markers included, so the
same map always gets the same URL and the same signature.
Signing keeps strangers out; it does not protect you from your own bugs. The Plug still enforces limits on every signed request:
| Plug option | Default | Limits |
|---|---|---|
:max_size | {1280, 1280} | the largest :size it renders |
:max_scale | 2 | the largest :scale |
:max_markers | 100 | markers in one map |
:max_concurrency | System.schedulers_online() | renders in progress at once, for this mounted Plug |
Identical requests that arrive while a render is in progress wait for that render instead of starting their own. This matters on a cold CDN: a popular page can send the same URL many times before the first response is cached.
Responses
| Situation | Status | Cache-Control |
|---|---|---|
| Rendered | 200 | your :cache settings |
If-None-Match matches | 304 | your :cache settings |
| Bad or missing signature | 403 | no-store |
| Signed, but over a limit or invalid | 400 | no-store |
| A tile failed, no source could render, or the render timed out | 503 | no-store |
| The render failed otherwise, such as in libvips | 500 | no-store |
Not GET or HEAD | 405 | no-store |
Every failure is no-store, so a CDN never keeps an error for a month. The Plug always
renders with on_tile_error: :fail for the same reason. Error bodies are a short generic
message; details go to your logs, not to the client. HEAD is answered like GET
without the body.
Keeping the CDN able to cache
A CDN will not cache a response that sets a cookie, and most will not cache one marked
private. Mount the Plug outside your :browser pipeline so no session or CSRF plug
touches its responses. The Plug itself never sets cookies or Vary, because every response
for a URL is the same whoever asks for it. Your web server may add
Vary: Accept-Encoding on its own, as Bandit does; CDNs handle that without trouble.