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

Fix Cloudinary image uploads via Netlify CMS #271

Merged
merged 7 commits into from Mar 15, 2020
Merged

Conversation

nholden
Copy link
Contributor

@nholden nholden commented Mar 14, 2020

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 a publicId 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 the output_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:

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 the cloudinary-react functionality.

@netlify
Copy link

netlify bot commented Mar 14, 2020

Deploy preview for texasjusticeinitiative ready!

Built with commit 9f02f98

https://deploy-preview-271--texasjusticeinitiative.netlify.com

@@ -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
Copy link
Contributor Author

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} />
Copy link
Contributor Author

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?

Copy link
Contributor

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}>
Copy link
Contributor Author

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,
Copy link
Contributor Author

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.

@nholden nholden requested a review from LayaTaal March 14, 2020 19:17
@nholden nholden self-assigned this Mar 14, 2020
@nholden nholden changed the title [WIP] Fix Cloudinary image uploads via Netlify CMS Fix Cloudinary image uploads via Netlify CMS Mar 14, 2020
@nholden nholden added this to In progress in Website Enhancements via automation Mar 14, 2020
Copy link
Contributor

@LayaTaal LayaTaal left a 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} />
Copy link
Contributor

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.

@nholden
Copy link
Contributor Author

nholden commented Mar 15, 2020

Thanks for the review, @LayaTaal!

I didn't quite understand what the issue was with images not displaying correctly.

The images were displaying correctly because I had manually updated our config.yml file to include just the filenames instead of the full URLs. When Eve uploaded a new image via the CMS, the resulting path in the config was /static/images/uploads/AnastasiaEfremova_1_f5z7rz.png. From the documentation, I expected that to be AnastasiaEfremova_1_f5z7rz.png.

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 output_filename_only was wrong. Lesson learned, and I'll be more careful with changes like these going forward!

FWIW, all of the URLs in the config in this PR were generated by the CMS.

@nholden nholden merged commit 88f8ba7 into master Mar 15, 2020
Website Enhancements automation moved this from In progress to Done Mar 15, 2020
@nholden nholden deleted the fix-cloudinary-images branch March 15, 2020 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants