mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 23:04:59 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+316
-239
@@ -1,262 +1,339 @@
|
||||
---
|
||||
description: This rule provides guidelines for Google Maps JavaScript API development, covering code organization, performance, security, testing, and common pitfalls. It promotes best practices to ensure efficient, secure, and maintainable map applications.
|
||||
globs: *.js,*.jsx,*.ts,*.tsx,*.html,*.css
|
||||
description: This guide provides definitive best practices for integrating and managing the Google Maps JavaScript API in modern web applications, focusing on security, performance, and maintainability.
|
||||
globs: **/*.{js,jsx}
|
||||
---
|
||||
# Google Maps JavaScript API Best Practices
|
||||
# google-maps-js Best Practices
|
||||
|
||||
This document outlines best practices for developing applications using the Google Maps JavaScript API. Following these guidelines will help you create efficient, maintainable, and secure mapping solutions.
|
||||
This document outlines the definitive guidelines for working with the Google Maps JavaScript API. Adhering to these practices ensures secure, performant, and maintainable map-centric applications.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
### 1.1. Directory Structure
|
||||
Encapsulate map logic to keep components clean and reusable. Avoid direct global `google.maps` object manipulation within React components.
|
||||
|
||||
### ✅ GOOD: Custom Hooks for Map Logic (React)
|
||||
|
||||
Abstract map initialization and interactions into custom hooks.
|
||||
|
||||
```jsx
|
||||
// hooks/useGoogleMap.js
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { Loader } from '@googlemaps/js-api-loader';
|
||||
|
||||
const API_KEY = process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY; // Securely load API key
|
||||
|
||||
export function useGoogleMap(mapContainerRef, options) {
|
||||
const [map, setMap] = useState(null);
|
||||
const loaderRef = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!mapContainerRef.current || !API_KEY) return;
|
||||
|
||||
if (!loaderRef.current) {
|
||||
loaderRef.current = new Loader({
|
||||
apiKey: API_KEY,
|
||||
version: 'weekly', // Use 'weekly' for latest features, 'quarterly' for stability
|
||||
// Do NOT specify 'libraries' here unless absolutely necessary for caching.
|
||||
// Prefer dynamic importLibrary() calls.
|
||||
});
|
||||
}
|
||||
|
||||
Adopt a clear and consistent directory structure to improve code maintainability and collaboration. A suggested structure is:
|
||||
const initMap = async () => {
|
||||
try {
|
||||
const { Map } = await loaderRef.current.importLibrary('maps');
|
||||
const newMap = new Map(mapContainerRef.current, {
|
||||
center: { lat: -34.397, lng: 150.644 },
|
||||
zoom: 8,
|
||||
mapId: 'YOUR_MAP_ID', // Always use a Map ID
|
||||
...options,
|
||||
});
|
||||
setMap(newMap);
|
||||
} catch (error) {
|
||||
console.error('Error loading Google Maps:', error);
|
||||
// Implement user-facing error message, e.g., show a fallback UI
|
||||
}
|
||||
};
|
||||
|
||||
initMap();
|
||||
|
||||
// Cleanup: Google Maps API doesn't have a direct 'destroy' method for Map instances
|
||||
// but unmounting the container and nulling the map reference helps.
|
||||
return () => {
|
||||
setMap(null); // Clear map instance on unmount
|
||||
};
|
||||
}, [mapContainerRef, options]);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
// components/MyMapComponent.jsx
|
||||
import React, { useRef, useEffect } from 'react';
|
||||
import { useGoogleMap } from '../hooks/useGoogleMap';
|
||||
|
||||
export function MyMapComponent() {
|
||||
const mapRef = useRef(null);
|
||||
const map = useGoogleMap(mapRef, { zoom: 10, center: { lat: 34.0522, lng: -118.2437 } });
|
||||
|
||||
useEffect(() => {
|
||||
if (map) {
|
||||
// Map is loaded, add markers, listeners, etc.
|
||||
console.log('Map loaded:', map);
|
||||
const marker = new google.maps.marker.AdvancedMarkerElement({
|
||||
map,
|
||||
position: map.getCenter(),
|
||||
title: 'Hello World',
|
||||
});
|
||||
// Remember to clean up markers/overlays if they are not managed by the map itself
|
||||
}
|
||||
}, [map]);
|
||||
|
||||
return <div ref={mapRef} style={{ height: '500px', width: '100%' }} />;
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Security
|
||||
|
||||
API key security is paramount. Never expose your API keys directly in client-side code.
|
||||
|
||||
### ❌ BAD: Hardcoding API Keys
|
||||
|
||||
```javascript
|
||||
// In a client-side JS file
|
||||
const API_KEY = "YOUR_HARDCODED_API_KEY_HERE"; // EXPOSED!
|
||||
const loader = new Loader({ apiKey: API_KEY });
|
||||
```
|
||||
|
||||
### ✅ GOOD: Secure API Key Management
|
||||
|
||||
Store API keys as environment variables and restrict them in the Google Cloud Console.
|
||||
|
||||
```javascript
|
||||
// .env.local (for Next.js/Vite, etc.)
|
||||
NEXT_PUBLIC_GOOGLE_MAPS_API_KEY=AIzaSy...
|
||||
|
||||
// In your application code (e.g., useGoogleMap.js)
|
||||
const API_KEY = process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY;
|
||||
|
||||
// Google Cloud Console: Always restrict API keys by HTTP referrer (your domain)
|
||||
// or by application (Android/iOS package names).
|
||||
// Use separate keys for different projects/environments to isolate failures.
|
||||
```
|
||||
|
||||
## 3. Loading the API
|
||||
|
||||
Always use modern, performant methods for loading the Maps JavaScript API.
|
||||
|
||||
### ❌ BAD: Synchronous Script Tag
|
||||
|
||||
```html
|
||||
<!-- Loads all libraries upfront, blocks rendering -->
|
||||
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>
|
||||
```
|
||||
|
||||
### ✅ GOOD: Dynamic Library Import with `js-api-loader`
|
||||
|
||||
project-root/
|
||||
src/
|
||||
components/
|
||||
MapComponent.js # or .jsx, .ts, .tsx
|
||||
MarkerComponent.js
|
||||
InfoWindowComponent.js
|
||||
services/
|
||||
mapService.js # Handles API calls and data processing
|
||||
geolocationService.js
|
||||
utils/
|
||||
utils.js # Helper functions
|
||||
styles/
|
||||
MapComponent.css
|
||||
App.js # Main application component
|
||||
public/
|
||||
index.html # HTML entry point
|
||||
.env # API keys and environment variables
|
||||
package.json # Dependencies and scripts
|
||||
webpack.config.js # or equivalent
|
||||
This allows lazy loading of specific libraries as needed, reducing initial bundle size and improving page load performance.
|
||||
|
||||
```javascript
|
||||
import { Loader } from '@googlemaps/js-api-loader';
|
||||
|
||||
### 1.2. File Naming Conventions
|
||||
const loader = new Loader({
|
||||
apiKey: process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY,
|
||||
version: 'weekly',
|
||||
});
|
||||
|
||||
* **Components:** Use PascalCase (e.g., `MapComponent.js`).
|
||||
* **Services/Utilities:** Use camelCase (e.g., `mapService.js`, `geolocationService.js`).
|
||||
* **Styles:** Match component names (e.g., `MapComponent.css`).
|
||||
* **Images/Assets:** Use kebab-case (e.g., `custom-marker.png`).
|
||||
async function initializeMapAndPlaces() {
|
||||
try {
|
||||
const { Map } = await loader.importLibrary('maps');
|
||||
const { AdvancedMarkerElement } = await loader.importLibrary('marker');
|
||||
const { AutocompleteService } = await loader.importLibrary('places');
|
||||
|
||||
### 1.3. Module Organization
|
||||
const map = new Map(document.getElementById('map'), { /* ... */ });
|
||||
const marker = new AdvancedMarkerElement({ map, /* ... */ });
|
||||
const autocompleteService = new AutocompleteService();
|
||||
// ... use services
|
||||
} catch (error) {
|
||||
console.error('Failed to load Google Maps libraries:', error);
|
||||
}
|
||||
}
|
||||
|
||||
* **ES Modules:** Use `import` and `export` for modularity.
|
||||
* **Single Responsibility Principle:** Each module should have a specific purpose.
|
||||
* **Avoid Circular Dependencies:** Refactor code to eliminate circular dependencies between modules.
|
||||
initializeMapAndPlaces();
|
||||
```
|
||||
|
||||
### 1.4. Component Architecture
|
||||
## 4. Event Handling
|
||||
|
||||
* **Component-Based Approach:** Break down the UI into reusable components (Map, Marker, InfoWindow, etc.).
|
||||
* **Presentational and Container Components:** Separate concerns: presentational components focus on UI, while container components handle logic and data.
|
||||
* **Props and State:** Use props to pass data down to components and state to manage component-specific data.
|
||||
Manage event listeners carefully to prevent memory leaks and ensure proper cleanup.
|
||||
|
||||
### 1.5. Code Splitting
|
||||
### ❌ BAD: Unmanaged Event Listeners
|
||||
|
||||
* **Dynamic Imports:** Use dynamic imports (`import()`) to load map-related code chunks on demand.
|
||||
* **Webpack/Parcel/Rollup:** Configure your bundler to create separate chunks for different parts of the application.
|
||||
* **Route-Based Splitting:** If the map is only used on specific routes, load the map-related code only when the route is accessed.
|
||||
```javascript
|
||||
// In a React component, without cleanup
|
||||
useEffect(() => {
|
||||
if (map) {
|
||||
map.addListener('click', (e) => {
|
||||
console.log('Map clicked at:', e.latLng.toString());
|
||||
});
|
||||
// This listener will persist even if the component unmounts
|
||||
}
|
||||
}, [map]);
|
||||
```
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
### ✅ GOOD: Managed Event Listeners with Cleanup
|
||||
|
||||
### 2.1. Design Patterns
|
||||
Use `google.maps.event.addListener` and store the listener handle for later removal with `google.maps.event.removeListener`. In React, this is handled in `useEffect`'s cleanup function.
|
||||
|
||||
* **Observer Pattern:** Use the Observer pattern to handle map events (e.g., marker clicks, map bounds changes). This decouples event sources from event handlers.
|
||||
* **Singleton Pattern:** Use a Singleton pattern (carefully) for global map services to ensure a single instance across the application. Be mindful of testability.
|
||||
* **Factory Pattern:** Employ the Factory pattern to create different types of markers or overlays based on data.
|
||||
```javascript
|
||||
useEffect(() => {
|
||||
if (map) {
|
||||
const clickListener = map.addListener('click', (e) => {
|
||||
console.log('Map clicked at:', e.latLng.toString());
|
||||
});
|
||||
|
||||
### 2.2. Recommended Approaches for Common Tasks
|
||||
const dragEndListener = map.addListener('dragend', () => {
|
||||
console.log('Map dragged to:', map.getCenter().toString());
|
||||
});
|
||||
|
||||
* **Loading the API:** Asynchronously load the Google Maps JavaScript API using a Promise. Use `async/await` syntax for cleaner code.
|
||||
* **Marker Management:** Use a data-driven approach for marker creation and updates. Store marker data in an array and iterate over it to create markers.
|
||||
* **InfoWindows:** Create InfoWindow instances outside of loops to avoid memory leaks. Update the content of the existing InfoWindow instead of creating new ones.
|
||||
* **Event Handling:** Use event listeners provided by the API (e.g., `map.addListener('click', ...)`). Avoid inline event handlers.
|
||||
|
||||
### 2.3. Anti-patterns and Code Smells
|
||||
|
||||
* **Direct DOM Manipulation:** Avoid directly manipulating the DOM inside map components. Use the API's methods for adding and updating elements.
|
||||
* **Global Variables:** Minimize the use of global variables to prevent naming conflicts and improve code encapsulation.
|
||||
* **Nested Callbacks:** Avoid deeply nested callbacks (callback hell). Use Promises and `async/await` for asynchronous operations.
|
||||
* **Tight Coupling:** Design components to be loosely coupled, making them easier to test and reuse.
|
||||
|
||||
### 2.4. State Management
|
||||
|
||||
* **Local Component State:** Use React's `useState` hook or similar for component-specific state.
|
||||
* **Context API:** Use the Context API for sharing global map configurations or data across components.
|
||||
* **Redux/Mobx:** For larger applications, consider using a state management library like Redux or Mobx to manage complex application state.
|
||||
|
||||
### 2.5. Error Handling
|
||||
|
||||
* **API Loading Errors:** Handle errors that occur during API loading (e.g., invalid API key, network issues).
|
||||
* **Geolocation Errors:** Gracefully handle geolocation errors (e.g., user denied permission, browser doesn't support geolocation).
|
||||
* **Try-Catch Blocks:** Use `try-catch` blocks to catch and handle exceptions during API calls and data processing.
|
||||
* **User Notifications:** Display user-friendly error messages instead of crashing the application.
|
||||
* **Logging:** Implement logging to track errors and debug issues.
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### 3.1. Optimization Techniques
|
||||
|
||||
* **Raster Images for Markers:** Use raster images (PNG, JPG) instead of SVG for a large number of custom markers (hundreds or more) for optimal performance.
|
||||
* **Marker Clustering:** Implement marker clustering for maps with a high density of markers. Libraries like `markerclustererplus` can help.
|
||||
* **Viewport Optimization:** Only render markers and overlays that are visible within the current viewport (map bounds). This reduces the number of DOM elements and improves rendering performance.
|
||||
* **Debouncing/Throttling:** Use debouncing or throttling to limit the frequency of map updates in response to user interactions (e.g., panning, zooming).
|
||||
* **Custom Overlays Judiciously:** Be mindful when using custom overlays. Avoid heavy computations in the `OverlayView.draw()` method, as it's called on every pan and zoom. Defer adding and removing content until the map is stationary.
|
||||
|
||||
### 3.2. Memory Management
|
||||
|
||||
* **Remove Event Listeners:** Remove event listeners when they are no longer needed to prevent memory leaks.
|
||||
* **Release Resources:** Release resources (e.g., marker instances, InfoWindow instances) when they are no longer in use.
|
||||
* **Avoid Unnecessary DOM Updates:** Minimize the number of DOM updates to improve rendering performance.
|
||||
|
||||
### 3.3. Rendering Optimization
|
||||
|
||||
* **Hardware Acceleration:** Ensure that hardware acceleration is enabled in the browser for smoother map rendering.
|
||||
* **CSS Transitions:** Use CSS transitions for smooth animations when updating map elements.
|
||||
* **WebGL:** Explore WebGL features for advanced visualizations and rendering (if applicable).
|
||||
|
||||
### 3.4. Bundle Size Optimization
|
||||
|
||||
* **Tree Shaking:** Use tree shaking to remove unused code from the bundle. Ensure your bundler is configured correctly for tree shaking.
|
||||
* **Code Splitting:** Split the code into smaller chunks to reduce the initial load time.
|
||||
* **Minification:** Minify the code to reduce the bundle size.
|
||||
* **Compression:** Compress the bundle using Gzip or Brotli.
|
||||
|
||||
### 3.5. Lazy Loading
|
||||
|
||||
* **Lazy Loading of Map Component:** Load the map component only when it is needed (e.g., when a specific route is accessed).
|
||||
* **Lazy Loading of Markers:** Load markers only when they are visible within the viewport.
|
||||
|
||||
## 4. Security Best Practices
|
||||
|
||||
### 4.1. Common Vulnerabilities
|
||||
|
||||
* **API Key Exposure:** Protect your API key and prevent it from being exposed in client-side code or version control. Restrict API key usage to specific domains or IP addresses.
|
||||
* **Cross-Site Scripting (XSS):** Sanitize user input to prevent XSS attacks.
|
||||
* **Clickjacking:** Protect against clickjacking attacks by setting the `X-Frame-Options` header.
|
||||
* **Data Injection:** Validate data received from the Google Maps API to prevent data injection attacks.
|
||||
|
||||
### 4.2. Input Validation
|
||||
|
||||
* **Validate User Input:** Validate user input (e.g., addresses, coordinates) before passing it to the Google Maps API.
|
||||
* **Sanitize Data:** Sanitize data received from the Google Maps API before displaying it to the user.
|
||||
* **Rate Limiting:** Implement rate limiting to prevent abuse of the API.
|
||||
|
||||
### 4.3. Authentication and Authorization
|
||||
|
||||
* **Secure API Key Storage:** Never store API keys directly in client-side code. Use environment variables or a backend proxy to manage API keys.
|
||||
* **Restrict API Key Usage:** Restrict API key usage to specific domains or IP addresses in the Google Cloud Console.
|
||||
* **Backend Authentication:** Implement backend authentication for sensitive operations (e.g., geocoding, directions). This prevents unauthorized access to the API.
|
||||
|
||||
### 4.4. Data Protection
|
||||
|
||||
* **HTTPS:** Always use HTTPS to encrypt data transmitted between the client and the server.
|
||||
* **Data Encryption:** Encrypt sensitive data stored on the server.
|
||||
* **Data Minimization:** Only collect and store the data that is necessary for the application.
|
||||
|
||||
### 4.5. Secure API Communication
|
||||
|
||||
* **Use HTTPS:** Always use HTTPS for API requests.
|
||||
* **Validate Responses:** Validate the responses received from the API.
|
||||
* **Handle Errors:** Properly handle errors returned by the API.
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
### 5.1. Unit Testing
|
||||
|
||||
* **Test Individual Components:** Write unit tests for individual map components (MapComponent, MarkerComponent, etc.).
|
||||
* **Mock API Calls:** Mock API calls to isolate components from external dependencies.
|
||||
* **Test Event Handling:** Test event handling logic (e.g., marker click events).
|
||||
|
||||
### 5.2. Integration Testing
|
||||
|
||||
* **Test Component Interactions:** Write integration tests to verify that components work together correctly.
|
||||
* **Test API Integrations:** Test the integration with the Google Maps API.
|
||||
* **Use a Test Environment:** Use a test environment to avoid making changes to the production environment.
|
||||
|
||||
### 5.3. End-to-End Testing
|
||||
|
||||
* **Simulate User Interactions:** Simulate user interactions to test the entire application flow.
|
||||
* **Verify Functionality:** Verify that the application functions as expected.
|
||||
* **Use a Testing Framework:** Use a testing framework like Cypress or Puppeteer for end-to-end testing.
|
||||
|
||||
### 5.4. Test Organization
|
||||
|
||||
* **Directory Structure:** Maintain a clear directory structure for tests.
|
||||
* **Test Naming:** Use descriptive names for tests.
|
||||
* **Test Descriptions:** Provide clear descriptions of what each test does.
|
||||
|
||||
### 5.5. Mocking and Stubbing
|
||||
|
||||
* **Mock the Google Maps API:** Mock the Google Maps API to isolate components during testing.
|
||||
* **Use Mocking Libraries:** Use mocking libraries like Jest or Sinon to create mocks and stubs.
|
||||
* **Control Test Data:** Use stubs to control the data returned by the API during testing.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
### 6.1. Frequent Mistakes
|
||||
|
||||
* **Forgetting to Enable Billing:** Ensure that billing is enabled for your Google Maps Platform project.
|
||||
* **Exceeding API Usage Limits:** Monitor API usage and optimize code to avoid exceeding usage limits.
|
||||
* **Using Deprecated Features:** Avoid using deprecated features of the API.
|
||||
* **Ignoring Browser Compatibility:** Test the application in different browsers to ensure compatibility.
|
||||
|
||||
### 6.2. Edge Cases
|
||||
|
||||
* **Handling No Geolocation Support:** Gracefully handle the case where the browser doesn't support geolocation.
|
||||
* **Handling Network Errors:** Handle network errors that occur during API calls.
|
||||
* **Handling Invalid Data:** Handle invalid data returned by the API.
|
||||
|
||||
### 6.3. Version-Specific Issues
|
||||
|
||||
* **Check the API Version:** Be aware of the API version you are using and any version-specific issues.
|
||||
* **Read the Release Notes:** Read the release notes for new versions of the API to be aware of any changes.
|
||||
|
||||
### 6.4. Compatibility Concerns
|
||||
|
||||
* **Library Conflicts:** Avoid using libraries that are known to conflict with the Google Maps JavaScript API (e.g., Prototype, older versions of MooTools and DateJS).
|
||||
* **CSS Conflicts:** Avoid CSS conflicts by using specific CSS classes for map elements and avoiding overriding internal API styles.
|
||||
|
||||
### 6.5. Debugging Strategies
|
||||
|
||||
* **Use Browser Developer Tools:** Use browser developer tools to debug JavaScript code, inspect network requests, and analyze performance.
|
||||
* **Check the Console:** Check the console for error messages and warnings.
|
||||
* **Use Debugging Tools:** Use debugging tools like `console.log` or a debugger to step through the code and inspect variables.
|
||||
* **Read the Documentation:** Refer to the Google Maps JavaScript API documentation for troubleshooting information.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
### 7.1. Recommended Development Tools
|
||||
|
||||
* **Code Editor:** Use a code editor like VS Code, Sublime Text, or Atom.
|
||||
* **Browser Developer Tools:** Use browser developer tools for debugging and performance analysis.
|
||||
* **Bundler:** Use a bundler like Webpack, Parcel, or Rollup.
|
||||
* **Linting Tools:** Use linting tools like ESLint to enforce code style and identify potential errors.
|
||||
|
||||
### 7.2. Build Configuration
|
||||
|
||||
* **Configure the Bundler:** Configure the bundler to optimize code for production (e.g., minification, compression).
|
||||
* **Use Environment Variables:** Use environment variables to manage API keys and other configuration settings.
|
||||
* **Set Up Build Scripts:** Set up build scripts to automate the build process.
|
||||
|
||||
### 7.3. Linting and Formatting
|
||||
|
||||
* **ESLint:** Use ESLint to enforce code style and identify potential errors.
|
||||
* **Prettier:** Use Prettier to automatically format code.
|
||||
* **Code Style Guide:** Follow a consistent code style guide (e.g., Airbnb, Google).
|
||||
|
||||
### 7.4. Deployment
|
||||
|
||||
* **Deploy to a Web Server:** Deploy the application to a web server (e.g., Netlify, Vercel, AWS S3).
|
||||
* **Use HTTPS:** Ensure that the application is served over HTTPS.
|
||||
* **Configure Caching:** Configure caching to improve performance.
|
||||
|
||||
### 7.5. CI/CD Integration
|
||||
|
||||
* **Set Up a CI/CD Pipeline:** Set up a CI/CD pipeline to automate the build, test, and deployment process.
|
||||
* **Use a CI/CD Tool:** Use a CI/CD tool like Jenkins, Travis CI, or CircleCI.
|
||||
* **Automated Testing:** Automate testing as part of the CI/CD pipeline.
|
||||
|
||||
By following these best practices, you can develop robust, efficient, and maintainable Google Maps JavaScript API applications.
|
||||
// Cleanup function: remove listeners when component unmounts or dependencies change
|
||||
return () => {
|
||||
google.maps.event.removeListener(clickListener);
|
||||
google.maps.event.removeListener(dragEndListener);
|
||||
};
|
||||
}
|
||||
}, [map]);
|
||||
```
|
||||
|
||||
## 5. Performance Considerations
|
||||
|
||||
Optimize map rendering and API requests to ensure a smooth user experience.
|
||||
|
||||
### 5.1 Marker Management
|
||||
|
||||
For applications with many markers, implement clustering or only render visible markers.
|
||||
|
||||
### ✅ GOOD: Marker Clustering
|
||||
|
||||
Use a helper library like `@googlemaps/markerclusterer` for efficient marker management.
|
||||
|
||||
```javascript
|
||||
import { MarkerClusterer } from '@googlemaps/markerclusterer';
|
||||
|
||||
async function addClusteredMarkers(map, locations) {
|
||||
const { AdvancedMarkerElement } = await loader.importLibrary('marker');
|
||||
const markers = locations.map(loc => new AdvancedMarkerElement({ position: loc }));
|
||||
|
||||
new MarkerClusterer({ map, markers });
|
||||
}
|
||||
```
|
||||
|
||||
### 5.2 Debouncing/Throttling Map Events
|
||||
|
||||
Avoid excessive re-renders or API calls on continuous events like `mousemove` or `drag`.
|
||||
|
||||
### ✅ GOOD: Debouncing `idle` Event
|
||||
|
||||
```javascript
|
||||
import { debounce } from 'lodash'; // Or implement your own debounce utility
|
||||
|
||||
useEffect(() => {
|
||||
if (map) {
|
||||
const handleMapIdle = debounce(() => {
|
||||
const center = map.getCenter();
|
||||
const zoom = map.getZoom();
|
||||
console.log(`Map idle. Center: ${center.toString()}, Zoom: ${zoom}`);
|
||||
// Trigger API calls or state updates here (e.g., fetch new data for current viewport)
|
||||
}, 500); // Wait 500ms after map stops moving
|
||||
|
||||
const idleListener = map.addListener('idle', handleMapIdle);
|
||||
|
||||
return () => {
|
||||
google.maps.event.removeListener(idleListener);
|
||||
handleMapIdle.cancel(); // Cancel any pending debounced calls
|
||||
};
|
||||
}
|
||||
}, [map]);
|
||||
```
|
||||
|
||||
## 6. Request/Response Patterns & Rate Limiting
|
||||
|
||||
All web service requests must be asynchronous, use HTTPS, and implement robust error handling with exponential backoff.
|
||||
|
||||
### ❌ BAD: Synchronous Requests & No Backoff
|
||||
|
||||
```javascript
|
||||
// This is a conceptual BAD example, as browser JS typically prevents synchronous XHR to external domains.
|
||||
// The core issue is making repeated, unmanaged requests to web services.
|
||||
function getGeolocation(location) {
|
||||
// ... make direct fetch call without retry logic ...
|
||||
// This will fail on transient errors and could hit rate limits quickly.
|
||||
}
|
||||
```
|
||||
|
||||
### ✅ GOOD: Asynchronous Requests with Exponential Backoff
|
||||
|
||||
For server-side or client-side web service calls (e.g., Geocoding, Places API *web services*), implement exponential backoff.
|
||||
|
||||
```javascript
|
||||
async function fetchWithExponentialBackoff(url, maxRetries = 5, initialDelay = 100) {
|
||||
let delay = initialDelay;
|
||||
for (let i = 0; i < maxRetries; i++) {
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
// Handle 4XX/5XX errors
|
||||
if (response.status >= 400 && response.status < 500 && response.status !== 429) {
|
||||
// Client errors (e.g., bad request, unauthorized) should not be retried
|
||||
throw new Error(`API Error: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
// Server errors or rate limits (429) should be retried
|
||||
throw new Error(`Transient API Error: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.warn(`Attempt ${i + 1} failed: ${error.message}. Retrying in ${delay / 1000}s...`);
|
||||
if (i === maxRetries - 1) throw error; // Re-throw after last retry
|
||||
await new Promise(resolve => setTimeout(resolve, delay));
|
||||
delay *= 2; // Exponential increase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Example usage for a Geocoding API call (server-side recommended for API keys)
|
||||
async function geocodeAddress(address) {
|
||||
const encodedAddress = encodeURIComponent(address);
|
||||
const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}&key=${process.env.GOOGLE_MAPS_SERVER_API_KEY}`;
|
||||
try {
|
||||
const data = await fetchWithExponentialBackoff(url);
|
||||
if (data.status === 'OK') {
|
||||
return data.results[0].geometry.location;
|
||||
} else {
|
||||
throw new Error(`Geocoding failed: ${data.status} - ${data.error_message || 'Unknown error'}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Final Geocoding error:', error);
|
||||
// Notify user or log for investigation
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 6.1 URL Encoding
|
||||
|
||||
Always URL-encode parameters for web service requests, especially before signing if applicable.
|
||||
|
||||
```javascript
|
||||
// ❌ BAD: Unencoded parameters can break requests or lead to incorrect results
|
||||
const address = "5th&Main St.";
|
||||
const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=...`;
|
||||
|
||||
// ✅ GOOD: Properly encoded parameters
|
||||
const address = "5th&Main St.";
|
||||
const encodedAddress = encodeURIComponent(address); // "5th%26Main%20St."
|
||||
const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}&key=...`;
|
||||
```
|
||||
|
||||
## 7. Common Pitfalls and Gotchas
|
||||
|
||||
### 7.1 Map ID Usage
|
||||
|
||||
Always use a Map ID when initializing a map. This enables cloud-based map styling and future features.
|
||||
|
||||
```javascript
|
||||
// ❌
|
||||
Reference in New Issue
Block a user