dotted‑world‑map

Can I render a dotted map on the server or at the edge?

Yes: @setkernel/dotted-map-next uses no DOM, no canvas and no Node.js modules, so getSVG() returns the same SVG string in Node.js, in an edge function and in the browser. Write the string to a file, send it as image/svg+xml, or put it in the HTML your server renders.

npm install @setkernel/dotted-map-next

Node.js: write a file

Run it with node write-map.mjs. This page is built the same way: every map on it is drawn by its code, in Node.js, when the site is built.

write-map.mjs
import { writeFileSync } from 'node:fs';
import { DottedMap } from '@setkernel/dotted-map-next';

const map = new DottedMap({ height: 40 });
map.addPin({ lat: 52.52, lng: 13.405 });
const svg = map.getSVG({ color: '#8c959f', title: 'Berlin on a world map' });

writeFileSync('map.svg', svg);
Berlin on a world map
Drawn by write-map.mjs when this page was built.

An edge function

The fetch handler of a module worker, the format Cloudflare Workers runs. The map is drawn once, when the worker starts, and each request gets the same string.

worker.mjs
import { DottedMap } from '@setkernel/dotted-map-next';

const svg = new DottedMap({ height: 40 }).getSVG({ color: '#8c959f' });

export default {
  fetch() {
    return new Response(svg, {
      headers: { 'content-type': 'image/svg+xml' },
    });
  },
};
Drawn by worker.mjs when this page was built.

The precomputed entry

The first map with given settings computes its grid, about 40 ms for the world at height: 60 in a new Node.js process, and caches it. Where each start counts, compute the grid once, at build time, with getMapJSON(), and draw it with the without-countries entry, which holds no country data.

make-map.mjs
import { writeFileSync } from 'node:fs';
import { getMapJSON } from '@setkernel/dotted-map-next';

writeFileSync('world.json', getMapJSON({ height: 60 }));
light-worker.mjs
import { DottedMap } from '@setkernel/dotted-map-next/without-countries';
import world from './world.json' with { type: 'json' };

const svg = new DottedMap({ map: world }).getSVG({ color: '#8c959f' });

export default {
  fetch() {
    return new Response(svg, {
      headers: { 'content-type': 'image/svg+xml' },
    });
  },
};
Drawn by make-map.mjs and light-worker.mjs when this page was built.

Bundled with its dependencies and the world at height: 60, that worker's map code is 7,549 bytes gzipped, against 43,525 bytes for the main entry and its dependencies. The map's settings are fixed when it is precomputed: pins and every getSVG() option still work.