0

I deployed JSON-LD asynchronously inejecting it in a script tag:

var jsonLD = document.createElement('script');
jsonLD.type = 'application/ld+json';
jsonLD.innerHTML = JSON.stringify({
  "@context": "https://json-ld.org/contexts/person.jsonld",
  "@id": "http://dbpedia.org/resource/John_Lennon",
  "name": "John Lennon",
  "born": "1940-10-09",
  "spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
});
document.head.appendChild(jsonLD);

Google's testing tool does not recognize any JSON-LD when checking the URL though. Will it still be indexed or is Google (and other bots) are blind to asychonously loaded JSON-LD?

SomewhereDave
  • 453
  • 8
  • 20

2 Answers2

0

The testing tool doesn't evaluate any Javascript. Instead you will have to hard-code the JSON-LD into your HTML. Here is an example snippet which is parsed just fine by the tool:

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <script type="application/ld+json">
   {
      "@type": "person",
      "@context": "https://json-ld.org/contexts/person.jsonld",
      "@id": "http://dbpedia.org/resource/John_Lennon",
      "name": "John Lennon",
      "born": "1940-10-09",
      "spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
    }
  </script>
</body>
</html>
caffeinated.tech
  • 6,428
  • 1
  • 21
  • 40
  • So the follow up question then is, if the testing tool behaves like the actual crawler? The JS injection works just fine for regular browser clients in that it is loaded, but I'm not sure about the crawler. – SomewhereDave Aug 12 '19 at 15:17
  • Most bots don't execut JS, but I believe google now processes some JS. See [this answer for details](https://stackoverflow.com/questions/22588705/when-does-googlebot-execute-javascript) – caffeinated.tech Aug 12 '19 at 16:27
0

The Structured Data Testing Tool has limited rendering ability, and it looks like it can't cope with your example.

Googlebot renders using an evergreen version of Chrome. i.e. it renders very well.

One way to test is to view the page in Chrome and copy its rendered HTML into the Structured Data Testing Tool. You will probably then see your data.

Note that most bots do not render at this time. So generating your SD in that way will limit the number of systems that can see your data.

Tony McCreath
  • 2,882
  • 1
  • 14
  • 21