简体   繁体   English

Gatsby 不解析 GraphQL 图像地址

[英]Gatsby Not Resolving GraphQL Image Address

My project is based on the gatsby-creative starter.我的项目基于gatsby-creative starter。 I am trying to add an Open Graph image meta property.我正在尝试添加一个 Open Graph 图像元属性。 Unfortunately, the address of the image in the source folder is not being translated into a proper content path.不幸的是,源文件夹中的图像地址没有被转换为正确的内容路径。 Here is what I did...这是我所做的...

I started by adding a "url" and "image" properties to siteMetaData:我首先向 siteMetaData 添加了“url”和“image”属性:

  siteMetadata: {
    title: `A Web Site`,
    description: `Yes, this is a web site.`,
    author: `Brent Arias`,
    url: `https://website.com`,
    image: `/images/snape.jpg`
  },

So far I'm doing the same thing as the online documentation .到目前为止,我正在做与在线文档相同的事情。 However, those instructions have a comment which states the above "image" property contains the Path to your image you placed in the 'static' folder .但是,这些说明有一条注释,指出上述“图像”属性包含Path to your image you placed in the 'static' folderPath to your image you placed in the 'static' folder Huh?嗯? I don't believe I'm supposed to "place an image in the static folder"...I think that is Gatsby's job.我不相信我应该“在静态文件夹中放置一个图像”......我认为这是 Gatsby 的工作。 But moving along...但是一路走...

Starting from this original seo.js file , I added the same "url" and "image" properties to the graphQL query:从这个原始seo.js 文件开始,我向 graphQL 查询添加了相同的“url”和“image”属性:

  const { site } = useStaticQuery(
    graphql`
      query {
        site {
          siteMetadata {
            title
            description
            author
            url
            image
          }
        }
      }
    `
  )

Seemingly, so far so good.看起来,到目前为止一切顺利。 Finally, in the same seo.js file, I added an entry for the image itself:最后,在同一个 seo.js 文件中,我为图像本身添加了一个条目:

        {
          property: `og:type`,
          content: `website`,
        },
        {
          property: `og:url`,
          content: site.siteMetadata.url,
        },
        {
          property: `og:image`,
          content: `${site.siteMetadata.url}${site.siteMetadata.image}`
        },

When the page renders, the meta tag contains the useless address https://website.com/images/snape.jpg .当页面呈现时,元标记包含无用地址https://website.com/images/snape.jpg

Yes the online documentation is not identical to this approach, but it seems to be the equivalent.是的,在线文档与这种方法不同,但它似乎是等效的。 I don't see why the address isn't being resolved to the "static" folder where the image should be found.我不明白为什么地址没有被解析到应该找到图像的“静态”文件夹。

As a work-around hack, instead I added to the seo.js file the following:作为一种解决方法,我在 seo.js 文件中添加了以下内容:

import socialBanner from '../images/snape.jpg'

When I place that socialBanner into the "content" of that same "og:image" property, then it works as expected and resolves to: https://website.com/static/snape-119744a51329473845d08af3df4b5290.jpg .当我将该socialBanner放入同一个“og:image”属性的“内容”中时,它会按预期工作并解析为: https://website.com/static/snape-119744a51329473845d08af3df4b5290.jpg

I don't want that hack.我不想要那个黑客。 What do I need to change to properly make use of Gatsby tools (StaticQuery, Helmet, etc) to handle this image and address?我需要更改什么才能正确使用 Gatsby 工具(StaticQuery、Helmet 等)来处理此图像和地址?

The static folder being referenced by the Gatsby documentation is at the root of your project. Gatsby 文档引用的static文件夹位于项目的根目录下。 Here's the documentation for the static folder and it's behavior in Gatsby .这是 static 文件夹的文档及其在 Gatsby 中的行为

If you want to use the approach shown in the SEO example documentation, you'll need to add your OpenGraph image to this folder, not to public/static , which is a folder that Gatsby uses for files that it generates.如果您想使用 SEO 示例文档中显示的方法,您需要将 OpenGraph 图像添加到此文件夹,而不是public/static ,这是 Gatsby 用于生成文件的文件夹。

Adding assets outside of the module system在模块系统之外添加资产

You can create a folder named static at the root of your project.您可以在项目的根目录下创建一个名为static的文件夹。 Every file you put into that folder will be copied into the public folder.您放入该文件夹中的每个文件都将复制到公用文件夹中。 Eg if you add a file named sun.jpg to the static folder, it'll be copied to public/sun.jpg例如,如果您将名为sun.jpg的文件添加到静态文件夹,它将被复制到public/sun.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM