【问题标题】:Using Gatsby.js, how do I add a route-specific og:image meta tag?使用 Gatsby.js,如何添加特定于路由的 og:image 元标记?
【发布时间】:2018-10-14 03:59:17
【问题描述】:

我已经在 React Helmet 中为我的根路由设置了社交分享图片,代码如下:

<meta property="og:url" content="https://foo.com" />
<meta property="og:image" content="https://myhostedimg.png" />

我在 Gatsby 中有一条单独的路线或“页面”,我想为其设置另一个 og:image 值。由于没有任何东西将og:image 链接到og:url,如何在我的页面上指定自定义og:urlog:image 链接?

【问题讨论】:

    标签: javascript reactjs facebook-opengraph gatsby react-helmet


    【解决方案1】:

    选项 1:您可以在 layout 主文件中进行路由嗅探,然后将两个道具(一个用于图像,一个用于路由)传递到控制您的元数据的任何组件中。

    在这个例子中,我的元数据组件被命名为metadata,但无论你的组件结构到底是什么,它都应该是有意义的:

    // Setup react stuff
    import React from 'react'
    import PropTypes from 'prop-types'
    
    // Get your custom gatsby components - assuming a pretty basic layout here
    import Metadata from '../components/Metadata'
    import Header from '../components/Header'
    import Footer from '../components/Footer'
    
    // Get images you want, also assuming you have these in a static folder and you don't need to use `gatsby-image` to get them
    import ogImgOne from './og-img-one.png'
    import ogImgTwo from './og-img-two.png'
    
    // Setup template wrapper
    export default class TemplateWrapper extends React.Component {
    
      // We want to render different base templates depending on the path the user hits
      render() {
    
        // Page 1
        if (this.props.location.pathname === "/") {
          return (
            <div className="page1">
              <Header />
              <Metadata ogImgSrc={ ogImgOne } 
                        ogUrl={ this.props.location.pathname } />
              <div>
                { this.props.children() }
              </div>
              <Footer />
            </div>
          )
        } 
    
        // Page 2
        if (this.props.location.pathname === "/page-2/") {
          return (
            <div className="page2">
              <Metadata ogImgSrc={ ogImgTwo } 
                        ogUrl={ this.props.location.pathname } />
              <Header />
              <div>
                { this.props.children() }
              </div>
              <Footer />
            </div>
          )
        }
      }
    }
    

    选项 2 只是使用 React Helmet,它包含在 Gatsby 中(至少从 v2.x 开始)。在此设置中,您可以设置一个使用 Helmet 的元数据组件,然后在 Gatsby 页面中轻松覆盖这些预设。 Helmet 只会覆盖您指定的元数据项,如果不需要调整,则保留其他项。只需在您的页面/组件中导入/调用Helmet 即可轻松覆盖:

    元数据.js:

    import React from 'react'
    import Helmet from 'react-helmet'
    
    import icon from '../../images/logo.png'
    import socialBanner from '../../images/PFB_2018_social.jpg'
    
    const Metadata = () => (
      <div>
        <Helmet>
          <title>Whatever</title>
    
          <meta property='og:image' content={ socialBanner } />
          <meta property='og:locale' content='en_US' />
          <meta property='og:type' content='website' />
          <meta property='og:title' content='Whatever' />
          <meta property='og:description' content="Description" />
          <meta property='og:url' content='https://example.org' />
          <meta property='og:updated_time' content='2019-01-31' />
        </Helmet>
      </div>
    )
    
    export default Metadata
    

    page-example.js:

    import React from 'react'
    import Helmet from 'react-helmet'
    
    export default class Example extends React.Component {
      render() {
        return (
          <div>
    
                {/* Custom metadata - assuming something coming from Node.js in pageContext prop */}
                <Helmet>
                  <title>Override here</title>
    
                  { /* Example from pageContext prop, gatsby-node.js */}
                  <meta property='og:title' content={ `${this.props.pageContext.title} | Your brand` } />
    
                  { /* Image from gatsby-node.js example, using gatsby-image for override */}
                  <meta property='og:image' content={ this.props.pageContext.images[0].localFile.childImageSharp.fluid.src } />
    
                  { /* Get path from location string */}
                  <meta property='og:url' content={ this.props.location.href } />
    
                </Helmet>
          </div>
        )
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 2021-02-28
      • 2012-10-01
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多