Does JSON-LD have to be embedded?
Asked Answered
C

1

30

We are currently using the Microdata format to expose data to search engines and we are looking at exposing more info to be able to support some more advanced Google Search features. As I'm working my way through the fields I'm finding I need information that we currently load asynchronously so it is not a part of the initial response.

JSON-LD looks like its what Google prefers but all the examples I've seen have it embedded in the page. Could you have a link to a JS file so it gets loaded as a separate call? Something like

<script type="application/ld+json" src="/myid123/jsonld.js"></script>
Catapult answered 16/6, 2015 at 10:11 Comment(1)
Possible duplicate of How to externalize json-ld and include in html docAdila
G
14

If you are using the script element as data block, "the src attribute must not be specified".

If the script element is not used as data block, it has to be "used to include dynamic scripts". But a JSON-LD document is not a dynamic script.

For linking to another resource, just like you do it with external stylesheets or Favicons, you can use the link element in the head (or the corresponding HTTP header):

<link href="/myid123/jsonld.js" rel="alternate" type="application/ld+json" />

In principle, consumers could follow this reference (possibly only if a certain link type is specified), and make use of data, just like they do it with embedded JSON-LD, Microdata, or RDFa.

However, consumers don’t have to do this, of course, and many probably don’t.
Google Search in particular does not claim to support it for consuming Schema.org in the JSON-LD format. However, they claim to support "dynamically injected" JSON-LD data blocks.

Gardener answered 17/6, 2015 at 0:51 Comment(7)
Sorry copy and paste error, link is what i meant. but if google supports dynamically loading via JS then this will work as well. Does google need some sort of indication to expect JSON-LD to be loaded dynamically?Catapult
What does this mean? Does Google crawl my json-ld which is referenced by <link href="/myid123/jsonld.js" type="application/ld+json" />? Or do I need to embeed it inline in my <script>-tag?Brashy
@nimo23: According to their documentation, they only support JSON-LD in script, not via link.Gardener
Okay, thanks. So according to #26892897, I have to use a script tag and put it inline. Unfortunately, even "@context" does not work with google search..Brashy
It's a pity that google doesn't yet support external JSON-LD document because we need to make the page bigger only to help the search engine; the user doesn't care that there is something hidden in the page as he/she loot at it.Hallerson
It's 2024...does ld+json still need to be embedded in a <script> tag or can we reference a linked document using the <link> tag? How ironic if linked documents cannot be a link.Eulaeulachon
@BrianTakita I've just tested it using a link tag and no, neither Google or Schema.org testing tools recognize any dataPound

© 2022 - 2024 — McMap. All rights reserved.