Performance

How to Optimize a WordPress/WooCommerce Shop to Load in Under 3 Seconds

Aug 22, 20207 min readRami Mustaklem

Originally published on Medium in August 2020.

Usually optimizing a WooCommerce shop takes a lot of time and effort, and soon after that, by just installing a new plugin or changing the tiniest things, your work is gone in vain.

In this article I’ll go over my tech stack and steps I did, for around a few weeks, to have successfully optimized my WooCommerce shop to load in less than 3 seconds.

TL;DR

  • Choose production grade Cloud service
  • Choose a high CPU high Memory VM, or split your architecture into separate instances (e.g. Server VM, DB VM, Cache VM)
  • Use OpenLiteSpeed for your server
  • Use a fast CDN
  • Compress and Convert Images (forget PNG if you have no transparent background)
  • Configure server caching
  • Minify and Combine CSS and JS
  • Get rid of unnecessary and unused CSS and JS with “Asset CleanUp” plugin
  • Get rid of unnecessary and unused plugins
  • Use Redis (on same machine or separate)
  • Keep looking for parts in your website to optimize (Fonts, Sliders, Images, General Content, etc…)

And you will achieve less than 3 second loading times, with ~1 MB total page size and ~30 Requests.

Project

Believe me when I say, working with WordPress is like working with old computer hardware. You fix a part and another breaks. That’s what happens when you try to optimize your WordPress site, you fix something, another breaks, and there’s frustration everywhere you look.

At first, well, I wanted to start my online shop, and I started searching around about where and what is the best way to start one, according to a budget.

So, to start quick and have something in no time, I bought WooCommerce theme from themeforest.net, which wasn’t perfect, but I do not regret buying it as they do consistent and quick updates to it.

Server

I started my hunt with what to use as a server. I thought between Nginx and Apache, and well I found out there’s something called LiteSpeed which is built for WordPress and they have a plugin called LiteSpeed Cache which is compatible with the server and performs really well. Working with this plugin, I saved myself a lot of time and a lot of optimization plugin installation.

Hosting — DigitalOcean

I have decided that this is the server I wanted to work with, so I started searching on where can I host my app, and which hosting is cheap but also good. So I ended up hosting my site on a DigitalOcean droplet, where I was looking around in their marketplace for a WordPress Image to kick start my journey with a ready Ubuntu image, and Voila, I found an WordPress Image that is based on OpenLiteSpeed, which is the free, open source version of LiteSpeed Server.

It was a breeze to work with this image on DigitalOcean. It was like, fire up the droplet, ssh into it, go through the automated steps that start with logging into the droplet to choose the domain name and set a let’s encrypt certificate, and you now have a fully setup WordPress installation with SSL. In, let’s say, half an hour.

Hosting — AWS

To cut the story short, before I started optimizing my app, one day I came in the morning and I found out that my droplet was turned off for the whole night, due to some issues in the region where DigitalOcean had my droplet, so right away, I turned it on and started looking for another hosting. That’s when I decided to take advantage of the AWS Free Tier for 12 months of really good services.

So I backed up my site, with All-in-One WP Migration, and started moving to AWS. Splitting my app to Server Instance and DB Instance to save server resources and use the free tier.

First I started an EC2 t2.micro instance, a t2.micro RDS database instance, connected an elastic IP to my EC2, which is covered by the free tier, then I routed the IP to my Cloudflare DNS, and started to look around the AWS marketplace for a similar image with OpenLiteSpeed, and Voila, I found it. Fired up my instance, did the same steps, set it all up, and restored my site.

CDN

Now, I decided to stick with AWS, and started my optimization process. First thing I searched for is a good and cheap CDN, and I found BunnyCDN, which gives a 14-day trial to try it out, and it’s really easy to work with. Unlike other CDNs, BunnyCDN requires only a minimum of $1 a month, and a minimum account credit charge of $10.

After I used BunnyCDN and compared its performance to other CDNs, I decided to stick with CloudFront and take advantage of my free tier.

