Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lighter webp is a lot slower than png #1647

Open
LasterAlex opened this issue Dec 25, 2024 · 3 comments
Open

Lighter webp is a lot slower than png #1647

LasterAlex opened this issue Dec 25, 2024 · 3 comments

Comments

@LasterAlex
Copy link

I've tried to use webp format to make loading big images faster, but it seems that webp is a lot slower than the same file in png when accessed from the _ipx path (or if accessed with NuxtImg):

http://localhost:3000/_ipx/_/fractal.png produces a ~60ms wait:
Image

http://localhost:3000/_ipx/_/fractal.webp produces a ~400ms wait:
Image

format="webp" also doesn't do better... http://localhost:3000/_ipx/f_webp/fractal.png produces a ~400ms wait:
Image

It is even cached, and most of the time it's just connecting.

If i request a raw file from http://localhost:3000/fractal.webp it gives the cached file in no time:
Image

Dev or production mode, nightly or stable, firefox or chrome doesn't matter, it's still a lot slower.

Repo to reproduce (it's really just a few files in the public directory): https://github.com/LasterAlex/nuxt_bad_webp

Am i doing something stupid? Is this expected? A lot of sources encourage the use of webp for optimization, so it's kind of confusing to see that the nuxt-image path is both slower than the raw public file AND slower than the less compressed file format.

Thank you for your time in advance.

@alexbidenko
Copy link

@LasterAlex Locally, there is almost no delay in data transfer, but image optimization introduces its own latency. That's why you're seeing these results.

It’s more important to check the outcome under simulated network throttling (using the network tab in DevTools) or to test it on a real server.

However, keep in mind that on the server side there will also be time costs for processing and optimizing images, although they will most likely be less than transferring a large original image. If you want to achieve ideal performance on the server side as well, it can be a good idea to add browser caching headers (such as Cache-Control) and to configure web-server caching, for example using proxy_cache if you’re running Nginx.

@LasterAlex
Copy link
Author

Thanks for the response!

It’s more important to check the outcome under simulated network throttling (using the network tab in DevTools) or to test it on a real server.

I already did, that's why i saw it in the first place.
I spun up a test server to show to others, and for some reason every time i opened a tab in incognito mode with no cache, it would take an awful amount of time to load.
I just tested my example repo with a real server, and it's even worse. /_ipx/_/fractal.webp takes around 1 second now, with browser cache, while /fractal.webp takes just 50ms.
And the initial images are already from the Network tab in firefox DevTools.
I understand that image optimization introduces some latency, but if the latency is actually longer than the time to get the image with a regular endpoint, there is something wrong, and i don't yet know what.

If you want to achieve ideal performance on the server side as well, it can be a good idea to add browser caching headers (such as Cache-Control) and to configure web-server caching, for example using proxy_cache if you’re running Nginx.

So... The method to fix slow _ipx routes is to... Not serve them, instead using nginx cache?
This sounds wrong, and makes me question even more, why NuxtImg doesn't do that itself, instead just idling for a second?

I run my server by just pnpm run build && pnpm run preview --host, this looks pretty simple, but is there something wrong?

All the settings in the repo are default, created by npx nuxi@latest init my-app, and i can't see what could be wrong with it. Did i forget to set something?

@alexbidenko
Copy link

@LasterAlex I’d like to clarify that I’m not a nuxt/image developer; I’m just sharing the solutions I used for the same issue, until the official developers respond😁

From my perspective, using caching to reduce server load is a perfectly valid approach. If that’s not convenient for you, or if you find the image optimization speed unsatisfactory, there are build-time solutions like vite-imagetools. Also, nuxt/image can pre-generate images.

Regarding optimization speed: I agree with you that there can be performance problems. Personally, I’ve noticed more issues when creating avif images, while webp was generated at a decent speed in my case. There could be environmental differences at play.

By default, nuxt/image uses the ipx provider (https://github.com/unjs/ipx) under the hood, which, if I’m not mistaken, relies on the sharp library (https://github.com/lovell/sharp). If you’re certain the issue lies in the optimization process itself, it might make sense to open an issue in the relevant repositories.

But once again — I’m not a nuxt/image developer; I’m just trying to help because I’ve run into similar challenges myself😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants