Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blocks.EMBEDDED_ASSET] not working for images #1

Closed
codeithuman opened this issue Feb 1, 2019 · 10 comments
Closed

[Blocks.EMBEDDED_ASSET] not working for images #1

codeithuman opened this issue Feb 1, 2019 · 10 comments

Comments

@codeithuman
Copy link

[Blocks.EMBEDDED_ASSET] not working for images.

I think it has to do with

const contentType = get(data, 'target.contentType');
.

My node structure does not match this pattern, but looks like the following. It is an image asset uploaded to a rich text field:

2019-01-31_16-40-36

My goal is to pull the id (contentful_id) out of data.target.sys and then use to query for that image and produce a fluid image for gatsby-image.

@codeithuman
Copy link
Author

I forked the project and found a fix that works for me. You can override [Blocks.EMBEDDED_ASSET] with a component now. If you don't override, no component or 'unknown' message is displayed. https://github.com/codeithuman/rich-text-to-jsx

connor-baer added a commit that referenced this issue Feb 6, 2019
BREAKING CHANGE: Overrides for custom elements are now indexed the other way round: nodeType ->
contentType/mimeType instead of contentType/mimeType -> nodeType

fix #1
connor-baer added a commit that referenced this issue Feb 6, 2019
BREAKING CHANGE: Overrides for custom elements are now indexed the other way round: nodeType ->
contentType/mimeType instead of contentType/mimeType -> nodeType

fix #1
@connor-baer
Copy link
Owner

connor-baer commented Feb 6, 2019

Hi @codeithuman! Thanks for opening an issue. I struggled a bit to find a good, flexible solution for embedded assets and entries. Assets are particularly tricky since they don't have a content type and the mime type seems overly specific (e.g. you probably want to use the same component for image/jpg and image/png).

I've finally found a compromise that I'm happy with: for assets, you can now define custom components by mime type group — that's the first part of the mime type, e.g. image. I've just released this change as 2.0.0 (it's a breaking change to the API).

Have a look at the updated docs and let me know what you think!

@codeithuman
Copy link
Author

Wonderful, thank you @connor-baer! I'll be able to take a look before the end of the week and let you know. Again, I appreciate your time on this.

@connor-baer
Copy link
Owner

I've also added default components for image, video, and audio assets. Published as 2.1.0.

@kylefraser
Copy link

kylefraser commented Feb 12, 2019

@connor-baer @codeithuman have either of you been able to test this and confirm that it's working?

I'm getting a:

Node type is invalid red line error.

It looks like there is no nodeType in the content array on the embedded-asset-block in my query and thus it's failing.

My graphQL query looks like this

    body {
      nodeType
      content {
        nodeType
        data {
          target {
            sys {
              id
            }
          }
        }
        content {     
          value
          nodeType
		  data {
            uri
          }
        }
      }
    }

And the results looks like this:

...
{
  "nodeType": "embedded-asset-block",
    "data": {
      "target": {
        "sys": {
          "id": "c5cTwlejTau9t8MRbUNEze"
         }
       }
    },
  "content": []
},
{
  "nodeType": "heading-2",
    "data": {
      "target": null
       },
   "content": [
     {
      "value": "Test header field",
      "nodeType": "text"
     }
   ]
},
...

I believe the error is stemming from that empty content array and that it's looking for a nodeType within that.

Any help would be greatly appreciated. Everything else is working fine. Thanks.

@codeithuman
Copy link
Author

codeithuman commented Feb 12, 2019 via email

@connor-baer
Copy link
Owner

connor-baer commented Feb 12, 2019

@kylefraser For assets, I use the mime type group which is extracted from the asset's mime type.

The mime type is not part of the content, but the target property, specifically at target.fields.file.contentType.

Could you please try with this query?

...
    body {
      nodeType
      content {
        nodeType
        data {
          target {
            sys {
              id
            }
            fields {
              file {
                contentType
              }
            }
          }
        }
        content {     
          value
          nodeType
		  data {
            uri
          }
        }
      }
    }
...

@kylefraser
Copy link

kylefraser commented Feb 12, 2019

@connor-baer hmm i'm unable to even query fields on target, it doesn't show up.

scratch that, I'm able to grab this.. still no image however.

looks like it's because of the extra locale i have in my query.

body {
      nodeType 
      content {        
        nodeType
        data {          
          target {            
            sys {
              id
            }
            fields {              
              file {                
                en_US {                  
                  fileName
                  contentType
				  url
                }
              }
            }
          }          
        }
        content {     
          value
          nodeType
          data {
            uri
          }
        }
      }
    }

@connor-baer
Copy link
Owner

connor-baer commented Feb 12, 2019

Yeah, rich-text-to-jsx requires a specific content structure: entry and asset links need to be resolved and the content localized. I'll add a note about this to the README.

If you're unable to adapt your content structure, I'd recommend looking at the official rich-text-react-renderer package. I collaborated on it with another developer after realizing the limitations of this approach.

@kylefraser
Copy link

Thank you all for the feedback. @connor-baer we were able to get everything working with the rich-text-react-renderer, thanks a bunch of pointing me in the right direction!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants