This Salesforce lightning component code works great on Chrome but in Mozilla Firefox the on click marker titles do not work and a popup error appears on load that states:
Resolve this error so this code operates as expected in both browsers
The issue you are experiencing with the Salesforce Lightning component in Firefox, specifically the error stating [L is not defined], is likely related to how the Leaflet library is being loaded or used in that browser. This error typically occurs when the Leaflet library (L) is not properly initialized or is not accessible in the current scope. Below are steps and modifications to help troubleshoot and resolve the issue:
Steps to Resolve the Issue:
Ensure Leaflet Library is Loaded:
First, verify that the Leaflet library is successfully loaded before any code tries to use it. In the connectedCallback method of your mapOfThingsMap.js file, you are loading Leaflet libraries using loadScript:
Make sure that all these scripts are loading correctly without any errors. You can check this in the Firefox Developer Tools under the Network tab.
Check for Scope Issues:
If the Leaflet library is loaded correctly but still throws errors, it may be due to scope issues. Ensure that you are using the Leaflet (L) object properly. Sometimes, certain operations may execute before the library is fully ready.
If you are using any asynchronous code that depends on Leaflet, make sure it runs only after Leaflet is fully loaded.
Inspect the Load Order:
Sometimes, the order of loading scripts can affect their availability. Make sure that the Leaflet script is loaded before any other scripts that depend on it. You can add debug statements after loading scripts to confirm they load in the right order.
Add Fallback for Leaflet Initialization:
In your drawMap() function (in drawmap.js), you might want to add checks to ensure that L is defined before trying to use it. You can do this by wrapping your Leaflet code in a conditional block:
javascript
export function drawMap() {
console.log("start drawing map");
if (typeof L === 'undefined') {
console.error("Leaflet library is not loaded.");
return; // Stop execution if Leaflet is not loaded
}
const container = this.template.querySelector(MAP_CONTAINER);
this.map = L.map(container, {
zoomControl: true, tap: false
}).setView(this.mapDefaultPosition, this.mapDefaultZoomLevel);
L.tileLayer(this.tileServerUrl, {
minZoom: MIN_ZOOM,
attribution: this.tileServerAttribution,
unloadInvisibleTiles: true
}).addTo(this.map);
// Rest of your code...
}
Check Marker Popup Initialization:
Ensure that the marker popups are initialized correctly and that there are no JavaScript errors when trying to bind popups to markers. You might need to add similar checks in your renderMarkers() function to ensure that all properties used for popups are correctly defined.
Debugging in Firefox:
Utilize Firefox’s web console to check for any additional warnings or errors that may provide insight into what might be going wrong. Sometimes, the error messages can be more descriptive.
Example Modifications in Your Code:
Here’s an example of how you might adjust your drawMap function to ensure Leaflet is loaded:
javascript
export function drawMap() {
console.log("start drawing map");
// Check if the Leaflet library is loaded
if (typeof L === 'undefined') {
console.error("Leaflet library is not loaded.");
return; // Exit if Leaflet is not available
}
// Proceed with map initialization
const container = this.template.querySelector(MAP_CONTAINER);
this.map = L.map(container, {
zoomControl: true, tap: false
}).setView(this.mapDefaultPosition, this.mapDefaultZoomLevel);
L.tileLayer(this.tileServerUrl, {
minZoom: MIN_ZOOM,
attribution: this.tileServerAttribution,
unloadInvisibleTiles: true
}).addTo(this.map);
console.log("start loading shapefile with school districts: " + this.schooldistrictsUrl);
// Load shapefile logic ...
}
Conclusion
By ensuring that the Leaflet library is properly loaded and accessible in the right scope, and by adding error checks, you should be able to resolve the issue of the component not functioning correctly in Firefox while still working in Chrome. If problems persist, consider debugging further to isolate specific interactions that might be causing the issue.