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.

referencestoken-management.md

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

Token Management Patterns

Environment Variables (Recommended)

Different frameworks use different prefixes for client-side environment variables:

Framework/Bundler Environment Variable Access Pattern
Vite VITE_MAPBOX_ACCESS_TOKEN import.meta.env.VITE_MAPBOX_ACCESS_TOKEN
Next.js NEXT_PUBLIC_MAPBOX_TOKEN process.env.NEXT_PUBLIC_MAPBOX_TOKEN
Create React App REACT_APP_MAPBOX_TOKEN process.env.REACT_APP_MAPBOX_TOKEN
Angular environment.mapboxAccessToken Environment files (environment.ts)

Vite .env file:

VITE_MAPBOX_ACCESS_TOKEN=pk.YOUR_MAPBOX_TOKEN_HERE

Next.js .env.local file:

NEXT_PUBLIC_MAPBOX_TOKEN=pk.YOUR_MAPBOX_TOKEN_HERE

Important:

  • Always use environment variables for tokens
  • Never commit .env files to version control
  • Use public tokens (pk.*) for client-side apps
  • Add .env to .gitignore
  • Provide .env.example template for team

.gitignore:

.env
.env.local
.env.*.local

.env.example:

VITE_MAPBOX_ACCESS_TOKEN=your_token_here

Style Configuration

Default Center and Zoom Guidelines

Example defaults (used in create-web-app demos):

  • Center: [-71.05953, 42.36290] (Boston, MA)
  • Zoom: 13 for city-level view

Note: GL JS defaults to center: [0, 0] and zoom: 0 if not specified. Always set these explicitly.

Zoom level guide:

  • 0-2: World view
  • 3-5: Continent/country
  • 6-9: Region/state
  • 10-12: City view
  • 13-15: Neighborhood
  • 16-18: Street level
  • 19-22: Building level

Customizing for user location:

// Use browser geolocation
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition((position) => {
    map.setCenter([position.coords.longitude, position.coords.latitude]);
    map.setZoom(13);
  });
}

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.