12

Is there anyway to use JSON-LD without including the script inline in the HTML, but still get Google (& other) spiders to find it? Looking around I've seen some conflicting information.

If this was the JSON-LD file:

    <script type="application/ld+json">
    {
      "@context" : "http://schema.org",
      "@type" : "WebSite",
      "name" : "Example Site",
      "alternateName" : "example",
      "description" : "Welcome to this WebSite",
      "headline" : "Welcome to Website",
      "logo" : "https://example.com/public/images/logo.png",
      "url" : "https://example.com/"
    }
    </script>

And I have this in the head of the HTML:

<script src="/public/json-ld.json" type="application/ld+json"></script>

EDIT: I've also tried:

<link href="/public/json-ld.json" rel="alternate" type="application/ld+" />

Google Spiders seem to miss it and so does the testing tool unless I point it directly at the file. I'm trying to work around unsafe-inline in the CSP. And the only thing I can find is this, which would work in Chrome but don't want to be firing console errors on every other browser. Plus, I just like the idea of Schema.org data being abstracted out of the page structure. Would adding the JSON-LD to the sitemap for Google Webmaster Tools help?

Apologies, total noob to JSON-lD and keep ending up in email documentation (this would be for a site) or old documentation.

Cynic
  • 6,779
  • 2
  • 30
  • 49
  • Possible duplicate of [Does JSON-LD have to be embedded?](http://stackoverflow.com/questions/30864619/does-json-ld-have-to-be-embedded) – unor Feb 12 '16 at 13:39
  • Have tried this, and it's still not getting picked up. My best guess is JSON-LD for websites isn't supported yet in external files. – Cynic May 20 '16 at 16:56

1 Answers1

4

True, it can not be made external and it is not supported inline, but you can still achieve what you want by injecting it into the DOM via a JavaScript file.

Note: I am using an array for neatness so I can segment all the structured data elements and add an infinite amount of them. I have a more complicated version of this code on my websites and actually have an external server side rendered file masquerading as a JavaScript file.

An yes Google search bot does understand it. May take days for it to register and using Webmaster tools to force a re-crawl does not seem to force a refresh of JSON-LD data - seems like you just have to wait.

var structuredData = {
    schema: {
        corporation: {
            '@context':         'http://schema.org',
            '@type':            'Corporation',
            'name':             'Acme',
            'url':              'https://acme.com',
            'contactPoint':
            {
                '@type':        'ContactPoint',
                'telephone':    '+1-1234-567-890',
                'contactType':  'customer service',
                'areaServed':   'US'
            }
        },
        service: {
            '@context':         'http://schema.org/',
            '@type':            'Service',
            'name':             'Code optimization',
            'serviceOutput' :   'Externalized json',
            'description':      'Inline json to externalized json'
        },
    },
    init: function () {
        var g = [];
        var sd = structuredData;
        g.push(sd.schema.corporation);
        g.push(sd.schema.service);
        //etc.

        var o = document.createElement('script');
        o.type = 'application/ld+json';
        o.innerHTML = JSON.stringify(g);
        var d = document; (d.head || d.body).appendChild(o);
    }
}
structuredData.init();
NoCelery
  • 41
  • 4