How do I preload the first image on product pages? And the first image in the slider?

Solved
Preben_Frenning
Shopify Partner
37 0 12

Hi! 

I've been wanting to preload the first image on product pages for quite some time now. As have I been wanting to preload the first image in the slider on our home page. 

How can I do that?

Something like <link rel="preload" as="image" href="{ product.image[0]}">

cdnmikes
Excursionist
21 0 7

@Preben_Frenning did you figure out a good way to this yet?

I am looking for some examples of how to properly prefetch or preload items in a Shopify theme.

Something like loading product images on product pages on hover from a link on a collection page. Or prefetching collection page items from a link on the nav.

 

oreoorbitz
Shopify Partner
127 13 77

If your on the product page, you can access the product object for that product.

So product.media.first will get you the first media element, in your case an image.

Then you use the same image dimensions as you do on the product page,  so that the resulting url will match what you use on the product page. 

As for dynamically getting the first image of the carousel in the homepage, I have no idea besides writing a tool to find the image and dynamically inject a preload into your theme.

Available for freelance. I specialize in speed improvement and theme development.
https://www.upwork.com/fl/orionholmes
cdnmikes
Excursionist
21 0 7

Anyway we can do something like this Sapper - Prefetch in this video is shows how we can pre load data before the use goes to that page.

I am trying to set this up with themekit and webpack but haven't quite figured it out yet.

Preben_Frenning
Shopify Partner
37 0 12

I haven't had much time to look more into it unfortunately, but I am also looking to solve something similar. Ideally, also prefetching the cart page after adding a product to the cart with AJAX as well.

I did try Instantpage a little bit, but never had time to configure it properly to work well with Shopify. But I'm gonna try some manual prefetching next time I am working on performance. It felt great, and a lot faster, but had a negative impact on our CR, most likely because something interfered.

The preloading of the first image is mainly to improve the perceived speed and rendering, with the purpose of getting a better CLS and LCP.

Preben_Frenning
Shopify Partner
37 0 12

Thanks! I'll try this. Though find it a bit challenging to have it preload the same image size. Will need to look more into how that actually is determined in Shopify.

0 Likes
Preben_Frenning
Shopify Partner
37 0 12

This technique looks like it can be added to the code manually to places you know will work. I'll try adding it to the menu and collection pages this week.

You can preload the whole site by using this app: Fastify. The app automatically preloads the page that users are going to click on.

 

pandicorn.app
0 Likes
DariusWS
Excursionist
8 0 5

For me working as charm like this: 

  {% if template contains 'product' %}
    <link rel="preload" href="{{ product.media.first | img_url: 'master' }}" as="image">
  {% endif %}

 

 

Mircea_Piturca
Shopify Partner
1485 42 313

@DariusWS DariusWS solution is the way to go but make sure about a few things:

You need to preload exactly the image displayed, product.media.first will return the first product image. You want to preload the featured_image or selected_or_first_available_variant image. This depends on your setup. Also, make sure that your img_url filter matches.

Preload exactly the same image displayed in your product gallery, otherwise, you can do more harm than good.

Finally—Add variant descriptions to your products