簡體   English   中英

如何使用 Gatsby 以編程方式在反應頭盔中顯示圖像?

[英]How to show image in react helmet programmatically with Gatsby?

我正在將 Gatbsy 用於我的首頁,並且我正在嘗試使用 react-helmet 以編程方式添加一些元數據。

我有一個 SEO 組件,如下所示:

const SEO = ({title, description, location, image_url}) => {
    const capitalize = string => {
        if (string !== undefined){
            return string.charAt(0).toUpperCase() + string.slice(1);
        }

        return string

    };

    const MAIN_TITLE = settings.META_TITLE;
    return (
        <Helmet>
            <title>{`${MAIN_TITLE} | ${capitalize(title)}`}</title>
            <meta name="description" content={description}/>
            <meta property="og:title" content={`${MAIN_TITLE} | ${capitalize(title)}`}/>
            <meta property="og:description" content={description}/>
            <meta property="og:image" itemProp="image" content={image_url !== undefined ? image_url : `${settings.ROOT_URL}/static/image-cover-1e01617fb26211f1cd8e3dd24977070d.jpg`}/>
            <meta property="og:type" content="website"/>
            <meta property="og:url" content={location.href}/>
            <meta property="twitter:title" content={`${MAIN_TITLE} | ${capitalize(title)}`}/>
            <meta property="twitter:description" content={description}/>
            <meta property="twitter:image" itemProp="image"  content={image_url !== undefined ? image_url : `${settings.ROOT_URL}/static/image-cover-1e01617fb26211f1cd8e3dd24977070d.jpg`}/>
            <meta property="twitter:card" content="summary_large_image"/>
        </Helmet>
    )
};

export default SEO

我在 Gatsby 的其中一頁中使用它,如下所示:

<div className="flex flex-col overflow-x-hidden min-h-screen">
     {loading && !hasError && <LoadingIndicator/>}
     {!loading && hasError && <div className="container my-12"><ErrorIndicator/></div>}
     {!loading && !hasError && <React.Fragment>
             <SEO title={`${vehicle.make} ${vehicle.model} ${vehicle.type}`}
                  description={`${vehicle.make} ${vehicle.model} wacht op u.`}
                  location={location}
                  image_url={vehicle.images instanceof Array ? vehicle.images[0].uri : vehicle.images.uri}/>
              .... // Rest of the code
      <React.Fragment>}
</div>

當我在本地主機或服務器上的標簽中簽入控制台時,我看到了我應該看到的內容:

在此處輸入圖片說明

但在 Facebook 上,我看到了別的東西。 對於圖像,例如我看到:

og:image    http://my-site/static/image-cover-1e01617fb26211f1cd8e3dd24977070d.jpg

Facebook 在<meta property="og:image" ..../>標簽中使用else語句,但image_url不是未定義的,在控制台中我看到了正確的 url。

任何的想法?

OG 文檔沒有將 item prop 顯示為屬性,但我認為不是這樣。

https://ogp.me/

我認為您的代碼以編程方式添加了元數據,因此在第一次加載時它不存在並且 Facebook 的機器人沒有捕捉到它? 嘗試將該元標記硬編碼到 HTML 中,看看 Facebook 是否接受,這至少會讓您知道這是否是問題所在。

編輯:抱歉,這並沒有真正回答根本問題。 如果上述結果是問題,我建議盡可能在服務器端以編程方式添加它。 你可以嘗試用 EJS 之類的東西來模板化它。 從本質上講,我認為您只需要確保它在 DOM 中足夠早地出現在 Facebook 的機器人中。

編輯:Facebook 根本不支持(不再)自定義共享。 它是 OG 或什么都沒有,他們在您的 JS 有機會更改它之前從您的原始 html 中獲取 OG。 它只是不適合 SPA。 您可以為 SPA 使用 Google OG,有些人已經提出了黑客代理,但它們對您的 SEO 不利。

對不起 :/

