How do I add a dotted world map to an Astro site?
Draw the map in the frontmatter of an Astro component with @setkernel/dotted-map-next, and render the SVG string that getSVG() returns with <Fragment set:html={svg} />. Astro runs the frontmatter at build time or on the server, so the page gets the SVG and no client JavaScript.
npm install @setkernel/dotted-map-next
The component
set:html inserts the string as HTML. That is safe here: the
library escapes every text it writes and throws on a colour or a number it
cannot draw.
src/components/WorldMap.astro---
import { DottedMap } from '@setkernel/dotted-map-next';
const map = new DottedMap({ height: 40 });
map.addPin({ lat: 19.4326, lng: -99.1332 });
const svg = map.getSVG({ title: 'Mexico City on a world map' });
---
<Fragment set:html={svg} />
src/components/WorldMap.astro when this page was built.On a page
Import the component into a page. astro build writes the map into
the page's HTML, with no <script>.
src/pages/index.astro---
import WorldMap from '../components/WorldMap.astro';
---
<html lang="en">
<head>
<meta charset="utf-8" />
<title>World map</title>
</head>
<body>
<WorldMap />
</body>
</html>