react-helmet icon indicating copy to clipboard operation
react-helmet copied to clipboard

Example of dynamically creating JSON-LD to create Google Rich Cards in Meteor?

Open kpma5891 opened this issue 9 years ago • 12 comments

I would like to see if somebody could post an example of how to dynamically create a JSON-LD to create a google rich card? I had read issues here and stack overflow posts and I think it's possible but I haven't figured it out how. I would really appreciate if somebody could show me an example of how to implement this.

This is what I've so far:

<Helmet> <script type="application/ld+json"> { { "@context": "http://schema.org", "@type": "WebSite", "name": ${this.state.title.toString()}, "url": ${this.state.url.toString()}, "description": ${this.state.body.toString()}, "image": ${this.state.imgUrlGal.toString()} } } </script> </Helmet>

Thanks

kpma5891 avatar Nov 04 '17 21:11 kpma5891

see Reference Guide https://github.com/nfl/react-helmet

<Helmet>
  <script type="application/ld+json">{`
    {
      "@context": "http://schema.org",
      "@type": "WebSite",
      "name": "${this.state.title}"
    }
  `}</script>
</Helmet>

vanvuongngo avatar Apr 19 '18 11:04 vanvuongngo

@vanvuongngo , if we have collection , for example multiple name, need to bind dynamically. how we can do.

Raja-Ashutosh avatar Jun 06 '18 12:06 Raja-Ashutosh

@vanvuongngo

  I need below structure 

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [{
    "@type": "ListItem",
    "position": 1,
    "item": {
      "@id": "https://example.com/books",
      "name": "Books",
      "image": "http://example.com/images/icon-book.png"
    }
  },{
    "@type": "ListItem",
    "position": 2,
    "item": {
      "@id": "https://example.com/books/authors",
      "name": "Authors",
      "image": "http://example.com/images/icon-author.png"
    }
  },{
    "@type": "ListItem",
    "position": 3,
    "item": {
      "@id": "https://example.com/books/authors/annleckie",
      "name": "Ann Leckie",
      "image": "http://example.com/images/author-leckie-ann.png"
    }
  },{
    "@type": "ListItem",
    "position": 4,
    "item": {
      "@id": "https://example.com/books/authors/ancillaryjustice",
      "name": "Ancillary Justice",
      "image": "http://example.com/images/cover-ancillary-justice.png"
    }
  }]
}
</script>

Raja-Ashutosh avatar Jun 06 '18 17:06 Raja-Ashutosh

itemListElement": [{ "@type": "ListItem", "position": 1, "item": { "@id": "https://example.com/books", "name": "Books", "image": "http://example.com/images/icon-book.png" }

Dynamically need to inject into script tag. any suggestion ?

Raja-Ashutosh avatar Jun 06 '18 17:06 Raja-Ashutosh

Apologies mate but you need to learn react. This isn't a helmet question. This is a question about how to use JSX.

mfowlewebs avatar Jul 19 '18 20:07 mfowlewebs

My website is https://israel-escorts.com/. Which are kind of rich cards can be used for my site?

devhelper avatar Sep 09 '18 10:09 devhelper

SEO

structured-data

https://developers.google.com/search/docs/guides/intro-structured-data

https://search.google.com/structured-data/testing-tool

JSON-LD

https://json-ld.org/

https://en.wikipedia.org/wiki/JSON-LD

xgqfrms avatar May 23 '19 14:05 xgqfrms

Why not to write more elegant way: <script type="application/ld+json">{JSON.stringify(seo.structuredData)}</script>

themondays avatar Sep 30 '19 09:09 themondays

I am following @themondays approach.

I am getting two script elements in <head>.

Screenshot 2019-10-02 at 14 12 45

They have different values. I guess it is due to re-renders with a different value. Is there a way to replace the firsts ones with the last one?

PolGuixe avatar Oct 02 '19 12:10 PolGuixe

@PolGuixe I think you can add key prop in your script tag, like <script key="my-structured-data" type="application/ld+json">{JSON.stringify(seo.structuredData)}</script>

DmytroUlianov avatar Nov 04 '19 14:11 DmytroUlianov

Just my 2 cents, most of the examples here are wrong. If you want your JSON minified and not encoded to HTML you should use this: <script type="application/ld+json" dangerouslySetInnerHTML={{__html: JSON.stringify(myData)}}></script>.

igo avatar Jan 11 '20 09:01 igo

@igo have you tried using Helmet around script tag? Helmet accepts a encodeSpecialCharacters option that can be set to false.

<Helmet encodeSpecialCharacters={false}>
  <script type="application/ld+json">{JSON.stringify(someStructuredData)}</script>
</Helmet>

sebranly avatar Apr 20 '20 23:04 sebranly