kit icon indicating copy to clipboard operation
kit copied to clipboard

Vite transforms enhanced:img every build

Open DusterTheFirst opened this issue 3 years ago • 2 comments

Describe the problem

Building a site using vite build after making no changes to images referenced by enhanced:img or the image tags themselves will always result in vite re-optimizing those images. This causes build times to baloon upwards of 20 minutes in some cases. In projects with many, static images, this can be a large pain point.

Describe the proposed solution

The processed images should be able to be cached, only rebuilding if there was a change to the source image data, or if the requested dimensions or other attributes changed.

This can be implemented using a cache that stores a hash of the source image contents to detect if the source changed, as well as a hash of the query parameters (ie. the directives found here: https://github.com/JonasKruckenberg/imagetools/blob/main/docs/directives.md)

It may be important for space reasons to not store more than one build worth of cache, so this cache might want to be cleared of any stale items after the build.

Alternatives considered

To work around this large build time, users can pre-optimize their images, and simply reference them without ?enhanced. This shifts the work onto the user to produce the optimized images, but will no longer take minutes to build every image.

It may be useful to allow the optimization step to be separate from the vite-build step as well. This would allow the image optimization to be done separately of the html generation.

Importance

would make my life easier

Additional Information

I am a recent adopter of SvelteKit, moving from Gatsby. From my experience, gatsby has implemented a system like this as subsequent build times are not as long as the initial build.

DusterTheFirst avatar Nov 28 '23 21:11 DusterTheFirst

For me this would not only make life easier, but because we use playwright to test our website it actually breaks the workflow. When using enhanced-img the build takes easily more than 3 minutes every time and so often playwright will time out waiting for the webserver to start.

MrGreenTea avatar Dec 01 '23 16:12 MrGreenTea

This is fixed in the latest version of @sveltejs/enhanced-img (version 0.2.0) changelog

Previously, caching was not done, hence the package would transform images at each build. And as you added more images, the build time would increase significantly. Now with the latest version, my build time on Netlify went down significantly: output

I have written about this in my blog if you'd like to learn more: https://www.danyelkoca.com/en/blog/reduce-deploy-time-with-caching

danyelkoca avatar May 11 '24 21:05 danyelkoca