It's easy to optimize web page size and load time with Sitefinity. In this post, learn how you can integrate a tool like Kraken.io with Sitefinity for simple image optimization.
TL;DR The code showcased below in this blog post is available here. You can start using it right away by downloading the add-on and installing it to your Progress Sitefinity project.
Websites have come a long way since their inception in 1991. As technology progressed, we’ve gradually pushed it in the direction of being more and more visually engaging. The modern web page must provide information, but in a way that is interesting for the user and will retain their attention. Thus, caught in a fierce competition, websites have increasingly come to rely on images and video as content. This of course has resulted in bigger and bigger pages over the years. In fact, based on information provided by httparchive.org, the average web page size was 702kb in 2010 compared to 3545kb in 2018.
Ouch… that’s some heavy visual engagement right there. We’re all painfully aware of why we need to optimize web page size and load time, but we’re required to deliver exceptional content at the same time. So, a struggle might ensue between content creators who add more media to pages and web developers who try to optimize them - at least, that’s what happened with our company websites here at Progress. On one occasion, we had a blog post with over 10 images, each of which was several MBs in size. When using a CMS, content creators rarely pay attention to image sizes, or page load optimization in general… nor should they, their job is to create exceptional content.
So how do we improve the CMS to make the lives of both content editors and developers easier? Let’s build an image optimization module!
There are services available, such as Kraken.io, which compress the image size while retaining the same image quality. The idea is to use such services and reduce the size of images before they are saved in Sitefinity. Luckily, Sitefinity provides a mechanism to plug into the file processing flow. The goal is to plug our optimization logic into the file processing flow, so that users don't have to do anything more than uploading their image.
To be fair, there is an existing open-source project that provides Kraken.io integration with Sitefinity, but it was built 4 years ago and due to the admin re-invention, it won’t be compatible with newer versions. This blog post focuses on a simpler, easier to setup approach.
Create a new Sitefinity solution, containing one web app project. Add a separate project that will store our logic. The idea is to then create a NuGet package for easy installation and reusability. The full code is available here, so you can go and get it, and I’ll also be providing Gist links with the code for every step.
First, create a base class called ImageOptimizationProcessorBase that extends ProcessorBase and implements IInstallableFileProcessor. The point of the base class is to provide some common functionality that can be reused by image optimization file processors.
The goal of the IInstallableFileProcessor is to create a contract for us, so that we can automatically install all image optimization file processors on project startup.
Next, we’re going to build our Kraken.io image optimization processor. To do that, we’ll need to install the “kraken-net” NuGet package. After that we’re going to create a new class called KrakenImageOptimizationProcessor that extends ImageOptimizationProcessorBase.
Let’s add a startup class that will handle the registration of all image optimization processors. We currently have only one processor for Kraken.io, but the idea is that there can be different file processors for different image optimization services. We want to provide logic for automatic registration for all of them and allow the user to pick which one to configure. We’re basically automating the file processor registration described in Sitefinity’s documentation.
Let’s start our website and configure our Kraken image optimization processor. We can see that in Advanced settings, under Libraries we have a new configuration added for “Kraken IO Image Optimization.” We need to set the values for the ApiKey and ApiSecret parameters. You can get them from your Kraken account under the API Credentials section.
If you followed the above steps, it means that you are all set! Now, it’s time to upload an image and see how the optimization works.
Oooh Weee! That’s some fine image optimization right there! Furthermore, it's all done seamlessly through the file processing flow and content editors don't need to think or know about it.
If we have an existing website and want to optimize already uploaded images, we’ll need to create a more sophisticated approach using a background task and a way to track already optimized images. Let us know in the comments what your use case is and whether you can see Kraken.io applicable as a solution.
If you’re new to Sitefinity and want to learn more, feel free to give it a try for free today or schedule a demo.
Yasen Yankov currently leads one of the Sitefinity engineering teams. Prior to that, he spent 5 years developing and maintaining Sitefinity applications as part of the Progress web team. He has worked on complex web projects like telerik.com, progress.com, sitefinity.com and nativescript.org
Copyright © 2018 Progress Software Corporation and/or its subsidiaries or affiliates.
All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks for appropriate markings.