Linking external JSON-LD schema (from Schema.org)
The script
element can be used for two things:
- dynamic/classic scripts
- data
And for data, the spec defines:
When used to include data blocks, the data must be embedded inline […]
So you may use the src
attribute only for scripts, not for data.
→ As JSON-LD is data, you have to inline it.
While linking a JSON-LD file is possible with the link
element, the Schema.org sponsoring search engines don’t seem to support it.
According to the documentation:
[…] Also, Google can read JSON-LD data when it is dynamically injected into the page's contents, such as by JavaScript code or embedded widgets in your content management system.
Which suggests that, while yo cannot link external json-ld data, you can still load it dynamically and inject it to the DOM. This might be good enough for your use case, as you can load static JavaScript on your page that will then inject the JSON-LD into the DOM.
This may, or may not help, but take a look at how Trevor Fox did it:
<script>
$.getJSON( "/your-schema-file.jsonld", function( data ) {
$( "<script/>", {
"type": "application/ld+json",
"html": JSON.stringify(data)
}).appendTo( "head" );
});
</script>
This may be your best option. He used jQuery version 3.1.1 for this solution.