bobbingwide / sb-chart-block

Chart block for Gutenberg
GNU General Public License v3.0
6 stars 0 forks source link

Eliminate render-blocking resources - chart.js #9

Closed bobbingwide closed 5 months ago

bobbingwide commented 3 years ago

When running Google's PageSpeed Insights against a WordPress website you often get told about the following opportunity to improve the website's performance.

Eliminate render-blocking resources

Resources are blocking the first paint of your page. Consider delivering critical JS/CSS inline and deferring all non-critical JS/styles. Learn more

WordPress There are a number of WordPress plugins that can help you inline critical assets or defer less important resources.

Beware that optimizations provided by these plugins may break features of your theme or plugins, so you will likely need to make code changes.

Requirement

Proposed solution

Wrap the inline JavaScript written by the server side rendering with:

$script .= 'document.addEventListener( "DOMContentLoaded", function() {';

and

$script .= '}, false );';

This part of the solution uses JavaScript that is identical to the code used by people fiddling with Gravity forms JavaScript. It's a lot easier to implement though.

The other change is to ensure the script is enqueued in the footer.

wp_register_script( "chartjs-script", 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js', null, null, true );

simplify the enqueue_script call

wp_enqueue_script( "chartjs-script" );
//'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js', null, null, true );

and to remove the hook that enqueues the script willy-nilly.

//add_action( 'wp_enqueue_scripts', 'sb_chart_block_enqueue_scripts' );

In this explanation I've just commented out the code that's not necessary any more.

bobbingwide commented 5 months ago

Closing. Delivered in v0.1.0.... a long time ago