Use Sharp.js with Kirby

Hi everyone!

This is perhaps a general question: I am encountering performance issues in a website I am developing, and it seems that the main problems come from image compressions. Looking at the recommendations for automatic image compressions here, I followed the instruction, installed the node package and copied the following script:

const sharp = require('sharp');
const fs = require('fs');
const directory = './images';

fs.readdirSync(directory).forEach(file => {
  sharp(`${directory}/${file}`)
    .resize(200, 100) // width, height
    .toFile(`${directory}/${file}-small.jpg`);
  });

It doesn’t seem to work and I imagine that’s because I don’t have a ./images folder in my website. How can I make this work?

I apologise if the question is too broad. Feel free to let me know if I can help giving more informations.

Thank you!!

Don’t know why you are using a JavaScript library, see Responsive images | Kirby CMS

:flushed: (silence followed by embarrassment)

I’ll try using it straight away.

Thanks! :pray:

1 Like

Works perfectly, and performances went straight up to 91%. Thank you so much