westonruter / syntax-highlighting-code-block

Extending the WordPress Code block with syntax highlighting rendered on the server, thus having faster frontend performance and being AMP-compatible.
https://wordpress.org/plugins/syntax-highlighting-code-block/
GNU General Public License v2.0
117 stars 11 forks source link

Opt to register styles in standard way #785

Closed westonruter closed 10 months ago

westonruter commented 10 months ago

Fixes #286.

By registering styles for the block in the traditional way, we can be guaranteed that the style will be printed to the page. There are scenarios (see #286) in which a block can be rendered and yet not printed, and this can prevent the inlined styles from being served. Styles were originally inlined in this way in order to prevent serving them when they weren't needed. Nevertheless, block themes now by default only print styles for blocks which are actually rendered on the page. And yet, classic themes do still print all styles for all registered blocks in the head unconditionally. In order to retain that conditional style printing, you can add the following plugin code:

add_filter( 'should_load_separate_core_block_assets', '__return_true' );

For classic themes, this has the effect of printing block styles in the footer since only at this point does WordPress know which blocks have been rendered. The downside here is that there may be a flash of unstyled content and critical styles may not be loaded soon enough.