React客户端添加ld+json脚本标签
Add ld+json script tag in client-side React
我目前已经构建了一个 React 应用程序。由于它是一个 SPA,它只有一个 index.html 文件。我想添加 2 "ld+json" script
标签,即用于某条路线的评论和书签。
我已经在该组件的 componentDidMount
中注入了 script
标记,但 Google 结构化数据测试工具无法读取它。
是不是因为Google是直接从index.html读取的,而我的script
标签是捆绑在[=44=里面的,所以无法读取?
是否可以在客户端 React 中执行此操作?服务器端渲染是唯一可行的方法吗?
-- 详细解释---
我目前想实现一个像 IMDB 那样的系统,即每当我们在 goole 中搜索电影时; IMDB 搜索结果将在 google 页面本身显示电影的评级。为此,我需要在我的 index.html 文件
中放置一个脚本
<script type='application/ld+json'>
{
"@context": "http://schema.org/",
"@type": "Review",
"itemReviewed": {
"@type": "Thing",
"name": "Name"
},
"reviewRating": {
"@type": "Rating",
"ratingValue": "3",
"bestRating": "5"
},
"publisher": {
"@type": "Organization",
"name": "1234"
}
}
</script>
因为我的应用程序是一个 SPA,所以我不能把它放在我的主 index.html 文件中。
我目前的做法:
假设“/movies/inception”路由呈现 "MovieDetail" 组件。所以,我目前正在该组件的末尾添加脚本。
import React from 'react';
import JsonLd from '../path_to_JSONLD';
class MovieDetail extends React.Component {
render(){
let data = {
"@context": "http://schema.org/",
"@type": "Review",
"itemReviewed": {
"@type": "Thing",
"name": "Name"
},
"reviewRating": {
"@type": "Rating",
"ratingValue": "3",
"bestRating": "5"
},
"publisher": {
"@type": "Organization",
"name": "1234"
}
}
return(
<SOME COMPOENTS />
<JsonLd data={data} />
)
}
我的 JsonLd 组件
import React from 'react';
const JsonLd = ({ data }) =>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(data) }}
/>;
export default JsonLd;
因此,当我检查组件时;我可以看到动态添加的脚本标签。但是,在结构测试工具“https://search.google.com/structured-data/testing-tool”中。它在验证后不显示架构。因此,我问它是否可以通过客户端完成,或者 SSR 是唯一的解决方案,我可以在其中提供更新的 index.html 作为响应。
我希望这消除了困惑。谢谢!
解决方案:使用"react-meta-tags"Link:https://github.com/s-yadav/react-meta-tags
import React from 'react';
import MetaTags from 'react-meta-tags';
import JsonLd from 'path_to_jsonld';
export default class MetaComponent extends React.Component {
render() {
return (
<div className="wrapper">
<MetaTags>
<title>{this.props.title}</title>
<meta property="og:type" content="website" />
<meta name="description" content={this.props.description} />
<meta name="og:description" content={this.props.description} />
<meta property="og:title" content={this.props.title} />
<meta property="og:url" content={window.location.href} />
<meta property="og:site_name" content={"content"}
/>
{
this.props.jsonLd &&
<JsonLd data={this.props.jsonLd} />
}
</MetaTags>
</div>
)
}
}
然后我在我的主要组件中导入了这个组件
import React from 'react';
import MetaComponent from '../path_to_Metacomponent';
class MovieDetail extends React.Component {
render(){
let data = {
"@context": "http://schema.org/",
"@type": "Review",
"itemReviewed": {
"@type": "Thing",
"name": "Name"
},
"reviewRating": {
"@type": "Rating",
"ratingValue": "3",
"bestRating": "5"
},
"publisher": {
"@type": "Organization",
"name": "1234"
}
}
return(
<SOME COMPOENTS />
<MetaComponent jsonLd={data} title={"abcd"} description={"xyza"} />
)
}
这个包所做的是动态地将脚本标签插入到 head 标签中,因为脚本现在没有捆绑在 main.js 文件中 google 能够从源代码中读取它。
对我来说,React Helmet效果很好。
<Helmet>
<script className='structured-data-list' type="application/ld+json">{structuredJSON}</script>
</Helmet>
其中 structuredJSON
类似于此类函数的结果:
export const structuredDataSingle = (prod, imgPath, availability) => {
let data = {
"@context": "http://schema.org/",
"@type": "Product",
"name": `${prod.title}`,
"image": prod.images.map((item) => imgPath + item),
"description": prod['description'],
"url": location.href,
"offers": {
"@type": "Offer",
"priceCurrency": `${prod['currency'] || "₴"}`,
"price": prod['price'] ? `${parseFloat(prod['price'])}` : 0,
"availability": `${availability}`,
"seller": {
"@type": "Organization",
"name": "TopMotoPro"
}
}
};
// brand
if(prod['brand']) {
data['mpn'] = prod['brand'];
data['brand'] = {
"@type": "Thing",
"name": `${prod['brand']}`
};
}
// logo
if(prod['logo']){
data['logo'] = imgPath + prod['logo'];
}
return JSON.stringify(data);
};
As suggested, you can use React Helmet 内联脚本元素。您还可以将它们内联并使用变量插值:
<script type="application/ld+json">{`
{
"@context": "http://schema.org",
"@type": "${typeVariable}"
}
`}</script>
你可以简单地危险地渲染它
<script type='application/ld+json' dangerouslySetInnerHTML={ { __html: `{ "@context": "http://schema.org", "@type": "LocalBusiness", ... }`}} />
这对我有帮助:
- 准备好你的 JSON 并对其进行字符串化。
const ORG_SCHEMA = JSON.stringify({
"@context": "http://schema.org",
"@type": "Organization",
"name": "Allround",
"description": "Allround - An online learning experience through creative and co-curricular pursuits for kids aged 5-15. Learn western vocals, keyboard, chess & Spanish from experts.",
"url": "https://allround.club/",
"logo": "https://allround.club/favicon.svg",
"address": {
"@type": "PostalAddress",
"streetAddress": "DD3, Diamond District, HAL Old Airport Road",
"addressLocality": "Bengaluru",
"addressRegion": "Karnataka",
"postalCode": "560008",
"Telephone": "+918035003600"
},
"sameAs": [
"https://www.facebook.com/Allround-Learning",
"https://www.linkedin.com/company/allround-club",
"https://www.instagram.com/allround.club/"
]
});
- 使用dangerouslySetInnerHTML在脚本标签中设置字符串化JSON。
<script type='application/ld+json' dangerouslySetInnerHTML={ { __html: ORG_SCHEMA} } />
您可以在此处查看它如何显示给抓取工具:
https://allround.club/
您可以通过 Google:
使用 react-schemaorg
我目前已经构建了一个 React 应用程序。由于它是一个 SPA,它只有一个 index.html 文件。我想添加 2 "ld+json" script
标签,即用于某条路线的评论和书签。
我已经在该组件的 componentDidMount
中注入了 script
标记,但 Google 结构化数据测试工具无法读取它。
是不是因为Google是直接从index.html读取的,而我的script
标签是捆绑在[=44=里面的,所以无法读取?
是否可以在客户端 React 中执行此操作?服务器端渲染是唯一可行的方法吗?
-- 详细解释--- 我目前想实现一个像 IMDB 那样的系统,即每当我们在 goole 中搜索电影时; IMDB 搜索结果将在 google 页面本身显示电影的评级。为此,我需要在我的 index.html 文件
中放置一个脚本<script type='application/ld+json'>
{
"@context": "http://schema.org/",
"@type": "Review",
"itemReviewed": {
"@type": "Thing",
"name": "Name"
},
"reviewRating": {
"@type": "Rating",
"ratingValue": "3",
"bestRating": "5"
},
"publisher": {
"@type": "Organization",
"name": "1234"
}
}
</script>
因为我的应用程序是一个 SPA,所以我不能把它放在我的主 index.html 文件中。
我目前的做法: 假设“/movies/inception”路由呈现 "MovieDetail" 组件。所以,我目前正在该组件的末尾添加脚本。
import React from 'react';
import JsonLd from '../path_to_JSONLD';
class MovieDetail extends React.Component {
render(){
let data = {
"@context": "http://schema.org/",
"@type": "Review",
"itemReviewed": {
"@type": "Thing",
"name": "Name"
},
"reviewRating": {
"@type": "Rating",
"ratingValue": "3",
"bestRating": "5"
},
"publisher": {
"@type": "Organization",
"name": "1234"
}
}
return(
<SOME COMPOENTS />
<JsonLd data={data} />
)
}
我的 JsonLd 组件
import React from 'react';
const JsonLd = ({ data }) =>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(data) }}
/>;
export default JsonLd;
因此,当我检查组件时;我可以看到动态添加的脚本标签。但是,在结构测试工具“https://search.google.com/structured-data/testing-tool”中。它在验证后不显示架构。因此,我问它是否可以通过客户端完成,或者 SSR 是唯一的解决方案,我可以在其中提供更新的 index.html 作为响应。
我希望这消除了困惑。谢谢!
解决方案:使用"react-meta-tags"Link:https://github.com/s-yadav/react-meta-tags
import React from 'react';
import MetaTags from 'react-meta-tags';
import JsonLd from 'path_to_jsonld';
export default class MetaComponent extends React.Component {
render() {
return (
<div className="wrapper">
<MetaTags>
<title>{this.props.title}</title>
<meta property="og:type" content="website" />
<meta name="description" content={this.props.description} />
<meta name="og:description" content={this.props.description} />
<meta property="og:title" content={this.props.title} />
<meta property="og:url" content={window.location.href} />
<meta property="og:site_name" content={"content"}
/>
{
this.props.jsonLd &&
<JsonLd data={this.props.jsonLd} />
}
</MetaTags>
</div>
)
}
}
然后我在我的主要组件中导入了这个组件
import React from 'react';
import MetaComponent from '../path_to_Metacomponent';
class MovieDetail extends React.Component {
render(){
let data = {
"@context": "http://schema.org/",
"@type": "Review",
"itemReviewed": {
"@type": "Thing",
"name": "Name"
},
"reviewRating": {
"@type": "Rating",
"ratingValue": "3",
"bestRating": "5"
},
"publisher": {
"@type": "Organization",
"name": "1234"
}
}
return(
<SOME COMPOENTS />
<MetaComponent jsonLd={data} title={"abcd"} description={"xyza"} />
)
}
这个包所做的是动态地将脚本标签插入到 head 标签中,因为脚本现在没有捆绑在 main.js 文件中 google 能够从源代码中读取它。
对我来说,React Helmet效果很好。
<Helmet>
<script className='structured-data-list' type="application/ld+json">{structuredJSON}</script>
</Helmet>
其中 structuredJSON
类似于此类函数的结果:
export const structuredDataSingle = (prod, imgPath, availability) => {
let data = {
"@context": "http://schema.org/",
"@type": "Product",
"name": `${prod.title}`,
"image": prod.images.map((item) => imgPath + item),
"description": prod['description'],
"url": location.href,
"offers": {
"@type": "Offer",
"priceCurrency": `${prod['currency'] || "₴"}`,
"price": prod['price'] ? `${parseFloat(prod['price'])}` : 0,
"availability": `${availability}`,
"seller": {
"@type": "Organization",
"name": "TopMotoPro"
}
}
};
// brand
if(prod['brand']) {
data['mpn'] = prod['brand'];
data['brand'] = {
"@type": "Thing",
"name": `${prod['brand']}`
};
}
// logo
if(prod['logo']){
data['logo'] = imgPath + prod['logo'];
}
return JSON.stringify(data);
};
As
<script type="application/ld+json">{`
{
"@context": "http://schema.org",
"@type": "${typeVariable}"
}
`}</script>
你可以简单地危险地渲染它
<script type='application/ld+json' dangerouslySetInnerHTML={ { __html: `{ "@context": "http://schema.org", "@type": "LocalBusiness", ... }`}} />
这对我有帮助:
- 准备好你的 JSON 并对其进行字符串化。
const ORG_SCHEMA = JSON.stringify({
"@context": "http://schema.org",
"@type": "Organization",
"name": "Allround",
"description": "Allround - An online learning experience through creative and co-curricular pursuits for kids aged 5-15. Learn western vocals, keyboard, chess & Spanish from experts.",
"url": "https://allround.club/",
"logo": "https://allround.club/favicon.svg",
"address": {
"@type": "PostalAddress",
"streetAddress": "DD3, Diamond District, HAL Old Airport Road",
"addressLocality": "Bengaluru",
"addressRegion": "Karnataka",
"postalCode": "560008",
"Telephone": "+918035003600"
},
"sameAs": [
"https://www.facebook.com/Allround-Learning",
"https://www.linkedin.com/company/allround-club",
"https://www.instagram.com/allround.club/"
]
});
- 使用dangerouslySetInnerHTML在脚本标签中设置字符串化JSON。
<script type='application/ld+json' dangerouslySetInnerHTML={ { __html: ORG_SCHEMA} } />
您可以在此处查看它如何显示给抓取工具: https://allround.club/
您可以通过 Google:
使用 react-schemaorg