Search code examples
javascriptschema.orgjson-ldgoogle-rich-snippets

Schema.org snippet in JavaScript with JSON-LD syntax not detected


I wrote the schema.org of my website in JavaScript with JSON-LD syntax. When I test on Google's test site, it works... but on schema.org's test site, it doesn't. When I copy/paste my shema.org by hand, it also works for the schema.org test site...

Should I be worried about my schema.org being recognized? I would like your feedback, because it's the first time I've done this in JavaScript... Thank you.


Solution

  • The problem is that because you're adding structured data dynamically, reading it depends on each consumer's implementation, i.e. if it is set to read dynamically added structured data.

    Apparently, Google's tool does, but if you read Validator's page, it says:

    Schema.org Markup Validator

    (...)

    This service will validate Schema.org-based structured data embedded in web pages.

    https://webschemas.org/docs/validator.html

    So, the keyword is embedded, and judging by it and your tests, it will at the moment probably read it only if it's already there when the page is served, not when scripts load, and then add it, which is why it works when you copy/paste it manually.

    So, it depends what you're trying to achieve. If you want for Google to find it, then you're fine. If you also want other structured data parsers to find it, then you'll need to adapt to their current abilities, and play safe and embed it, i.e. hardcode it, instead of adding it via JavaScript/jQuery, because parsing dynamically added structured data takes more resources, time, complex software, coding etc..