Tuesday, 12 November 2019

Magento 2 catalog page images taking too long to load and failing.

If you've upgraded or cleared the image caches for magento 2, you may notice that every catalog page loads the images at a snail pace, and if you have any load balancers that restrict php processes (like hypernode does), it may not load more than say 9 images on the page before giving up and showing broken links.

The short version of why this is happening, is because after you clear the cached thumb/small/etc images, you are only left with base images at full size (at least as far as magento is concerned) and so every time it wants to display a smaller image, it has to reprocess it on the fly, this uses php code, and this is why we encounter problems, we're asking too much of magento and a server by using more than our share of php processes to convert every single image on the catalog page.

You can obviously minimise the issue by limiting the items per page to 9 or 12 or whatever, but this is a workaround not a solution.

As it happens, magento has thought about this and provided a function to "pre-warm" the image cache and generate all the necessary images in all sizes.

You should know that a) you need shell access and b) you need a while (on our server, 10k products took about 4 hours).

The function is as follows. You should really only be doing this if you are familiar with the magento binary.



/bin/magento catalog:images:resize


Pretty simple, when you know, and when it works.