All skills
mapbox avatar

/mapbox-web-integration-patterns

@f5ae7de official
by mapboxmapbox/mapbox-agent-skills80 stars
17

Official integration patterns for Mapbox GL JS across popular web frameworks (React, Vue, Svelte, Angular). Covers setup, lifecycle management, token handling, search integration, and common pitfalls. Based on Mapbox's create-web-app scaffolding tool.

Use this Skill: https://skilld.dev/gh/mapbox/mapbox-agent-skills/mapbox-web-integration-patterns

This session only. Nothing lands on disk.

referencesnextjs.md

≈346 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Next.js Specific Patterns

App Router (Recommended)

'use client'  // Mark as client component

import { useRef, useEffect } from 'react'
import mapboxgl from 'mapbox-gl'
import 'mapbox-gl/dist/mapbox-gl.css'

export default function Map() {
  const mapRef = useRef<mapboxgl.Map>()
  const mapContainerRef = useRef<HTMLDivElement>(null)

  useEffect(() => {
    if (!mapContainerRef.current) return

    mapboxgl.accessToken = process.env.NEXT_PUBLIC_MAPBOX_TOKEN!

    mapRef.current = new mapboxgl.Map({
      container: mapContainerRef.current,
      center: [-71.05953, 42.36290],
      zoom: 13
    })

    return () => mapRef.current?.remove()
  }, [])

  return <div ref={mapContainerRef} style={{ height: '100vh' }} />
}

Key points:

  • Must use 'use client' directive (maps require browser APIs)
  • Use process.env.NEXT_PUBLIC_* for environment variables
  • Type mapRef properly with TypeScript

Pages Router (Legacy)

import dynamic from 'next/dynamic'

// Dynamically import to disable SSR for map component
const Map = dynamic(() => import('../components/Map'), {
  ssr: false,
  loading: () => <p>Loading map...</p>
})

export default function HomePage() {
  return <Map />
}

Key points:

  • Use dynamic import with ssr: false
  • Provide loading state
  • Map component itself follows standard React pattern

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill contains safe, standard documentation, integration patterns, and best practices for Mapbox GL JS across various web frameworks.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    1/2 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at f5ae7de. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 9 hours ago.

Activeupdated 2 months ago

README badge

README badge for mapbox/mapbox-agent-skills/mapbox-web-integration-patterns

Provides official integration patterns for Mapbox GL JS across React, Vue, Svelte, Angular, and vanilla JavaScript, with lifecycle management, token handling, and Search JS integration. Covers setup in each framework, cleanup to prevent memory leaks, and common mistakes like storing map instances in Vue's data() or forgetting map.remove() calls.

Generated from the current SKILL.md.

Does this skill cover all major web frameworks?
Yes. It covers React, Vue, Svelte, Angular, Next.js, vanilla JavaScript, and Web Components. Framework-specific patterns are documented in reference files (e.g., references/vue.md, references/angular.md).
What version of Mapbox GL JS should I use?
v3.x is recommended (minimum v3.0.0). v2.x is legacy and no longer actively developed. v3.x requires WebGL 2 and has improved TypeScript types, but core initialization patterns work the same in both versions.
How do I handle the Mapbox access token securely?
Store it in environment variables (e.g., VITE_MAPBOX_ACCESS_TOKEN for Vite, REACT_APP_MAPBOX_TOKEN for Create React App). Set it via mapboxgl.accessToken or pass it per-map in the constructor. The skill has a dedicated reference file on token management per bundler.
What's the most common mistake when integrating Mapbox?
Forgetting to call map.remove() in cleanup functions. Without it, WebGL contexts and event listeners accumulate, causing memory leaks. The skill documents this and three other critical mistakes with examples.
Can I use Mapbox Search JS with these patterns?
Yes. The skill includes React + Search JS integration examples and covers SearchBox configuration, positioning, and the required packages (@mapbox/search-js-react for React, @mapbox/search-js-web for other frameworks).

Generated from the current SKILL.md. These answers refresh after source changes.