Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structured Data with Lazy-Loaded images?

I want to put in Structured Data tags on Product-Images for SEO reasons.

<img class="img-responsive" src="placeholder URL" data-src="Actual URL"/>

The problem I'm running into is: Google picks up my SRC value which is only a placeholder image - the actual image(data-src) is only loaded when the user scrolls enough to bring the image into view.

like image 993
Qasim Avatar asked Nov 05 '15 08:11

Qasim


2 Answers

Use a <noscript> block and put your image with the structured data tag in there. Google will then use that image rather than the image placeholder. This also means any users without JS enabled (there are a few, but they're still about!) will also still see the images. Note: You need to then disable the placeholder images if JS isn't enabled otherwise non-JS users will see two images.

For example

 <img class="img-responsive js-only" src="placeholder URL" data-src="Actual URL"/>
 <noscript>
 <img src="Actual URL" data-src="Actual URL" itemprop="image"/>
 </noscript>

Verified this approach using Google's structured data testing tool - https://developers.google.com/structured-data/testing-tool/.

Edit: how to show images only with JS:

You don't need to hide noscript images - anything inside a noscript block is only used if JavaScript is disabled. You can show the responsive images only when JS is enabled by adding class="no-js" to the HTML element, the following JavaScript block to the HEAD:

<script>
    var headElement = document.getElementsByTagName("html")[0]; 
    var regExp = new RegExp('(\\s|^)no-js(\\s|$)'); 
    headElement.className = headElement.className.replace(regExp,' js ');  
</script> 

and the following CSS:

html.no-js .js-only {
    display:none;
}
html.js .no-js {
    display:none
} 
like image 71
andyberry88 Avatar answered Oct 23 '22 18:10

andyberry88


Just add the attribute content to your img tag with the real image url. Google will use this, but the browser will ignore it.

<img src="placeholder URL" data-src="Actual URL" itemprop="image" content="Actual URL" />

This is in my opinion better because you won't need extra meta, noscript or JS code, just one single attribute.

Tested and tried with Google's structured testing tool (https://search.google.com/structured-data/testing-tool)

like image 32
Vincent V Avatar answered Oct 23 '22 18:10

Vincent V