Display mit leuchtenden Pfeilen, die nach rechts weisen

Performance Boosts for Umbraco

Do you know the Umbraco Advent Calendar 24 Days in Umbraco? Every day in December, it features an Umbraco-realted article written by a different member of the community.

Two of our byte5 developers also had the honour of being featured this year. In his article, Lukas gives tips on how you can increase the performance of your website using simple methods.

Wednesday, December 20, 2017

Performance is the essence of your website. A slow website has several disadvantages. To improve the performance of your Umbraco site, there are many things you can do. And they won’t even cost you a lot of time. If you want to get a good search engine ranking and not scare away your users, read on for tips and methods.

In this article, I don’t want to write about how to push your site’s performance to the maximum, I will rather tell you about methods you can use on the fly without investing a lot of extra time on your site. If you apply these methods successfully, you will spend close to no extra time on optimising your performance.

Caching

“Caching is for sites with performance problems or for people who have too much time.” – Chances are, some of you have heard a phrase like this on your way to become developer. I’m not entirely sure anymore where I heard it but that’s not why I’m telling you this anyway. I’m telling you because of its message. And the message is – as you probably know – complete nonsense. Caching is essential to boost your site and to get the most out of it. “But what if the site runs quickly already?” – My answer to this question: You can always make it quicker. Plus, caching barely takes any time.

if (showCopyright) {

© Douglas Robar

}

CachedPartials

What can be written almost as quickly as @Html.Partial? You’re right, it’s @Html.CachedPartial! If you have already spent some development time thinking about where it makes sense to use CachedPartials instead of regular Partials, this procedure does not take a lot of time. You can also set further parameters, such as cachedByPage which allows you to get a separate cache per each type of site you have running on Partial, or cachedByMember with which you can cache for members.

IHtmlString CachedPartial(
    string partialViewName,
    object model,
    int cachedSeconds,
    bool cacheByPage = false,
    bool cacheByMember = false,
    ViewDataDictionary viewData = null,
    Func<object, ViewDataDictionary, string> contextualKeyBuilder = null)

While developing, CachedPartials won’t get in your way as they only cache with the debug mode set on false. Another thing to keep in mind is that the Partials cache is emptied by Umbraco publish events.

WebApi.OutputCache

How often do we make queries where we know beforehand that they will always provide the same data? Like when we use an WebApi to get settings from the backend. Why should you wait longer than necessary for an answer – especially when WebApi.OutputCache is working in one line? WebApi.OutputCache.V2 is a Nuget package. After the installation, you can apply an attribute to your methods and thereby cache them. See the following example:

publicclassMyController : UmbracoApiController
{
    [WebApi.OutputCache.V2.CacheOutput(ClientTimeSpan = 60, ServerTimeSpan = 60)]
    public string cachedFunction(string parameter)
    {
        // do something


        return something;
    }
}

There are a handful of variations of the output cache: CacheOutputUntil, CacheOutputUntilToday, CacheOutputUntilThisMonth and CacheOutputUntilThisYear. These serve the purpose of setting a specific time at which the site is supposed to be cached.

LeBlender

LeBlender is a free Umbraco package. It lets you build complex grid editors effortlessly. I, for one, could not imagine working without it anymore.

Just build your new grid editor in the developer section as you always do. In the “select a grid editor” section, choose LeBlender Editor.

if (showCopyright) {

© Antoint Grimaud

}

Now you can simply add properties. You can also use your own datatypes. LeBlender has a built-in cache. You only need to type in the cache period et voilà. You have already done a lot for your site, if you appropriately use the cache period in all editors.

Those of you who don’t know LeBlender yet, should definitely read this: https://our.umbraco.org/projects/backoffice-extensions/leblender/

if (showCopyright) {

© Antoine Giraud

}

Further Advantages of Caching

The most important thing, no matter which kind of caching you’re planning on doing, is to do some thinking about how often data changes in your project and how important it is to have the latest data available at all times – either while you’re developing or even better: before you start. Does it make a difference whether the new data is updated right away or in two hours? If the answer to that question is “no”, you should definitely cache. Consistent caching also gives you additional advantages, some of them with tremendous effects.

Some of you probably work with systems other than Umbraco which your client pays for per call. If you cache the result of a call, instead of having thousands of calls you might only have one. Your client will be pleased.

Another advantage is the working method caching offers. By thinking about data flow from an early point onwards, you avoid errors causing more problems the later they are found. Your team leaders will be eternally grateful.

“Quicker load time or not, if the users of the site don’t need to wait for too long, it’s all the same, isn’t it? After all, they won’t notice one hundred milliseconds more or less.” – The user might not, but the search engine certainly will! Caching has a positive influence on the speed of indexing a site which can help you get a better ranking.

Typed Content

Additionally, you should always pay attention to using typed content. As a general rule, typed content is more performant than Dynamics. Therefore, it is less prone to errors. If you still work with Dynamics, now would be a good time to change since support for Dynamics via IPublishedContent will cease with the introduction of Umbraco version 8.

Common Pitfalls & Anti-Patterns

If you’re new to Umbraco, there are a handful of rules which you should pay attention to in order to avoid errors. I want to send you off with a related our.umbraco article I highly recommend: https://our.umbraco.org/documentation/Reference/Common-Pitfalls/

This article was first featured on 20 December 2017 on 24 Days in Umbraco.

More questions about performance!

Our experts know how to make a website high-performing and future-ready! Just drop a line.

Contact the performance experts