Keyword

How to Add Prefetch Image Link of K2 Item Image Using Template Override

  • Dayyal Dg.
  • Dayyal Dg.'s Avatar Topic Author
  • Offline
  • New Member
More
3 years 9 months ago #178044 by Dayyal Dg.
Hello,

I want to inject a prefetch image link into the header for webpage speed optimization.
Eg:

<link href="example.com/example.jpg" rel="prefetch" as="image" />

I have tried the below code into item view using template override:

<?php
defined('_JEXEC') or die;
// Prefetch Article Image
$doc = JFactory::getDocument();
$doc->addHeadLink('<?php echo $this->item->image; ?>', 'prefetch', 'rel', array('as' => 'image'));
?>

But the output is:
<link href="/<?php echo $this->item->image; ?>" rel="prefetch" as="image" />

How can I get the item image URL?

Please Log in or Create an account to join the conversation.

  • Dayyal Dg.
  • Dayyal Dg.'s Avatar Topic Author
  • Offline
  • New Member
More
3 years 9 months ago #178045 by Dayyal Dg.
I have added the below code into item view using template override, and it works.

<?php
defined('_JEXEC') or die;
if($this->item->params->get('itemImage') && !empty($this->item->image)) {
// Prefetch Article Image
$doc = JFactory::getDocument();
$prefetchImage = $this->item->image;
$doc->addHeadLink("$prefetchImage", 'prefetch', 'rel', array('as' => 'image'));
}
?>

Please Log in or Create an account to join the conversation.

More
3 years 9 months ago #178063 by JoomlaWorks
There is really no performance benefit in that...

It's best to use a loading="lazy" attribute on the img element if the image is out of view when the item page loads.

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum