Load JS in Head to avoid flash of hidden content?
-
Since the website I work on uses Cloudflare, I have the following options enabled:
-Enable AJAX endpoint to get information for the current IP even on cached pages.
-Add JS Helper functions to all pages.
-Add a country-specific CSS class to the <body>-Tag (via AJAX).The localized content I have is above the fold and large. The localized content is an offer (like a coupon), where USA visitors are given a different offer than visitors outside of the US.
My CSS looks like this:
.geoip-country-US .non-usa, .usa {
display: none;
}
.geoip-country-US .usa {
display: block;
}With the above setup, I have observed that USA visitors will see a flash of the non-USA content, before seeing the correct content. I was curious about whether loading the JS in the HTML head would either resolve this, or make it less noticeable? If you agree, what is the best way to go about loading it in the head?
OTHER INFORMATION
I have been testing all of this on a local environment. On my local environment, there is no flash of non-USA content when a) The first option to add country-specific CSS classes is enabled and b) AJAX is disabled. However, this isn’t a viable option since the production version of this site uses Cloudflare. Due to this, I am open if you think there is a better way to structure my CSS, however I doubt this can be fixed by CSS changes.I have WP Rocket and WP Meteor installed. For WP Meteor, I have excluded js/dist/frontend.js and the inline script with ID “geoip-detect-js-js-extra” from optimization. I have verified that both scripts lack the attributes that WP Meteor adds to scripts. Comparatively, the flash is shorter when those scripts excluded from optimization than when they are optimized. But it is still very noticeable.
I also tried with WP Meteor deactivated. I would say that the flash is barely noticeable with it deactivated. However, due to the terrible way this website was coded, I can’t deactivate the WP Meteor plugin without causing a gigantic Lighthouse Performance score drop. I am working on building a new theme from scratch and converting to Gutenberg, but it will be awhile before that is ready. In the meantime, I unfortunately am stuck with this WP Meteor plugin.
I doubt this could contribute to the issue, but in case you think it might – the website also uses the awful WPBakery page builder.
- The topic ‘Load JS in Head to avoid flash of hidden content?’ is closed to new replies.