Skip to content

Charts sometimes stay blank on the front end: Chart.js loads async and races its renderer #1419

Backlog

Description

@vytisbulkevicius

Description

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:

$async_scripts = array( 'google-jsapi', 'chartjs', 'visualizer-datatables' );

An async script runs whenever it finishes downloading, so js/lib/chartjs.min.js can run after render-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

  1. Create a Chart.js chart and a Google Charts chart.
  2. Put the Chart.js chart in a footer block widget (so it is on every page), and the Google chart in a post.
  3. 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

  1. added theissue type on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions