Netlify CMS and Cloudinary

June 8, 2019

With Netlify CMS bundled alongside Netlify; the service is able to directly converse with your GitHub repo, making it an incredibly powerful way to create and manage content. This includes managing dependencies, environment variables, posts, pages, media - basically anything you can throw at it.

With my normal go-to website framework of choice; Gatsby, building a production-version of my projects is pretty seamless. In a recent project however, I realized I was frequently going over Netlify’s 15 minute build-time limit; which caused deploy failures. Because this project had a variety of collection-types and contributors, the types of content (and media) were pretty diverse. After a bit of investigation, I determined the reason being over 100MB of media stored directly in the repo; which was being populated by Netlify CMS’s built-in file uploader. This meant:

  • Pulling down the repo was a pretty resource-intensive task for the Netlify service to do; wasting time.
  • Transforming each image through Gatsby; while decently fast, also added build-time.
  • Netlify’s default file system does not provide much validation or any optimization; so images were being uploaded at their full, often enormous, sizes.

With the help of gatsby-transformer-sharp we weren’t feeling the effects on the production-site, but it was clear this was an unsustainable way to manage media.

After bringing a bit more sanity to Netlify CMS configuration, it was clear I needed to look into other media providers that could work alongside Netlify CMS. Luckily, Cloudinary; a service I’ve used for several years, also has an integration with Netlify CMS. Cloudinary not only provides cloud storage and CDN delivery for your media, but also has a powerful, on-demand transformation service.

Setting up Cloudinary was very straight-forward:

1import CMS from 'netlify-cms-app';
2import cloudinary from 'netlify-cms-media-library-cloudinary';
7 config: {
8 load_config_file: false,
9 backend: {
10 name: 'github',
11 repo: 'your/repo',
12 branch: 'master',
13 squash_merges: true, // @NOTE Beta feature
14 },
15 media_library: {
16 name: 'cloudinary',
17 config: {
18 cloud_name: 'your_name',
19 api_key: 'YOUR_API_KEY_HERE',
20 default_transformations: [
21 [
22 {
23 width: 2000,
24 quality: 80,
25 crop: 'limit',
26 },
27 ],
28 ],
29 },
30 },
31 media_folder: '/static/uploads',
32 public_folder: '/uploads',
33 collections: [
34 ...
35 ]
36 },

I was also able to setup default transformations that would be applied to images chosen from the built-in interface within the Netlify CMS. To help with optimizations on upload, I also created an upload preset; which keeps my usage of the Cloudinary service as small as possible. This also meant that less training was needed for contributors about optimizing images before upload; as well as the service fitting perfectly within the Netlify CMS interface.

After moving the existing media over, I was able to decrease build-times from 15-20 minutes to about 4-5 minutes. This not only solves the issue of intermittent build failures, but also allows content contributors to preview and publish their work faster.

Copyright © 2020 Zach Schnackel. Penalty is 🔥