Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anyway to use JSON-LD Schema not inlined

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.

like image 731
Cynic Avatar asked Feb 12 '16 01:02

Cynic


People also ask

Should I use microdata or JSON-LD?

With JSON-LD, a JavaScript object is inserted into the HTML of your page to define data, whereas microdata uses HTML tags and attributes to define data. In its structured data guidelines, Google states that it recommends JSON-LD over microdata for web content.

Where does JSON-LD schema go?

Google recommends adding JSON-LD to the <head> section of the HTML document; however, it's okay if the JSON-LD is within the <body> section.

Is JSON-LD valid JSON?

A JSON-LD document is always a valid JSON document.


1 Answers

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();
like image 81
NoCelery Avatar answered Oct 22 '22 19:10

NoCelery