-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use console warning rather than DOM element to indicate missing stylesheet #6261
Conversation
src/ui/map.js
Outdated
@@ -1388,12 +1387,20 @@ class Map extends Camera { | |||
return [width, height]; | |||
} | |||
|
|||
_hasCSS(): boolean { | |||
for (const sheet of (window.document.styleSheets: any)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How fast is this if a page has a bajillion CSS rules?
If iterating over them all in JS is expensive, another approach would be:
- Add
.mapboxgl-canary { background-color: salmon; }
to mapbox-gl.css. (Unfortunatelycanary
is not a CSS color keyword so I chose another animal.) - Map adds
<div class="mapboxgl-canary" style="visibility: hidden;"/>
and then checks the computed background color.
Oof, looks like there was a bug (possibly introduced in Chrome 64) causing cross-origin stylesheets not only to be missing a |
Switched to checking the computed color of a hidden div as suggested in #6261 (comment) (thank you @jfirebaugh!), both for performance and because of #6261 (comment). |
Agreed. I can think of a couple possible approaches:
|
src/ui/map.js
Outdated
_detectMissingCSS(): void { | ||
const computedColor = window.getComputedStyle(this._missingCSSCanary).getPropertyValue('background-color'); | ||
if (computedColor !== 'rgb(250, 128, 114)') { | ||
warnOnce('Missing Mapbox GL JS CSS: map may not display correctly.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This page appears to be missing CSS declarations for Mapbox GL JS, which may cause the map to display incorrectly. Please ensure your page includes mapbox-gl.css, as described in https://www.mapbox.com/mapbox-gl-js/api/.
This uses CSSOM interfaces to check for the presence of a
.mapboxgl-map
CSS rule upon map creation and display a warning in the console if it's missing, rather than adding and hiding a DOM element (ref #5785, #5359 (comment)).Fixes #5786.
Having to stub
console.warn
so many places in the unit tests feels weird, and treacherous for future writers of tests that includeMap
— if there's a better way to do this I'm all ears!Launch Checklist