This article describes how WP Rocket integrates with the Aelia Currency Switcher to serve the correct country- and currency-specific content from the cache. It also explains why the results returned by testing tools could be misleading, and how one can improve the performance.
1. How caching works with WP Rocket and the Currency Switcher
When WP Rocket is used together with the Currency Switcher, and the geolocation is active in the Currency Switcher settings, WP Rocket skips the cache for first time visitors. This is by design, and it's to allow the detection logic to determine the country and the currency for the visitor. If you disable the geolocation in the Currency Switcher settings, WP Rocket will serve cached content to first time visitors, but the content may or may not be the correct one for their country, as it would be "whatever was cached".
Below you can find more details about the caching logic, how the presence of the Currency Switcher changes the site's behaviour, and how to improve the page speed. Please note that the documentation covers WP Rocket. The same principles can apply to Litespeed, as well as a caching layer at server level, if there's any running.
2. Why testing tools might yield misleading results
Performance testing tools appear as first visitors every time, discarding any cookie they receive, therefore they always load the site without cache and indicate a slower performance than the actual one. In other words, the results you see with the Currency Switcher and the geolocation enabled are the same you would see with the cache completely disabled.
The caching logic works as follows:
- The first time a new visitor loads a page, and the geolocation is active, WP Rocket skips the cache entirely and serves the page dynamically This allows to run the geolocation, identify the currency, and calculate the prices for the products. Due to that, this first visit is always slower than the subsequent ones.
- The second time the visitor loads a page, the page is served from the cache, if available. The correct page content is determined by WP Rocket, using the currency stored after the first visit.
2.1 How the caching logic could affect the page speed and the "time to first byte" (TTFB)
The tools available to test the site's performance, such as Pagespeed, GT Metrix, Webpagetest or Speed Bot, make several calls to your site, appearing as a first time visitor at every call. Due to that, the tools will "see" your site as if it had no caching (thus slower performance). In addition to it, they would encounter the overhead added by the geolocation every time.
Real users, on the other hand, appear as first visitors once, then they receive cached content, and see the site loading significantly faster. In short, the results you see with testing tools don't necessarily reflect the real user experience.
2.1.1 Performance comparison
With WP Rocket, the Currency Switcher and the geolocation enabled, the time to first byte, or TTFB, which indicates the speed at which a document is served to the browser, depends on who is visiting the site, and when.
WP Rocket, no Currency Switcher, or Currency Switcher with the geolocation disabled
- First visit -> Cached content (regardless of the country or currency), lower TTFB (faster).
- Second visit -> Cached content (regardless of the country or currency) , lower TTFB (faster).
- Third visit -> Cached content (regardless of the country or currency) , lower TTFB (faster).
WP Rocket + Currency Switcher, human visitor
- First visit -> Live content (no cache), geolocation resolution, higher TTFB (slower).
- Second visit -> Cached content, lower TTFB (faster).
- Third visit -> Cached content, lower TTFB (faster).
WP Rocket + Currency Switcher, testing tool
- First visit -> Live content (no cache), higher TTFB (slower).
- Second visit -> Live content (no cache), higher TTFB (slower).
- Third visit -> Live content (no cache), higher TTFB (slower).
As anticipated, this behaviour is by design in WP Rocket. The first visit is (and only the first) needed to identify visitor's location and set the appropriate currency. Since this operation can only be performed by loading the geolocation library (unless something else, such as Varnish, or a custom script, already performed such operation), the site must be loaded dynamically.
3. Is it possible to have cached content served to all visitors, even at the first visit?
With WP Rocket and the Currency Switcher, there are two ways to serve cached content to visitors from the first visit:
3.1. Disable the geolocation in the Currency Switcher settings
If you disable the geolocation feature in the Currency Switcher, WP Rocket should serve cached content right away. This is a quick test that you can perform immediately, by disabling the option at WooCommerce > Currency Switcher > Geolocation. However, that may or may not be the correct content for visitor's location, because the geolocation doesn't run. Some of our customers adopt this solution, and rely on the customer to choose the correct currency manually, or enable the "force currency by country" option in the Currency Switcher settings, so that the currency changes when the customer choose a country at checkout. Please just note that we haven't tested this approach ourselves, it might be a good idea to give it a try.
Important: if you want your site to detect visitor's location and set the currency automatically, then the geolocation must run before any cached content can be served, in one way or another.
3.2. Implement a geolocation script that runs before the caching, so that the dynamic loading is not needed
An alternative to skipping the cache at the first visit would be to perform the geolocation at a higher level, before the caching logic runs. For example, Varnish (a cache application, which works at server level) can be programmed to detect when a visitor is coming to the site for the first time, perform the geolocation at server level, set the country and currency cookies, and serve cached content right away (see How to add dynamic caching to your site. The article includes a section about Varnish, written by one of our collaborators). This gives the best combination of performance and accuracy, but it requires some customisation on the server.
Note: some hosting providers don't allow to implement custom rules for Varnish. If you wish to take that route, we would suggest to contact your hosting provider and ask if they have that kind of restriction in place.
If your hosting provider allows it, it could also be possible to write a custom script for WP Rocket, in order to perform the geolocation before the caching logic is loaded. This would allow WP Rocket to serve cached content to first time visitors as well.
From the article about caching:
Update about WP Rocket - January 2021
Based on our tests, it should be possible to "trick" WP Rocket into serving cached content to first time visitors. To do this, one has to perform the geolocation before the caching logic is applied. This can be done with some custom code, which has to be tailored to the specific site where it's going to run, to set the cookies used by the Aelia plugins and WP Rocket to determine the content to serve (see Technical Details section, at the bottom of this article).
Please be aware that, although this approach can work, it's not officially supported by WP Rocket. Due to that, the WP Rocket Team can't help with its implementation. If needed, we are happy to offer our consultation services to assist with the scoping, implementation and testing of a custom solution for your site. Please feel free to contact us for more information and for an estimate: https://aelia.co/contact.
That customisation would change the loading flow from this:
- First time visitor, no country and currency cookies are set.
- WP Rocket skips the cache and serves the page dynamically.
- Geolocation sets the country and cache cookies.
- From the second visit onward, cached content is served.
- First time visitor, no country and currency cookies are set.
- Some custom code performs a preliminary geolocation, without loading the whole page, and sets the country and currency cookies.
- WP Rocket uses the cookies to serve the content from the cache, right from the first visit, using the cookies set at step #2.
This customisation for WP Rocket is unofficial and its implementation is entirely manual. The process involves reviewing the setup, modifying the wp-config.php file, copying the required files to a specific location, creating a link to the geolocation database, so that the custom code can use the GeoIP data that the Aelia Foundation Classes downloaded.
If a custom country/currency mapping is needed, e.g. to link EUR to Scandinavian countries, or Eastern European ones, that would also have to be hard-coded in the custom caching script for WP Rocket. This is because the script would have to be as lean as possible, and it couldn't load settings from the database, nor any WordPress plugin. All its logic and settings would have to be hard-coded.
Note: some hosting providers don't allow to modify the wp-config.php file. We recommend to check with your provider if this could be the case, as the restriction would prevent the implementation of the custom script.