-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
data-sveltekit-preload-data / preloadData - preload Images too #10864
Comments
I also thought of something like that, but i think here are some valid considerations:
I think your best bet is to load image previews/placeholders with js when loading page/component data, to 'instantly' show some preview and occupy the correct dimensions, and then just load the full-res image once the user navigated to the page. |
Starting the download of images that have |
hmm. Starting the download of any images on user hover seems pretty disrespectful of a user's bandwidth. Onpointerdown, yeah, then the user most likely will navigate to that page, but not in all cases. Also, one would need to somehow know the size of the asset. imagine preloading 10mb of images, that's probably gonna hog your connection for some time. (I'm thinking about people on slow or unstable connections). |
I agree that overly optimistic image preloading could cause problems. That's why I specified that only images with There are a few reasons that I don't believe preloading would cause network-congestion problems:
If bandwidth is a problem, then preloading is disrespectful to the users. Agreed! But if bandwidth isn't a problem, which in most places it's not, then not preloading is disrespectful to the users' time. If developers are worried that their userbase is bandwidth limited they could simply not use We need to trust developers to do the right thing for their product & userbase. The best way to do that is to open as many doors as possible and letting them choose. |
Still, while it would be possible, i am really not sure if it is even worth the hassle. To address your reasons:
I trust devs to do the right thing. But a framework often also acts as a sort of 'guideline': for example, sveltekit does not offer to use 'sveltekit-preload-data' eagerly, without hover, yet it supports it with code. Still the functions to preload data and code are pretty much identical (i am assuming this, didn't verify) so what's stopping them to make preload data and preload-code have the same functionalities? For now i guess i'll resort to using tiny placeholders that i get in my loading function and inline them as base64. Because with sveltekit, images are only one roundtrip away from the preloaded data. As for me, i'd rather have 'instantaneous' placeholders on all my e.g. thumbnails, instead of eventually preloaded images on some of my content. Nevertheless, i think it is a very interresting topic to discuss. |
Can't you somehow use html tag? |
@rbozan I do not think this I possible. Because I want to start loading images from route b while im on route a. |
I understand but if you have Apparently GitHub removed my tag or tried to render it. |
A workaround I've come up with is to implement both /* +page.ts */
export function load({ data }) {
if (typeof document !== 'undefined') {
// Preload the header image
new Image().src = data.headerImageURL;
}
return data;
} |
I thought a while about this, and came up with the following optimized approach:
This guarantees: |
Describe the problem
I think it is a little inconvenient that the preloadDat doesn't have a mechanism to preload Images too.
Svelte is focused on DX/convenient/rapid development and today's Website often contain a fair amount of Images. Which often contribute to the overall appearance / content of the website. So it would be nice to have them instantly available once a user navigate -> prefetch them before the user navigates.
I couldn't find a nice solution to implement this atm. I found a few thing that could be done but all fall apart at one place or the other:
1.) Programatically load the images via a
Image
or<link rel="preload" src="..."/>
-> this is somewhat fine and working but depending how you get the images can be some extra amount of work and only works if the server has ancache-policie
in place.2.) Somehow loading the Picture in the load function of
+page.server.ts
(like a blob an save it to a store or whatever) - this works well but falls apart if the user clicks on links fast and on 'full reloads' because then the function takes time to complete an blocks the ui from appearing. Thefull reload
issue can be fix if one checks theisDataRequest
attribute.3.) I had some more will add later
Describe the proposed solution
Have images fetched automatically, maybe have a special attribute on the img`element to tag them as important and only prefetch this images
Alternatives considered
No response
Importance
would make my life easier
Additional Information
I know that the problem arises because the navigation is done client side and the preload does fetch js/json. which includes the image urls but the browser won't start to fetch the until they become part of the html.
I started with svelte two weeks ago maybe I missed something
The text was updated successfully, but these errors were encountered: