Optimize and serve all images served in your Netlify site deploy with Cloudinary.
This plugin is not officially supported by Cloudinary.
-
Sign up for a free Cloudinary account.
-
Install the plugin:
npm install netlify-plugin-cloudinary # or yarn add netlify-plugin-cloudinary
- Add the plugin to your Netlify config:
[[plugins]] package = "netlify-plugin-cloudinary"
- Configure your Cloudinary Cloud Name:
[[plugins]] package = "netlify-plugin-cloudinary" [plugins.inputs] cloudName = "<Your Cloud Name>"
By default, your images will served via the fetch delivery type.
Name | Required | Example | Description |
---|---|---|---|
cloudName | No* | mycloud | Cloudinary Cloud Name |
deliveryType | No | fetch | The method in which Cloudinary stores and delivers images (Ex: fetch, upload) |
imagePath | No | /assets | Local path application serves image assets from |
folder | No | myfolder | Folder all media will be stored in. Defaults to Netlify site name |
uploadPreset | No | my-preset | Defined set of asset upload defaults in Cloudinary |
*Cloud Name must be set as an environment variable if not as an input
Name | Required | Example | Description |
---|---|---|---|
CLOUDINARY_CLOUD_NAME | No* | mycloud | Cloudinary Cloud Name |
CLOUDINARY_API_KEY | No | 1234 | Cloudinary API Key |
CLOUDINARY_API_SECRET | No | abcd1234 | Cloudinary API Secret |
*Cloud Name must be set as an input variable if not as an environment variable
You have two options for setting your Cloud Name: plugin input or environment variable.
Input
Inside your Netlify config:
[plugins.inputs] cloudName = "<Your Cloud Name>"
Environment Variable
Inside your environment variable configuration:
CLOUDINARY_CLOUD_NAME="<Your Cloud Name>"
Learn how to set environment variables with Netlify.
fetch
Default - no additional configuration needed.
The fetch method allows you to use Cloudinary delivery by providing a remote URL. Learn more about using delivering remote images with fetch.
upload - Unsigned
Unsigned uploads require an additional Upload Preset set up and configured in your Cloudinary account.
Inside your Netlify config:
[[plugins]] package = "netlify-plugin-cloudinary" [plugins.inputs] cloudName = "[Your Cloudinary Cloud Name]" deliveryType = "upload" uploadPreset = "[Your Cloudinary Upload Preset]"
Uploading media to Cloudinary gives you more flexibility with your media upon delivery. Learn more about unsigned uploads.
upload - Signed
Signed uploads require you to set your API Key and API Secret as environment variables.
Inside your Netlify config:
[[plugins]] package = "netlify-plugin-cloudinary" [plugins.inputs] cloudName = "[Your Cloudinary Cloud Name]" deliveryType = "upload"
Inside your environment variable configuration:
CLOUDINARY_API_KEY="[Your Cloudinary API Key]" CLOUDINARY_API_SECRET="[Your Cloudinary API Secret]"
Uploading media to Cloudinary gives you more flexibility with your media upon delivery. Learn more about signed uploads.
By default, the plugin will attempt to serve any thing served from /images as Cloudinary paths. This can be customized by passing in the imagesPath
input.
Inside your Netlify config:
[[plugins]] package = "netlify-plugin-cloudinary" [plugins.inputs] cloudName = "[Your Cloudinary Cloud Name]" imagesPath = "/my-path"
During the Netlify build process, the plugin is able to tap into the onPostBuild
hook where we use jsdom to create a node-based representation of the DOM for each output HTML file, then walk through each node, and if it's an image, we replace the source with a Cloudinary URL.
Depending on the configuration, we'll either use the full URL for that image with the Cloudinary fetch API or alternatively that image will be uploaded, where then it will be served by public ID from the Cloudinary account.
While this works great for a lot of cases and in particular the first load of that page, using a framework with clientside routing or features that mutate the DOM may prevent that Cloudinary URL from persisting, making all of that hard work disappear, meaning it will be served from the Netlify CDN or original remote source (which is fine, but that leads us to Part 2).
To provide comprehensive coverage of images being served from Cloudinary, we take advantage of Netlify's dynamic redirects and serverless functions to map any image being served from the /images directory (or the configured imagesPath
), redirect it to a serverless function, which then gets redirected to a Cloudinary URL.
Through this process, we're still able to afford the same option of using either the fetch or upload API depending on preference, where the latter would be uploaded if it's a new asset within the serverless function.
To actively develop with the plugin, you need to be able to run a Netlify build and deploy.
You can do this by pushing a site that uses this plugin to Netlify or you can use the Netlify CLI locally (recommended).
You can reference the plugin locally in your netlify.toml
by specifying the directory the plugin is in relative to your project, for example:
[[plugins]] package = "../netlify-plugin-cloudinary"
On the locally linked Netlify project, you can then run:
netlify deploy --build
Which will combine the build and deploy contexts and run through the full process, generating a preview URL.
- The Netlify CLI doesn't support all input and environment variables for build plugins, primarily
process.env.DEPLOY_PRIME_URL
meaning theonPostBuild
image replacement will not occur locally.
The repository additionally includes a demo which uses the plugin. The demo is a simple Next.js application that lows a few images statically and those same images in a separate list once the page loads. This helps us test both the on-page image replacement and the redirecting of the images directory.
You can link this project to your Netlify account for testing purposes by creating a new Netlify site in the root of this project and linking it to that new site.
Once linked, you can then run the build and deploy process with:
netlify deploy --build
Tests require all environment variables to be actively set in order to pass. See configuration above to see which variables need to be set.
Once set, tests can be ran with:
yarn test