簡體   English   中英

包含 gatsby 頭盔腳本

[英]Include gatsby helmet script

我不能在頭盔蓋茨比中包含腳本。 我要包含的腳本是這樣的:

  <script src = "assets / vendor / jquery / jquery.min.js"> </script>

我試過這樣做,控制台給了我一個錯誤:

 import Helmet from "react-helmet";
    import {withPrefix} from "gatsby";
    
    <Helmet>
    <script src = {withPrefix ('masonry.js')} />
    </Helmet>

使用這種語法我也嘗試過,它給了我一個錯誤:

<Helmet>
  <script src = "http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js" type = "text / javascript" />
</Helmet>

將如何解決?

你在那里混合了很多東西。 如果您的資產是內部資產,例如您在第一個和第二個片段中的資產,請將其添加到您的/static文件夾中並像這樣導入它:

import { withPrefix } from "gatsby"
// ...

<Helmet>
    <script src={withPrefix('jquery.min.js')} type="text/javascript" />
</Helmet>

請注意代碼中的空格,您應該跟蹤它們。

如果您的資產是外部第三方資產,您也可以在<Helmet>中導入它們,但不使用withPrefix內部 function。 只是:

<Helmet>
  <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js" type = "text / javascript" />
</Helmet>

像 React 中的任何其他組件一樣,您的<Helmet>標記必須用一些東西包裹以避免解析問題,請始終使用它:

const IndexPage = () => (
  <Layout>
    <Helmet>
      <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js" type="text/javascript" />
    </Helmet>
  </Layout>
)

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.

相關問題 在 React Helmet/ Gatsby 中插入自定義腳本 使用頭盔向 Gatsby 添加腳本標簽會引發語法錯誤 Gatsby 在 react-helmet 中嵌入帶有腳本標簽的表單 Gatsby 網站,使用頭盔請求第一頁時腳本不可用 Gatsby/React 項目不會重新識別 ssr 腳本標簽 cloudinary 變量也嘗試使用 Helmet 在每個 gatsby 頁面中包含 stripe.js 腳本的最佳方法 錯誤:無法找到插件“gatsby-plugin-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> 如何在 Helmet 標簽 gastby 中加載腳本文件 將 react-helmet 添加到我的 Gatsby 項目中會導致錯誤:元素類型無效
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM