Some performance optimization plugins and services modify how JavaScript files are loaded by applying defer or delay strategies. While this usually improves performance, it can delay the execution of critical scripts that are required for the page to render.
As a result, the page appears slowly until those scripts finally run.
What causes this?
This issue typically occurs when a third-party optimization tool delays JavaScript execution. Common examples include:
- LiteSpeed Cache
- WP Rocket
- FlyingPress
- Perfmatters
- Autoptimize
- NitroPack
- Cloudflare Rocket Loader
These tools often delay or defer JavaScript to improve performance metrics, but in some cases they may also affect scripts that should run immediately.
How Mosaic handles this
Mosaic identifies critical JavaScript files that are required for proper and immediate rendering of the page.
To ensure correct behavior, Mosaic includes compatibility logic for several popular optimization plugins. It automatically excludes its critical scripts from deferred or delayed loading wherever possible.
This is typically done using plugin-specific attributes or filters, such as:
data-cfasync="false"(Cloudflare)data-no-optimize="1"(Autoptimize, Cloudflare)data-no-defer="1"(LiteSpeed, Perfmatters)data-no-delay="1"(Flying Scripts, Perfmatters)data-no-async="1"(Async JavaScript plugin)
Additionally, some plugins provide their own exclusion mechanisms (e.g. WP Rocket’s rocket_exclude_js filter), which Mosaic supports where applicable.
Cloudflare Rocket Loader
It is not possible to detect Cloudflare Rocket Loader automatically, so it requires manual configuration.
To ensure compatibility, you need to enable the following constant in your wp-config.php file:
define('MOSAIC_CLOUDFLARE_ROCKET_LOADER', true);
This allows Mosaic to properly exclude its critical scripts from being delayed by Rocket Loader.
Recommendation
If you experience slow page rendering or delayed visibility:
- Check if any optimization plugin is delaying JavaScript execution.
- Make sure Mosaic scripts are excluded from defer/delay features.
- If using Cloudflare Rocket Loader, enable the constant above.
Proper configuration ensures that performance optimizations do not interfere with essential functionality.