gatsby-react-helmet 生成空<title>在 SSR 上&lt;/div&gt;</title><div id="text_translate"><p> 我的 Gatsby 網站沒有在 SSR 上生成正確的標題標簽。 當我建立網站時,我在生成的文件上得到的只是&lt;title data-react-helmet="true"&gt;&lt;/title&gt; 。 我很感激幫助找到問題並解決,因為經過長時間的調試過程,我不知道哪個可能是問題。</p><p> 相關文件:</p><p> <strong>package.json</strong></p><pre> ... "dependencies": { "gatsby": "^2.19.45", "gatsby-image": "^2.2.44", "gatsby-plugin-manifest": "^2.2.48", "gatsby-plugin-react-helmet": "^3.2.2", "gatsby-plugin-sharp": "^2.4.13", "gatsby-plugin-sitemap": "^2.3.1", "gatsby-plugin-typescript": "^2.3.3", "gatsby-source-filesystem": "^2.1.56", "gatsby-source-graphql": "^2.2.0", "react": "^16.12.0", "react-dom": "^16.12.0", "react-helmet": "^6.0.0", }...</pre><p> <strong>gatsby.config.js</strong></p><pre> plugins: [ `gatsby-plugin-react-helmet`, ... ]</pre><p> <em>(gatsby-plugin-offline 已禁用)</em></p><p> 搜索引擎優化</p><pre>import React from "react" import { Helmet } from "react-helmet" import { useStaticQuery, graphql } from "gatsby" interface Props { title: string description?: string image?: string } const SEO = ({ title, description, image }: Props) =&gt; { const { site } = useStaticQuery( graphql` query { site { siteMetadata { title description image siteUrl } } } ` ) const metaDescription = description || site.siteMetadata.description const shareImage = image || site.siteMetadata.image const url = site.siteMetadata.siteUrl return ( &lt;Helmet defer={false}&gt; &lt;title&gt;{title}&lt;/title&gt; &lt;meta name="description" content={metaDescription} /&gt; &lt;meta name="image" content={shareImage} /&gt; &lt;link rel="canonical" href={url} /&gt; &lt;meta property="og:url" content={url} /&gt; &lt;meta property="og:type" content="website" /&gt; &lt;meta property="og:title" content={title} /&gt; &lt;meta property="og:description" content={metaDescription} /&gt; &lt;meta property="og:image" content={shareImage} /&gt; &lt;meta name="twitter:card" content="summary_large_image" /&gt; &lt;meta name="twitter:title" content={title} /&gt; &lt;meta name="twitter:description" content={metaDescription} /&gt; &lt;meta name="twitter:image" content={shareImage} /&gt; &lt;/Helmet&gt; ) } export default SEO</pre><p> 將&lt;title&gt;{title}&lt;/title&gt;更改為&lt;Helmet title={title}&gt;或刪除defer={true}不會改變結果中的任何內容。</p><p> <strong>蓋茨比-ssr.js</strong></p><pre> import React from "react" import { Helmet } from "react-helmet" export const onRenderBody = ( { setHeadComponents, setHtmlAttributes, setBodyAttributes }, pluginOptions ) =&gt; { const helmet = Helmet.renderStatic() setHtmlAttributes(helmet.htmlAttributes.toComponent()) setBodyAttributes(helmet.bodyAttributes.toComponent()) setHeadComponents([ helmet.title.toComponent(), helmet.link.toComponent(), helmet.meta.toComponent(), helmet.noscript.toComponent(), helmet.script.toComponent(), helmet.style.toComponent() ]) }</pre><p> <em>我仍然有一個空的 srr 文件的問題。</em></p><p> 在任何給定的頁面上,我都會調用 SEO 標簽,例如:</p><pre> &lt;SEO title="Hello World" description="Foo Bar" /&gt;</pre></div>

