sveltejs / sveltejs/kit

A simple way to preload an enhanced:img

Open
#12,378 2 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pkg:enhanced-img
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

I have some large images on my site which are locked behind a button press but are not on a separate page. Because the images aren't rendered until the button is pressed, there is a delay after clicking the button before the image can be seen. So, I want the images to be preloaded. However, preloading images with multiple sources is quite messy, especially if those sources use multiple file types, and currently SvelteKit does not have a built-in way to handle this. I would imagine this would be a common issue for developers to run into, so rather than each developer having their own implementation, it would be very useful for SvelteKit to provide a way to handle this.

Describe the proposed solution

The simplest solution to use as a developer would be a preload property on the <enhanced:img/> element. However, this would need to be handled during preprocessing rather than when the element is rendered, which could be unintuitive.

A more intuitive solution would be a separate element (e.g: <preload:img>) which you use outside of any conditional blocks; this element should give a warning if no <enhanced:img/> with a matching src is found.


With both solutions, there would need to be a decision on how to handle multiple file types. I've identified three possible solutions and presented them in the below table. I personally think all of these options should be available, and the developer should get to choose between them based on the size of the images and how they are being used.

Multiple file types solution Pro Cons
Preloading all supported file types Always preloads the image which will actually be used - Eats into metered connection data - For large images, or sources with several file types, images might not finish loading before the image is shown
Just preload the largest file type (e.g: JPEG, but not AVIF) Preload can start and end very quickly - Might not actually preload the image used - Might use up more data than is necessary
Test support on the client (see here) Always preloads the image which will actually be used - Preload can't happen until later in the page's lifecycle - More JS and some small images need to be shipped to the client
Alternatives considered

Manually preloading each src and using my own implementation

Importance

nice to have

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The entry point is enhanced:img preprocessing; start by reviewing how it handles multiple sources and how conditional rendering affects generated output. Done means an agreed preload API, a defined strategy for multiple file types, and matching-source warnings if that approach is selected.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.