DEV Community

Cover image for 3 easy steps to fix slow image loading

3 easy steps to fix slow image loading

Felix Häberle on October 27, 2019

Loading images on the web can be a pain, especially if you are doing a website mainly for mobile devices. Because wireless connection is used in su...
Collapse
 
matthewbdaly profile image
Matthew Daly

I would also add that caching images for as long as possible is prudent, particularly if you're using a CDN. If an image may change in future, you can use various cache-busting techniques to replace the image even if it is cached for a long time.

Collapse
 
catalin560 profile image
catalin560

would you mind explaining a few of those cache-busting techniques?

Collapse
 
matthewbdaly profile image
Matthew Daly

Basically, the idea is that you append a query string to the URL for an asset, usually based on the timestamp when it was last changed. That way, if you replace an image or other asset, the URL used in the application will change, and so you load the new version, no matter how long the old version was cached for.

Many frameworks and CMS's provide this functionality out of the box, such as the version() method in Laravel Mix. If yours doesn't it's not hard to roll your own. Here's a simple example of a Twig filter that does this:

<?php declare(strict_types=1);

namespace App\Views\Filters;

final class Version
{
    public function __invoke(string $path): string
    {
        return DIRECTORY_SEPARATOR . $path . "?v=" . filemtime(PUBLIC_DIR . DIRECTORY_SEPARATOR . $path);
    }
}
Collapse
 
felixhaeberle profile image
Felix Häberle

You're right! That could also be added here, thanks for pointing it out. :)

Collapse
 
anwar_nairi profile image
Anwar

Great pieces of advices Felix :) I use them all, verlok/lazyload is astonishing! I also use gulp-reponsive to make variants of my images for each of my supported devices width (smartphone, tablet, desktop, large screen). I recommand it ;)

Collapse
 
felixhaeberle profile image
Felix Häberle

Nice, that makes me happy! Really looking forward to native lazy load 🙏🔥

Collapse
 
nickytonline profile image
Nick Taylor
Collapse
 
felixhaeberle profile image
Felix Häberle

Thanks for pointing this out! That adds huge value in here, I think about update my post based on this information! Thanks! :)

Collapse
 
bassemibrahim profile image
Bassem

I never knew the image tag has those attributes: srcset & sizes. Thanks Felix!

Collapse
 
felixhaeberle profile image
Felix Häberle

No problem, thanks for commenting! 🔥

Collapse
 
pavelloz profile image
Paweł Kowalski
  1. Use small images for small purposes. Meaning: Dont use 1600x1200 1.1MB image when you are using it in a 160x120 img tag.
Collapse
 
felixhaeberle profile image
Felix Häberle

Sure, that's what I wanted to say basically during the whole blog post! Thanks for adding it here! :)

Collapse
 
sushantrahate profile image
Sushant Rahate

For native software also have a look at Caesium Image Compressor. Its awesome and free.

Collapse
 
felixhaeberle profile image
Felix Häberle

Yeah, thanks for sharing that, will definitively try it out!! 🙏