[英]gatsby-react-helmet generating empty <title> on SSR

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何使用反應頭盔在 gatsby 的自定義 404 頁面中添加元標記? 在 React Helmet/ Gatsby 中插入自定義腳本 如何在反應頭盔中預加載最大內容繪制圖像 Gatsby 在 react-helmet 中嵌入帶有腳本標簽的表單 gatsby-react-helmet 生成空<title>在 SSR 上&lt;/div&gt;</title><div id="text_translate"><p> 我的 Gatsby 網站沒有在 SSR 上生成正確的標題標簽。 當我建立網站時,我在生成的文件上得到的只是&lt;title data-react-helmet="true"&gt;&lt;/title&gt; 。 我很感激幫助找到問題並解決,因為經過長時間的調試過程,我不知道哪個可能是問題。</p><p> 相關文件:</p><p> <strong>package.json</strong></p><pre> ... "dependencies": { "gatsby": "^2.19.45", "gatsby-image": "^2.2.44", "gatsby-plugin-manifest": "^2.2.48", "gatsby-plugin-react-helmet": "^3.2.2", "gatsby-plugin-sharp": "^2.4.13", "gatsby-plugin-sitemap": "^2.3.1", "gatsby-plugin-typescript": "^2.3.3", "gatsby-source-filesystem": "^2.1.56", "gatsby-source-graphql": "^2.2.0", "react": "^16.12.0", "react-dom": "^16.12.0", "react-helmet": "^6.0.0", }...</pre><p> <strong>gatsby.config.js</strong></p><pre> plugins: [ `gatsby-plugin-react-helmet`, ... ]</pre><p> <em>(gatsby-plugin-offline 已禁用)</em></p><p> 搜索引擎優化</p><pre>import React from "react" import { Helmet } from "react-helmet" import { useStaticQuery, graphql } from "gatsby" interface Props { title: string description?: string image?: string } const SEO = ({ title, description, image }: Props) =&gt; { const { site } = useStaticQuery( graphql` query { site { siteMetadata { title description image siteUrl } } } ` ) const metaDescription = description || site.siteMetadata.description const shareImage = image || site.siteMetadata.image const url = site.siteMetadata.siteUrl return ( &lt;Helmet defer={false}&gt; &lt;title&gt;{title}&lt;/title&gt; &lt;meta name="description" content={metaDescription} /&gt; &lt;meta name="image" content={shareImage} /&gt; &lt;link rel="canonical" href={url} /&gt; &lt;meta property="og:url" content={url} /&gt; &lt;meta property="og:type" content="website" /&gt; &lt;meta property="og:title" content={title} /&gt; &lt;meta property="og:description" content={metaDescription} /&gt; &lt;meta property="og:image" content={shareImage} /&gt; &lt;meta name="twitter:card" content="summary_large_image" /&gt; &lt;meta name="twitter:title" content={title} /&gt; &lt;meta name="twitter:description" content={metaDescription} /&gt; &lt;meta name="twitter:image" content={shareImage} /&gt; &lt;/Helmet&gt; ) } export default SEO</pre><p> 將&lt;title&gt;{title}&lt;/title&gt;更改為&lt;Helmet title={title}&gt;或刪除defer={true}不會改變結果中的任何內容。</p><p> <strong>蓋茨比-ssr.js</strong></p><pre> import React from "react" import { Helmet } from "react-helmet" export const onRenderBody = ( { setHeadComponents, setHtmlAttributes, setBodyAttributes }, pluginOptions ) =&gt; { const helmet = Helmet.renderStatic() setHtmlAttributes(helmet.htmlAttributes.toComponent()) setBodyAttributes(helmet.bodyAttributes.toComponent()) setHeadComponents([ helmet.title.toComponent(), helmet.link.toComponent(), helmet.meta.toComponent(), helmet.noscript.toComponent(), helmet.script.toComponent(), helmet.style.toComponent() ]) }</pre><p> <em>我仍然有一個空的 srr 文件的問題。</em></p><p> 在任何給定的頁面上,我都會調用 SEO 標簽,例如:</p><pre> &lt;SEO title="Hello World" description="Foo Bar" /&gt;</pre></div> 錯誤:無法找到插件“gatsby-plugin-React-helmet” 蓋茨比反應頭盔不是服務器端渲染 包含 gatsby 頭盔腳本 如何在 Gatsby/React 中創建 360 全景圖像? Gatsby/React 項目不會重新識別 ssr 腳本標簽 cloudinary 變量也嘗試使用 Helmet
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM