Skip to content

armgit / mapcn

Public Imported from GitHub
0

Beautiful map components. 100% Free, Zero config, one command setup.

TypeScript

lazy-load maplibre-gl

Anmol9a65ff170fc0
diff --git a/skills/mapcn/references/patterns.md b/skills/mapcn/references/patterns.md
index 213f761..afac34b 100644
--- a/skills/mapcn/references/patterns.md
+++ b/skills/mapcn/references/patterns.md
@@ -110,4 +110,4 @@ script-src 'self' https://unpkg.com;
worker-src 'self' blob:;
```
-Add your basemap host too. To self-host the worker, copy `maplibre-gl-worker.mjs` and `maplibre-gl-shared.mjs` from `node_modules/maplibre-gl/dist/` into `public/`, side by side. Then change the worker URL near the top of `map.tsx` to `MapLibreGL.setWorkerUrl("/maplibre-gl-worker.mjs")`.
+Add your basemap host too. To self-host the worker, copy `maplibre-gl-worker.mjs` and `maplibre-gl-shared.mjs` from `node_modules/maplibre-gl/dist/` into `public/`, side by side. Then change the worker URL in `loadMapLibre()` near the top of `map.tsx` to `lib.setWorkerUrl("/maplibre-gl-worker.mjs")`.
diff --git a/src/app/(main)/docs/installation/page.tsx b/src/app/(main)/docs/installation/page.tsx
index 979a53c..70be957 100644
--- a/src/app/(main)/docs/installation/page.tsx
+++ b/src/app/(main)/docs/installation/page.tsx
@@ -19,7 +19,7 @@ const selfHostWorkerCode = `cp node_modules/maplibre-gl/dist/maplibre-gl-worker.
node_modules/maplibre-gl/dist/maplibre-gl-shared.mjs \\
public/`;
-const workerUrlCode = `MapLibreGL.setWorkerUrl("/maplibre-gl-worker.mjs");`;
+const workerUrlCode = `lib.setWorkerUrl("/maplibre-gl-worker.mjs");`;
const usageCode = `import { Map, MapControls } from "@/components/ui/map";
import { Card } from "@/components/ui/card";
@@ -101,7 +101,10 @@ export default function InstallationPage() {
language="bash"
showLineNumbers={false}
/>
- <p>Then replace the unpkg URL near the top of the component:</p>
+ <p>
+ Then replace the unpkg URL in <DocsCode>loadMapLibre()</DocsCode> near
+ the top of the component:
+ </p>
<CodeBlock
code={workerUrlCode}
filename="components/ui/map.tsx"
diff --git a/src/registry/map.tsx b/src/registry/map.tsx
index 33f1831..7e830f7 100644
--- a/src/registry/map.tsx
+++ b/src/registry/map.tsx
@@ -1,6 +1,6 @@
"use client";
-import * as MapLibreGL from "maplibre-gl";
+import type * as MapLibreGL from "maplibre-gl";
import type { PopupOptions, MarkerOptions } from "maplibre-gl";
import "maplibre-gl/dist/maplibre-gl.css";
import type * as GeoJSON from "geojson";
@@ -22,12 +22,36 @@ import { X, Minus, Plus, Locate, Maximize, Loader2 } from "lucide-react";
import { cn } from "@/lib/utils";
-if (typeof window !== "undefined" && !MapLibreGL.getWorkerUrl()) {
- MapLibreGL.setWorkerUrl(
- `https://unpkg.com/maplibre-gl@${MapLibreGL.getVersion()}/dist/maplibre-gl-worker.mjs`,
- );
+type MapLibreModule = typeof MapLibreGL;
+
+let maplibre: MapLibreModule | null = null;
+let maplibrePromise: Promise<MapLibreModule> | null = null;
+
+function loadMapLibre() {
+ maplibrePromise ??= import("maplibre-gl")
+ .then((lib) => {
+ if (!lib.getWorkerUrl()) {
+ lib.setWorkerUrl(
+ `https://unpkg.com/maplibre-gl@${lib.getVersion()}/dist/maplibre-gl-worker.mjs`,
+ );
+ }
+ maplibre = lib;
+ return lib;
+ })
+ .catch((error) => {
+ maplibrePromise = null;
+ throw error;
+ });
+ return maplibrePromise;
}
+function getMapLibre() {
+ if (!maplibre) throw new Error("maplibre-gl has not loaded yet");
+ return maplibre;
+}
+
+if (typeof window !== "undefined") loadMapLibre().catch(() => {});
+
const defaultStyles = {
dark: "https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json",
light: "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json",
@@ -285,50 +309,66 @@ const Map = forwardRef<MapRef, MapProps>(function Map(
// Expose the map instance to the parent component
useImperativeHandle(ref, () => mapInstance as MapLibreGL.Map, [mapInstance]);
- // Initialize the map
+ // Initialize the map once maplibre-gl has loaded
useEffect(() => {
- if (!containerRef.current) return;
+ let cancelled = false;
+ let teardown: (() => void) | undefined;
+
+ const init = (lib: MapLibreModule) => {
+ if (cancelled || !containerRef.current) return;
+
+ const initialStyle =
+ resolvedTheme === "dark" ? mapStyles.dark : mapStyles.light;
+ currentStyleRef.current = initialStyle;
+
+ const map = new lib.Map({
+ container: containerRef.current,
+ style: initialStyle,
+ renderWorldCopies: false,
+ attributionControl: {
+ compact: true,
+ },
+ ...props,
+ ...viewport,
+ });
- const initialStyle =
- resolvedTheme === "dark" ? mapStyles.dark : mapStyles.light;
- currentStyleRef.current = initialStyle;
-
- const map = new MapLibreGL.Map({
- container: containerRef.current,
- style: initialStyle,
- renderWorldCopies: false,
- attributionControl: {
- compact: true,
- },
- ...props,
- ...viewport,
- });
+ const styleLoadHandler = () => {
+ styleSwapInFlightRef.current = false;
+ setIsStyleLoaded(true);
+ };
+ const loadHandler = () => setIsLoaded(true);
- const styleLoadHandler = () => {
- styleSwapInFlightRef.current = false;
- setIsStyleLoaded(true);
- };
- const loadHandler = () => setIsLoaded(true);
+ // Viewport change handler - skip if triggered by internal update
+ const handleMove = () => {
+ if (internalUpdateRef.current) return;
+ onViewportChangeRef.current?.(getViewport(map));
+ };
- // Viewport change handler - skip if triggered by internal update
- const handleMove = () => {
- if (internalUpdateRef.current) return;
- onViewportChangeRef.current?.(getViewport(map));
+ map.on("load", loadHandler);
+ map.on("style.load", styleLoadHandler);
+ map.on("move", handleMove);
+ setMapInstance(map);
+
+ teardown = () => {
+ map.off("load", loadHandler);
+ map.off("style.load", styleLoadHandler);
+ map.off("move", handleMove);
+ map.remove();
+ setIsLoaded(false);
+ setIsStyleLoaded(false);
+ setMapInstance(null);
+ };
};
- map.on("load", loadHandler);
- map.on("style.load", styleLoadHandler);
- map.on("move", handleMove);
- setMapInstance(map);
+ if (maplibre) init(maplibre);
+ else
+ loadMapLibre().then(init, (error) =>
+ console.error("Failed to load maplibre-gl", error),
+ );
return () => {
- map.off("load", loadHandler);
- map.off("style.load", styleLoadHandler);
- map.off("move", handleMove);
- map.remove();
- setIsLoaded(false);
- setIsStyleLoaded(false);
- setMapInstance(null);
+ cancelled = true;
+ teardown?.();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
@@ -485,7 +525,8 @@ function MapMarker({
};
const marker = useMemo(() => {
- const markerInstance = new MapLibreGL.Marker({
+ const { Marker } = getMapLibre();
+ const markerInstance = new Marker({
...markerOptions,
element: document.createElement("div"),
draggable,
@@ -644,7 +685,8 @@ function MarkerPopup({
const { offset, maxWidth } = popupOptions;
const popup = useMemo(() => {
- const popupInstance = new MapLibreGL.Popup({
+ const { Popup } = getMapLibre();
+ const popupInstance = new Popup({
offset: 16,
...popupOptions,
closeButton: false,
@@ -710,7 +752,8 @@ function MarkerTooltip({
const { offset, maxWidth } = popupOptions;
const tooltip = useMemo(() => {
- const tooltipInstance = new MapLibreGL.Popup({
+ const { Popup } = getMapLibre();
+ const tooltipInstance = new Popup({
offset: 16,
...popupOptions,
closeOnClick: true,
@@ -1042,7 +1085,8 @@ function MapPopup({
const { offset, maxWidth } = popupOptions;
const popup = useMemo(() => {
- const popupInstance = new MapLibreGL.Popup({
+ const { Popup } = getMapLibre();
+ const popupInstance = new Popup({
offset: 16,
...popupOptions,
closeButton: false,