I am developing this site, and I am experiencing slow page loads. Some times loading speed is normal, other times too slow.
I was using barba.js for transitions, and this slowness broke itâs effect. I now disabled it to rule out this dependency being the problem.
Could the problem be in my php code and Kirby rendering? Server response time? I see in the browserâs network tab that when a slowdown is happening, the requested page has a âpendingâ status for some seconds. What is happening during that time?
Any ideas about how to test, what to check etc would be appreciated.
You have a lot of high resulution photos being displayed much smaller. Make use of the resize function. I can see many of the images are in excess of 2,000px but you are only displaying them at around 600px on the page.
Check your PHP memory settings also - with all this heavy image work, setting it to atleast 2048mb. When i looked now, the home page was very slow but was fine earlier. Do you have something that might touching the files? The media generation uses file hashing to tell if an image has changed. It is as if the media folder keeps getting deleted or the files them selves are changing enough to cause regeneration.
I am using responsive images and lazy loading. The media files donât seem to be regenerating, as I can infer from the date/time the files are created.
For the home page, this loop seems to be responsible for the slowness:
It is my understanding (but I could be wrong) that calling ->width() and ->height() is quite resource intensive because the server doesnât have those info stored somewhere and it has to load the resources first in order to calculate those values and return you the correct information.
Maybe that is why your server is taking a long time to return the content of your page.
Both width and height are properties of the image object though just like url, alt etc., so these calls shouldnât have any additional effect on performance.
I followed @manuelmorealeâs advice (removed width/height and orientation) and curiously, the problem is gone. I would still need these dimensions and orientation though. I also noticed that as @jimbobrjames said, some images are huge, I missed that they were 300dpi. But even with those, I was expecting the page to load normally, and only the images to download late.
Can this be done automatically on file upload or replacement?
This is exactly what I ended up doing in all my sites.
I have a hook that stores width, height and a few other things.
looks like this
'file.create:after' => function ($file) {
# Exit if we're not working with a web image
if (!in_array($file->extension() , ['gif' , 'jpg' , 'jpeg' , 'png']))
return true;
// Add the relevant meta info
return $file->update([
'w' => $file->width(),
'h' => $file->height(),
]);
},
'file.replace:after' => function ($newFile, $oldFile) {
kirby()->trigger('file.create:after', ['file' => $newFile]);
}
As I said, I could be totally wrong but the last time I looked into this I remember finding out that since those info are not stored anywhere, at some point the server has to load those images into memory in order to calculate those dimensions.
I donât think you can avoid it because php has no idea how big an image is until it has loaded it and âmeasureâ it.
Again, I could be completely wrong but I remember that being the case at least for me the last time I looked into this and itâs why I ended up storing those values into the file txt
Usually itâs not an issue but it becomes one if youâre dealing with a lot of big images.