2

I initially wrote some code that created the structured data for breadcrumb and product markup on the server's side and rendered them in the <body> of the page. While the mark up was correct according to Google's Structured Data Testing Tool, I saw no difference in the SERPs and when I opted to "fetch by URL" instead of "snippet" the tool was fetching only the <head> of the given page's URL, so no structured data were being detected.

This was confusing, as in Google's documentation it is stated that the structured data can be inserted in the body or head of an HTML page.

I switched my approach and decided to place the product structured data in the <head> section, while leaving the breadcrumb structured data to be rendered as it was. I read in the documentation that there is no problem if you append the data dynamically by JavaScript or AJAX, so I wrote a script that creates the structured data and appends them at the <head> of the page.

When I tested my new links by "fetch URL" option, Google's tool was fetching the whole page (which it did not do before) but was not displaying the product structured data. This time however, it was fetching the breadcrumb data, which it did not do before and without changing anything in my code.

  1. Why was the tool initially fetching only the <head> but not the <body>?
  2. Why is the tool later also fetching the <body> part?
  3. Why is the product mark up not displaying when the page if fetched by URL? Can it be that, contradictory to the documentation, the structured mark up is not detected when appended by JavaScript?

The JavaScript code that creates and appends the product mark up to the <head> runs on document.ready. The breadcrumb mark up renders together with the HTML during runtime.

1
  • Also, I do not want to provide the URLs I am testing so please refrain from asking for them. Happy to provide any additional information that would help anyone cracking the case.
    – Prinny
    Feb 4, 2019 at 11:44

2 Answers 2

1

I never came across a case where the SDTT only fetches the head. My guess would be that something was off about one of the servers (yours or Google’s).

While Google Search supports JSON-LD added via JavaScript, it doesn’t necessarily mean that Google’s SDTT supports this, too. I came across many cases where the SDTT doesn’t run JavaScript, and no case where it runs JavaScript (but that doesn’t have to mean much, as I never add JSON-LD on the client-side).

If the project allows it, the best case is to add any structured data on the server-side. This allows all consumers to make use of your structured data (as most of them don’t support JavaScript).

If the SDTT only fetches the head again, I would suggest to investigate this issue.

1

I had a case where plain json-ld could not be added to a WordPress article body. The following seemed to work, but the CDATA opening and closing tags had to remain on separate lines.

<script type="application/ld+json">
//<![CDATA[
{
  "@context": "https://schema.org",
  ...
}
//]]>
</script>

I believe that WordPress was compressing the scripts and breaking them (because the CDATA tag begins with a JS comment). Compressing just the json part seemed to get WP leave the scripts alone.

<script type="application/ld+json">
//<![CDATA[
{"@context": "https://schema.org",  ...}
//]]>
</script>

This will not work

<script type="application/ld+json">//<![CDATA[{"@context": "https://schema.org", ...}//]]></script>

Your Answer

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

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