1

i created a phtml file, called in the catalog_product_view.xml file (inside the head.additional reference block), where i want to preload the main product image, for Core Vitals purpose, for example:

<link rel="preload" as="image" href="<?= $block->escapeUrl($imageUrl) ?>"/>

where $imageUrl is:

$imageUrl = $block->getImageUrl();

But, the url (cached) of the product image is not retrieved. No errors, but, simply this:

<link rel="preload" as="image" href/>

So, how can i simply get in the correct way the main product image url (the main product image in the product page?)

Thanks, regards

4 Answers 4

0

Try this in your phtml

<?php
    $_product = $block->getProduct();
    $imageUrl = $block->getImage($_product, 'product_base_image')->getImageUrl();
?>
<link rel="preload" as="image" href="<?= $block->escapeUrl($imageUrl) ?>"/>

I hope this works

0

no, it does not work. I think it's because the code is executed at head level, so, the instance of the product is not created at that time?

0

anybody found a solution to this? I think that a preload image in a product page of a Magento website is a critical improvement for SEO.

0

Has anybody found a solution for this?

I repeat: Magento 2.3.5, i want to insert in the head section the preload of the product image in a product page.

The target is to improve the LCP for SEO purpose. I have already inserted the preload of the font and of the head image (logo) with very good results.

i created a phtml file, called in the catalog_product_view.xml file (inside the head.additional reference block), where i want to preload the main product image, for Core Vitals purpose, for example:

<link rel="preload" as="image" href="<?= $block->escapeUrl($imageUrl) ?>"/>

where $imageUrl is:

$imageUrl = $block->getImageUrl();

But, the url (cached) of the product image is not retrieved. No errors, but, simply this:

<link rel="preload" as="image" href/>

I think it's because the code is executed at head level, so, the instance of the product is not created at that time, maybe?

So, how can i simply get in the correct way the main product image url (the main product image in the product page?)

Thanks

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.