A vector basemap used to mean a tile server: a process that answers /z/x/y requests from a database or a directory of millions of small files. PMTiles removes the process. The whole tile pyramid lives in one file with an index at the front, and the client reads only the bytes it needs with ordinary HTTP range requests. Any storage that answers Range with 206 Partial Content becomes a tile host.
Cloudflare R2 is a good fit because it charges nothing for egress. This guide extracts a region from the public Protomaps planet build, uploads it to R2, configures CORS and a domain, and shows what a map actually downloads, from a request trace we recorded on 24 November 2026.
How a PMTiles read works
We pointed the pmtiles JavaScript library (4.5.0) at the full planet build from 20 November 2026, a single 139,666,835,436-byte file, and logged every request it made to read a few tiles around Austin:
| Step | Range | Bytes | Why |
|---|---|---|---|
| Open the archive | bytes=0-16383 | 16,384 B | Header (127 B) and root directory (15.5 KB) in one read |
| Read metadata | bytes=139312890401-139312891579 | 1,179 B | Layer list, attribution, Planetiler version |
| Tile 0/0/0 | leaf directory, then tile | 133,322 B + 66,795 B | First tile in an area needs its leaf directory |
| Tile 10/233/421 | leaf directory, then tile | 109,998 B + 121,658 B | Austin at zoom 10 |
| Tile 14/3743/6745 | leaf directory, then tile | 116,447 B + 82,495 B | Downtown Austin at zoom 14 |
| Tile 14/3744/6745 | tile only | 56,647 B | Neighbour: leaf directory already cached |
Three things follow from that trace:
- Opening a 140 GB archive costs 16 KB. The client reads a fixed first chunk that holds the 127-byte header and the root directory, whatever the file size.
- Each new area costs one extra read. The planet has 354 MB of leaf directories; the client fetches only the one covering the tile it needs (110–133 KB here) and caches it. The neighbouring tile at zoom 14 needed a single request.
- Tiles are compressed. Every tile in this build is gzip-compressed inside the archive (downtown Austin at z14: 82 KB stored, 112 KB decoded) and the client decompresses it.
Latency was the weak point. Across two runs, each read took between 480 ms and 2.7 s from our machine in Europe to that Cloudflare-served bucket. The Protomaps documentation itself warns that R2 tends to have higher latency than other object stores (500 ms or more) in exchange for lower cost. Plan a cache in front of it (below).
The script is short enough to run against your own archive:
import { PMTiles } from "pmtiles"
const url = process.argv[2]
const realFetch = globalThis.fetch
globalThis.fetch = async (input, init) => {
const t0 = performance.now()
const res = await realFetch(input, init)
const body = await res.arrayBuffer()
console.log(init.headers.get("range"), res.status, body.byteLength, "B", Math.round(performance.now() - t0), "ms")
return new Response(body, { status: res.status, headers: res.headers })
}
const archive = new PMTiles(url)
const h = await archive.getHeader()
console.log({ minZoom: h.minZoom, maxZoom: h.maxZoom, tiles: h.numAddressedTiles })
const tile = await archive.getZxy(14, 3743, 6745)
console.log("decoded tile bytes:", tile?.data.byteLength)Step 1: extract your region
Protomaps publishes daily planet builds at build.protomaps.com/YYYYMMDD.pmtiles (see the downloads page for the current list and retention). You do not download the planet. The go-pmtiles CLI reads just the tiles inside your bounding box over HTTP:
pmtiles extract https://build.protomaps.com/20261120.pmtiles austin.pmtiles \
--bbox=-98.05,30.05,-97.45,30.55
# fetching 4063 tiles, 140 chunks, 42 requests
# Completed in 23.3s with 4 download threads
# Extract required 52 total requests.
# Extract transferred 60 MB (overfetch 0.05) for an archive size of 57 MBThat is our real run: a 0.6° × 0.5° box around Austin, zoom 0 to 15, 4,063 tiles, a 56.9 MB file, 23 seconds. Because the planet archive is clustered, the CLI batches neighbouring tiles into large reads (52 requests for 4,063 tiles). Use --region=area.geojson for an irregular shape, and --maxzoom if you never need street-level detail; dropping the deepest zoom levels removes most of the size.
Check the result locally before uploading: pmtiles show austin.pmtiles prints the header and metadata, and pmtiles serve . serves the folder for a local MapLibre page.
Step 2: upload to R2
The R2 dashboard uploads files up to 300 MB. For anything larger, use the S3-compatible API with rclone and an R2 API token (R2 → Manage R2 API Tokens gives you the access key, secret and account endpoint):
[r2]
type = s3
provider = Cloudflare
access_key_id = YOUR_ACCESS_KEY_ID
secret_access_key = YOUR_SECRET_ACCESS_KEY
endpoint = https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.comrclone copyto austin.pmtiles r2:maps/austin-20261120.pmtiles --progressPut the build date in the object name. A map that is open while you replace a file in place can mix tiles from two builds; the JavaScript client notices a changed ETag and reloads the header, but a new filename plus a style change is cleaner and makes rollbacks trivial.
Step 3: public domain and CORS
Serve the bucket from a custom domain (bucket → Settings → Custom Domains), not the r2.dev development URL, which Cloudflare rate-limits and does not intend for production. Then allow your site’s origin to make range requests. This CORS policy is the one the Protomaps documentation gives for R2:
[
{
"AllowedOrigins": ["https://example.com"],
"AllowedMethods": ["GET", "HEAD"],
"AllowedHeaders": ["range", "if-match"],
"ExposeHeaders": ["etag"],
"MaxAgeSeconds": 3000
}
]range must be allowed or the browser’s preflight fails; etag must be exposed or the client cannot detect a replaced file. Verify from a shell:
curl -sI -H "Range: bytes=0-16383" -H "Origin: https://example.com" \
https://maps.example.com/austin-20261120.pmtiles
# expect: HTTP/2 206, content-range: bytes 0-16383/56855332, access-control-allow-originStep 4: point MapLibre at it
Register the protocol once and use pmtiles:// plus the full URL as the source. The drive-time guide has the complete, typechecked setup with the Protomaps basemap layers; the essential part is:
import { addProtocol } from "maplibre-gl"
import { Protocol } from "pmtiles"
addProtocol("pmtiles", new Protocol().tile)
// in the style:
// sources: { protomaps: { type: "vector", url: "pmtiles://https://maps.example.com/austin-20261120.pmtiles" } }Host the glyphs (font PBFs) and sprites in the same bucket. They are easy to forget, and a map without glyphs renders with no labels and a console full of 404s.
Step 5: caching
You have two designs, with different trade-offs:
- Direct range requests from the browser to the bucket’s custom domain. Fewest moving parts. Each uncached tile costs a Class B read and carries R2’s latency. Check whether your Cloudflare cache rules cache these responses: look for
cf-cache-statusin the response headers. - A Worker that decodes PMTiles and serves
/name/z/x/y.mvt, from the Protomaps Cloudflare integration. Tiles become ordinary cacheable URLs and MapLibre needs no protocol plugin. The Worker must sit on your own domain for the cache to work; aworkers.devURL does not cache. You pay for Worker requests on cache misses.
What it costs
Cloudflare’s R2 pricing page on 24 November 2026 (Standard storage):
| Item | Price | Free each month |
|---|---|---|
| Storage | $0.015 / GB-month | 10 GB-month |
| Class B operations (reads, including range reads) | $0.36 / million | 10 million |
| Class A operations (writes, lists) | $4.50 / million | 1 million |
| Egress to the internet | Free | — |
Our 57 MB Austin file costs nothing to store. Even the 139.7 GB planet is about $1.95 a month after the free 10 GB. Reads are the variable: if a typical map session pulls 40 uncached tiles plus a header and a few leaf directories, that is roughly 45 Class B operations, so the 10 million free reads cover about 220,000 uncached sessions a month, and each further million reads costs $0.36. A cache in front of the bucket lowers that further, at the price of a Worker if you go that route.
When this is the wrong choice
- Data that changes hourly. Every update is a rebuild and re-upload. Serve dynamic layers from PostGIS or GeoJSON and keep PMTiles for the static basemap.
- Lowest possible latency with no cache budget. R2 reads are slower than a CDN hit; a busy map with no cache in front will feel it.
- You need feature queries (“all parcels where…”). PMTiles serves tiles by position, not by attribute.
- You would rather not operate anything. Hosted open-data tiles such as OpenFreeMap remove the build and upload steps; read their terms first.
Always show attribution: “© OpenStreetMap contributors” for the data and the Protomaps credit for the basemap schema and styles. The archive’s own metadata carries the OpenStreetMap attribution string, but MapLibre only shows it if you create the protocol with new Protocol({ metadata: true }) or set attribution on the source yourself.