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.

referencescommon-mistakes.md

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

Common Mistakes (continued) and Testing Patterns

Mistake 4: Wrong dependency array in useEffect

// BAD - Re-creates map on every render
useEffect(() => {
  const map = new mapboxgl.Map({ ... })
  return () => map.remove()
})  // No dependency array

// BAD - Re-creates map when props change
useEffect(() => {
  const map = new mapboxgl.Map({ center: props.center, ... })
  return () => map.remove()
}, [props.center])
// GOOD - Initialize once
useEffect(() => {
  const map = new mapboxgl.Map({ ... })
  return () => map.remove()
}, [])  // Empty array = run once

// GOOD - Update map property instead
useEffect(() => {
  if (mapRef.current) {
    mapRef.current.setCenter(props.center)
  }
}, [props.center])

Why: Map initialization is expensive. Initialize once, then use map methods to update properties.


Mistake 5: Hardcoding token in source code

// BAD - Token exposed in source code
mapboxgl.accessToken = 'pk.YOUR_MAPBOX_TOKEN_HERE';
// GOOD - Use environment variable
mapboxgl.accessToken = import.meta.env.VITE_MAPBOX_ACCESS_TOKEN;

Why: Tokens in source code get committed to version control and exposed publicly. Always use environment variables.


Mistake 6: Not handling Angular SSR

// BAD - Crashes during server-side rendering
ngOnInit() {
  import('mapbox-gl').then(mapboxgl => {
    this.map = new mapboxgl.Map({ ... })
  })
}
// GOOD - Check platform first
ngOnInit() {
  if (!isPlatformBrowser(this.platformId)) {
    return  // Skip map init during SSR
  }

  import('mapbox-gl').then(mapboxgl => {
    this.map = new mapboxgl.Map({ ... })
  })
}

Why: Mapbox GL JS requires browser APIs (WebGL, Canvas). Angular Universal (SSR) will crash without platform check.


Mistake 7: Missing CSS import

// BAD - Map renders but looks broken
import mapboxgl from 'mapbox-gl';
// Missing CSS import
// GOOD - Import CSS for proper styling
import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';

Why: The CSS file contains critical styles for map controls, popups, and markers. Without it, the map appears broken.


Testing Patterns

Unit Testing Maps

Mock mapbox-gl:

// vitest.config.js or jest.config.js
export default {
  setupFiles: ['./test/setup.js']
};
// test/setup.js
vi.mock('mapbox-gl', () => ({
  default: {
    Map: vi.fn(() => ({
      on: vi.fn(),
      remove: vi.fn(),
      setCenter: vi.fn(),
      setZoom: vi.fn()
    })),
    accessToken: ''
  }
}));

Why: Mapbox GL JS requires WebGL and browser APIs that don't exist in test environments. Mock the library to test component logic.

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.