It was a bit more complicated to work with CloudFront, but hey, it’s free for 12 months, also wasn’t that much more time and effort, it was also easy to work with.

On the Backend side of the app, with the OpenLiteSpeed Cache plugin on WordPress, I didn’t need to install any other plugins to use the CDN, it was very straight forward, put your cdn link, insert your base site url, and you have a working CDN.

Images

Before uploading my images, I converted all the images without a transparent background, to JPEG and then compressed them using online tools like compressjpeg.com.

CSS, JS and Images

By default, the LiteSpeed Cache plugin is setup with the most recommended default settings. What I had to do was, combine and minimize CSS, minimize HTML, minimize JavaScript and optimize images and lazy load them with different sizes and in Webp when possible.

So in this part, I really haven’t spent that much of an effort. It’s just reading and toggling settings.

Very Important Tip: DO NOT USE “LiteSpeed Cache” plugin to Combine or Minify JS, it always breaks something on the Frontend in my experience here. Instead use “Asset CleanUp” plugin.

Assets Cleanup

While working around, I noticed that there are a few assets, like css and js, that aren’t really essential to the Frontend of the site, so I started looking around on how to remove certain unused assets from the Frontend and I found a plugin called “Asset CleanUp”.

With this plugin you can also “preload” font links, combine and minimize css and js, and also remove certain scripts and styles from which ever page you choose. You can also remove site-wide features’ assets if you don’t use them like Gutenberg CSS or Emojis, or even jQuery migrate if your theme doesn’t need it.

Good news is: “Starting from WordPress 5.5, jQuery Migrate is no longer loaded”.

Caching — Redis

By default, LiteSpeed cache, uses memcached as its object caching engine and can also use Redis, and after a bit of a search on Google, I found that Redis’ performance is better, so I turned redis on and yes it’s already installed on the server by default and there’s no need to install it.

Knowing that AWS Elasticache is also covered in the free tier, I decided to take advantage and save server resources and separate the caching away from the same server, although I have noticed that using the local redis on the same server have been 3 to 4 seconds faster than using remote redis with Elasticache, but it should help reduce server processing during high traffic times.

Plugins

I have removed a lot of plugins that I didn’t use and that came with the theme. I also removed plugins that I used but saw not necessary like the “Slider Revolution” plugin, I mean who still uses sliders in their website to show pictures, it doesn’t improve UI/UX, not to mention performance issues. Although theme built in “Owl Carousel” is nice to show products on your page, but especially when you buy a theme, you cannot control how many slider libraries you have in that theme, and that also depends on the builder that comes with it.

Issues

Besides the Litespeed Cache plugin JS minification and combination being broken, I think if you’re going with a template try to research for the best WooCommerce templates in the market and there are some optimized ones.

Maybe this might help.

One of a few issues is that the theme I bought, unfortunately, although looks good on mobile, but I think it does not support mobile very well. UI/UX reports using Google’s PageSpeed Insights show a few problems, although loads fast on mobile, but “Time to Interactive” according to them is ~8 seconds, while manually testing that, noticed not more than ~4 seconds.

Conclusion

Well, this brings us to my future plans. I think WordPress/ WooCommerce is a great way to kick start your e-commerce endeavors on a very Frugal budget, if you have one, and you could stick with it long term if you keep maintaining it, but for me for long term goals, I would rather keep the Backend as it is, and rebuild the Frontend exactly as the shop requires, without bloating it with styles and scripts that you do not even use. Although I got as low as 1.8 seconds fully loading time after optimization.

I would also put the database and Redis back together with the same server instance as I saw ~4 seconds more loading time after moving Redis away into its own node, and make the server instance larger with at least 2 cores of CPU and 2 GB of Memory.

Investing more in this, I would rebuild the Frontend with NextJS, keep the WordPress/ WooCommerce and use its RESTful APIs.

I really appreciate it if you’ve reached this far reading. I hope my experience could help you optimize and speed up your site. If you feel like you need any help I’d be happy to do it.

R
Rami Mustaklem
Senior Full Stack Developer