gatsby image svg not found

后端 未结 4 1854
你的背包
你的背包 2021-02-14 07:16

When trying to load an SVG image this way:

export const query = graphql`
    query {
        fileName: file(relativePath: { eq: \"logo_large.svg\" }) {
                  


        
4条回答
  •  我在风中等你
    2021-02-14 07:45

    Don't know if this helps, but If you are looking for a generic and dynamic Image component that takes a custom file name and acts accordingly I've put together this:

    This approach considers that you have an images folder inside your src folder where you have all your images.

    const Image = ({ filename, alt }) => (
       {
    
          // Handles SVG extension
          const extension = filename.match(/[^\\]*\.(\w+)$/)[1]
          if (extension === "svg") {
            return {alt}/
          }
    
          // Finds your image among all
          const image = data.images.edges.find(n => {
            return n.node.relativePath.includes(filename)
          })
    
          if (!image) {
            return null
          }
          return (
            {alt}
          )
        }}
      />
    )
    

    Be careful that this is not optimized and queries for all of your images. If you are trying to optimize performance in every way possible this isn't the best way to go.

提交回复
热议问题