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
Fix Cloudinary image uploads via Netlify CMS #271
Conversation
Deploy preview for texasjusticeinitiative ready! Built with commit 9f02f98 https://deploy-preview-271--texasjusticeinitiative.netlify.com |
Automatically generated. Merged on Netlify CMS.
Automatically generated. Merged on Netlify CMS.
static/admin/config.yml
Outdated
@@ -3,7 +3,7 @@ backend: | |||
# Swap out "git-gateway" with "test-repo" and | |||
# Navigate to localhost:3333/static/admin/index.html to view Netlify CMS locally. | |||
name: "git-gateway" | |||
branch: master | |||
branch: fix-cloudinary-images |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will change this branch back to master
before I merge this PR, but I'm leaving it as-is right now so that we can check out the CMS in the preview deploy.
|
||
return ( | ||
<Image publicId={path.basename(url)} cloud_name="texas-justice-initiative" secure="true" alt={alt}> | ||
<Transformation width={maxWidth} crop="scale" aspect_ratio={aspectRatio} /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I called this prop maxWidth
to communicate that it doesn't actually control the width
attribute on the compiled <img>
tag. Instead, it's used to specify the width of the image we'd like Cloudinary to return, and we want that to be largest width at which we'll display the image.
Does that seem intuitive, or can you think of a better name or way to handle this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine. We could add a comment in to explain it, but I honestly think it's intuitive enough what we are trying to pass here.
const { url, alt, maxWidth, aspectRatio } = this.props; | ||
|
||
return ( | ||
<Image publicId={path.basename(url)} cloud_name="texas-justice-initiative" secure="true" alt={alt}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Node has this neat method for extracting the file name from a URL!
url: PropTypes.string.isRequired, | ||
alt: PropTypes.string.isRequired, | ||
maxWidth: PropTypes.number.isRequired, | ||
aspectRatio: PropTypes.number, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aspectRatio
is optional, and it's useful when we want to force the image to be a square like we do for the volunteer images.
7da4b56
to
6775640
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine to me @nholden. I didn't quite understand what the issue was with images not displaying correctly. When I reviewed the original PR they were showing up? Sounds like you and Eva may have run into an issue this week? I'll catch up on it today.
|
||
return ( | ||
<Image publicId={path.basename(url)} cloud_name="texas-justice-initiative" secure="true" alt={alt}> | ||
<Transformation width={maxWidth} crop="scale" aspect_ratio={aspectRatio} /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine. We could add a comment in to explain it, but I honestly think it's intuitive enough what we are trying to pass here.
Thanks for the review, @LayaTaal!
The images were displaying correctly because I had manually updated our What I should have done was updated the config via the CMS instead of doing it manually. It would have been more time consuming, but I would have learned that my assumption about FWIW, all of the URLs in the config in this PR were generated by the CMS. |
We're currently using the
cloudinary-react
library to include images from Cloudinary on the site. That library's<Image>
component expects a filename as apublicId
prop. If we pass it a full URL instead, Cloudinary won't perform our desired transformations and we end up downloading unoptimized images.So that we could pass just filenames to
<Image>
, I had originally set theoutput_filename_only: true
configuration for the Cloudinary media library so that Netlify CMS would store filenames instead of full URLs in our markdown. This ended up causing two issues:output_filename_only: true
.output_filename_only: true
wasn't even outputting filenames! It output non-existent local paths like this one.Moral of the story:
output_filename_only: true
is bad news. This PR removes that configuration. As a result, we store full Cloudinary URLs in our markdown, and it's our responsibility to grab the filenames from those URLs and pass them along to<Image>
.There was already some duplication in the way we inserted Cloudinary images, and this change would introduce more, so I extracted a new
CloudinaryImage
component as a wrapper around thecloudinary-react
functionality.