You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Charts sometimes stay blank on the front end: Chart.js loads async and races its renderer #1419
On the front end, charts on a page sometimes stay blank. It happens on some page loads and not others. When it does, the console shows:
ReferenceError: Chart is not defined
at renderSpecificChart (js/render-chartjs.js:121)
at renderChart (js/render-chartjs.js:13)
at HTMLBodyElement.<anonymous> (js/render-chartjs.js:528)
...
at showChart (js/render-facade.js:127)
Charts from other libraries on the same page can stay blank too. In one load, a Google pie in the content, a Google bar in a Text widget and the picked Google line chart were all empty, while the Chart.js widget in the footer was the one that threw.
Seen on Visualizer 4.0.11 (and the #1417 build, whose front-end files are byte-identical) on WordPress 7.1.5 with Twenty Twenty-One. It happened on 2 of 4 page loads during QA for #1413.
Cause
Visualizer_Module_Frontend::script_loader_tag() (classes/Visualizer/Module/Frontend.php:95) adds async to the library scripts:
An async script runs whenever it finishes downloading, so js/lib/chartjs.min.js can run afterrender-facade.js triggers visualizer:render:chart:start. The handler in render-chartjs.js then calls Chart.getChart() before Chart exists and throws.
Every library's renderer listens to the same jQuery event. An exception in one handler stops jQuery from calling the handlers registered after it, so one Chart.js failure can also blank the Google and DataTable charts on the page.
Steps to reproduce
Create a Chart.js chart and a Google Charts chart.
Put the Chart.js chart in a footer block widget (so it is on every page), and the Google chart in a post.
Open the post several times with the cache disabled.
Expected: every chart draws on every load. Actual: on some loads the console shows ReferenceError: Chart is not defined, and the Google chart stays blank (its .visualizer-front container is 0px tall).
Suggested fix
Make the renderers wait for their library instead of assuming it already ran. For example, in render-chartjs.js, wait until typeof window.Chart !== 'undefined' before rendering. Or stop marking chartjs as async, so it runs before the dependent render scripts. Also make each renderer's handler catch its own errors, so one library's failure can't stop the others.
Activity