Next.js Discord

Discord Forum

Run Image Optimization Programmatically

Unanswered
Maine Coon posted this in #help-forum
Open in Discord
Maine CoonOP
Hey! So I have a bunch of user uploaded images I want to optimize at the point of upload, not "on demand" when they are displayed using the next/image component in my app. Because it's a list of images and otherwise when the user opens the page my server will have to optimize ~100 images at once. What are my options here?

8 Replies

Maine CoonOP
(I'm self-hosting btw. and have sharp installed.)
Resize the image at time of upload
Or whatever compression you do
There’s libs to do this
So before the image is uploaded to the server but after the user has selected the images using the file chooser
Maine CoonOP
That would be a nice additional thing to do, but I do want to do the exact same thing next itself is doing on requesting the image, just ... earlier 😄
You can try importing the relevant optimiser functions from next/dist/server/image-optimizer

https://github.com/vercel/next.js/blob/07c652a1200445ebfed8ef13426e36a150a04a67/packages/next/src/server/image-optimizer.ts