This article is dedicated to Sitecore SXA Asset Optimizer. Precisely, I want to share with you how to resolve the issue with an empty timestamp parameter, which is responsible for assets versioning.
The Sitecore Experience Accelerator has a built-in Asset Optimizer module that is used for optimizing CSS styles and JS scripts. If you want to improve your site performance, you should enable this module in your production environment, and it will cut down the volume of data that needs to be transmitted.
As a Sitecore administrator, you have two options of enabling Asset Optimizer:
- Global enablement for the entire Sitecore instance
- Local enablement for selected sites
According to the best practice, both CSS styles and JS scripts are loaded on the client side with a version prefix to avoid a cache issue when a new delivery occurs. And SXA is not an exception: it applies a timestamp for the optimized assets, as shown below:
In one project I was involved in, we observed the following issue — the timestamp was not generated for localized sites. All optimized assets contained the empty timestamp parameter, as shown below:
This results in the situation when the client browser doesn’t update the cached version of CSS styles and JS scripts after another deployment, since the t parameter is a versioning element.
So, let’s investigate this case.
The first thing we need to do is to figure out how and where the timestamp is generated. A great idea is to start from the default SXA layout razor page. It’s called SxaLayout.cshtml.
As we can see, to get an asset link, it executes the static GenerateLinks method of the AssetLinksGenerator type
(full name: Sitecore.XA.Foundation.Theming.Bundler.AssetLinksGenerator)
Going deeper, we can find the GetOptimizedItemLink method, which is responsible for the asset link generation. It looks as shown below:
In the return expression, if an optimized script item is both not null and empty, the BuildAssetPath() extension method is executed.
All right, we’ve discovered the place where the timestamp is added. Interesting, it goes from the system’s __Updated template field. Hm, what could be the issue with that? Let’s check this field.
Ok, the __Updated field belongs to the Statistics template that is a part of the Standard Sitecore template. The key thing here is that this field is not shared. It means that if we have a site with no default localization, the optimized assets also should have this language version that is a bit strange — do we really need to have localized assets (CSS and JS)? I am not sure. So, we need to apply a fix to resolve this issue for localized sites.
- First, we need to create our own BuildAssetPath extension method (the same code as above) with one additional line of code inside the if statement:
Item item1 = item.EnsureFallbackVersionIfExist();
This extension method (assembly Sitecore.XA.Foundation.SitecoreExtensions.dll, ItemExtensions.cs) returns an item for a default language (usually English) if there are no language versions. That is what we need for optimized scripts.
- Second, we need to create a new type (inherited from AssetLinksGenerator.cs) with the overridden GetOptimizedItemLink method and a new static GenerateLinks method to hide the base one.
- Third, replace OOTB AssetLinkGenerator by the created one in the SXA layout:
That is it! Now, your localized Sitecore SXA sites have optimized assets with the generated timestamp parameter! Happy coding Sitecorians!