Skip to content

Commit e9e0c09

Browse files
authored
docs: gatsby-plugin-image + gatsby-transformer-cloudinary (#34)
Closes #27 Closes #29
1 parent 19a2115 commit e9e0c09

File tree

1 file changed

+71
-32
lines changed

1 file changed

+71
-32
lines changed

README.md

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Gatsby-Source-Cloudinary
22

3-
This source plugin queries media files from a Cloudinary account into `cloudinaryMedia` nodes in your Gatsby project.
3+
This source plugin queries media files from a Cloudinary account into `CloudinaryMedia` nodes in your Gatsby project.
44

55
[See a live demo here](https://gsc-sample.netlify.com/)
66

77
[Here's a tutorial on plugin usage](https://scotch.io/tutorials/handling-images-in-gatsby-with-high-performance)
88

9+
If support for the [gatsby-plugin-image](https://www.gatsbyjs.com/plugins/gatsby-plugin-image/) is needed add and configure the [gatsby-transformer-cloudinary](https://www.gatsbyjs.com/plugins/gatsby-transformer-cloudinary/) plugin.
10+
911
## Motivation
1012

11-
Gatsby offers the ability to develop high performance web pages with a rich developer experience and declarative data fetching Layer with GraphQL.
12-
Cloudinary provides a robust solution to manage media assets, from storage, optimized delivery, to media transformations. Extending the powers of Gatsby with the use of gatsby-source-cloudinary affords the best of both worlds, to allow users store media assets on Cloudinary,
13+
Gatsby offers the ability to develop high-performance web pages with a rich developer experience and declarative data fetching Layer with GraphQL.
14+
Cloudinary provides a robust solution to manage media assets, from storage, and optimized delivery, to media transformations. Extending the powers of Gatsby with the use of gatsby-source-cloudinary affords the best of both worlds, to allow users to store media assets on Cloudinary,
1315
leveraging Cloudinary's powerful optimization and transformation capabilities in fast sites built with Gatsby.
1416

1517
While Cloudinary images with on-the-fly transformations can be used during runtime in Gatsby, gatsby-source-cloudinary utilizes the build optimizations of Gatsby.
@@ -20,55 +22,82 @@ While Cloudinary images with on-the-fly transformations can be used during runti
2022
- Average of over 60% image optimizations using `f_auto` and `q_auto` applied by default.
2123
- Query Cloudinary images in Gatsby's data layer using GraphQL.
2224
- Utilize Cloudinary's robust transformation suite in media files on a Gatsby site.
23-
- Manage media assets of an application completely on Cloudinary rather than directly in the codebase.
25+
- Manage media assets of an application entirely on Cloudinary rather than directly in the codebase.
2426

25-
Looking to use the features of Gatsby-Image with Cloudinary optimized storage, transformations and delivery? Checkout the [gatsby-transformer-cloudinary](https://www.npmjs.com/package/gatsby-transformer-cloudinary) plugin.
27+
Looking to use the features of Gatsby-Image with Cloudinary optimized storage, transformations, and delivery? Check out the [gatsby-transformer-cloudinary](https://www.npmjs.com/package/gatsby-transformer-cloudinary) plugin.
2628

2729
## Example usage
2830

29-
Here's a sample usage of the source plugin to create an image gallery from images stored on Cloudinary:
31+
Example showing use with and without [gatsby-plugin-image](https://www.gatsbyjs.com/plugins/gatsby-plugin-image/) + [ [gatsby-transformer-cloudinary](https://www.gatsbyjs.com/plugins/gatsby-transformer-cloudinary/). The latter will add the `gatsbyImageData` resolver used below.
3032

31-
```jsx harmony
33+
```js
3234
import React from 'react';
3335
import { useStaticQuery, graphql } from 'gatsby';
36+
import { GatsbyImage, getImage } from 'gatsby-plugin-image';
3437

3538
const SingleImage = () => {
3639
const data = useStaticQuery(graphql`
3740
query CloudinaryImage {
38-
cloudinaryMedia(public_id: { eq: "gatsby-source-cloudinary/11" }) {
41+
cloudinaryMedia {
3942
secure_url
43+
gatsbyImageData(
44+
width: 300
45+
aspectRatio: 1
46+
transformations: ["e_grayscale", "c_fill"]
47+
)
4048
}
4149
}
4250
`);
43-
const clImage = data.cloudinaryMedia.secure_url;
51+
52+
const imageSrc = data.cloudinaryMedia.secure_url;
53+
const image = getImage(data.cloudinaryMedia);
4454

4555
return (
46-
<div>
47-
<div>
48-
<img src={clImage} alt={'no alt :('} />
49-
</div>
50-
</div>
56+
<>
57+
<GatsbyImage image={image} alt="no alt :(" />
58+
<img width="300" src={imageSrc} alt={'no alt :('} />
59+
</>
5160
);
5261
};
62+
63+
export default SingleImage;
5364
```
5465

5566
## Installation
5667

57-
Install the source plugin using either `npm` or `yarm`:
68+
Install the source plugin using either `npm` or `yarn`:
5869

5970
```bash
6071
npm install --save gatsby-source-cloudinary
6172
```
6273

74+
```bash
75+
yarn add --save gatsby-source-cloudinary
76+
```
77+
78+
### Gatsby Plugin Image
79+
80+
To use with [gatsby-plugin-image](https://www.gatsbyjs.com/plugins/gatsby-plugin-image/) you'll need to install it along with [gatsby-transformer-cloudinary](https://www.gatsbyjs.com/plugins/gatsby-transformer-cloudinary/).
81+
82+
> **NOTE:** Currently in beta, may be used with both Gatsby v3 and Gatsby v4
83+
84+
```bash
85+
npm install --save gatsby-transformer-cloudinary@beta-v4 gatsby-plugin-image
86+
```
87+
88+
```bash
89+
yarn add --save gatsby-transformer-cloudinary@beta-v4 gatsby-plugin-image
90+
```
91+
6392
### Cloudinary Credentials
6493

6594
Cloudinary offers a generous free tier which is more than enough to bootstrap projects.
66-
Obtain your cloudname, key and secret from your cloudinary console when you signup at [Cloudinary.com](https://cloudinary.com).
95+
Obtain your cloudname, key, and secret from your cloudinary console when you signup at [Cloudinary.com](https://cloudinary.com).
6796

6897
### Environment configuration
6998

7099
Store your `cloudName`, `apiKey` and `apiSecret` as environment variables for security.
71-
To do this, create a file in the root of the project named `.env`. Add your environment variables in it with:
100+
To do this, create a file in the project's root named `.env`. Add your environment variables in it with:
72101

73102
```
74103
CLOUDINARY_API_KEY=INSERT API KEY HERE
@@ -88,7 +117,7 @@ In your `gatsby-config.js` file, require and configure `dotenv` with:
88117
require('dotenv').config();
89118
```
90119

91-
There are several options to configuring `dotenv` to use different env files either in development or production. You can find that [here](https://www.npmjs.com/package/dotenv).
120+
There are several options to configure `dotenv` to use different env files either in development or production. You can find that [here](https://www.npmjs.com/package/dotenv).
92121

93122
Add the `.env` file to `.gitignore` so it's not committed.
94123

@@ -99,17 +128,27 @@ Ensure to configure the environment variables on deployment as well.
99128
In your `gatsby-config.js` file, include the plugin like this:
100129

101130
```js
102-
{
103-
resolve:`gatsby-source-cloudinary`,
104-
options: {
105-
cloudName: process.env.CLOUDINARY_CLOUD_NAME,
106-
apiKey: process.env.CLOUDINARY_API_KEY,
107-
apiSecret: process.env.CLOUDINARY_API_SECRET,
108-
resourceType: `image`,
109-
type: `type Value`,
110-
prefix: `abc-xyz/`
111-
}
112-
}
131+
module.exports = {
132+
plugins: [
133+
{
134+
resolve: `gatsby-source-cloudinary`,
135+
options: {
136+
cloudName: process.env.CLOUDINARY_CLOUD_NAME,
137+
apiKey: process.env.CLOUDINARY_API_KEY,
138+
apiSecret: process.env.CLOUDINARY_API_SECRET,
139+
resourceType: `image`,
140+
maxResults: 22,
141+
},
142+
},
143+
{
144+
resolve: `gatsby-transformer-cloudinary`,
145+
options: {
146+
transformTypes: [`CloudinaryMedia`],
147+
},
148+
},
149+
`gatsby-plugin-image`,
150+
],
151+
};
113152
```
114153

115154
### Plugin options
@@ -121,7 +160,7 @@ You can find the plugin options in the table below.
121160
| `cloudName` | string | true | n/a | Cloud name of your Cloudinary account, can be obtained from your [Cloudinary console](https://cloudinary.com/console/). This should be stored and retrieved as an environment variable. |
122161
| `apiKey` | string | true | na/a | API Key of your Cloudinary account, can be obtained from your [Cloudinary console](https://cloudinary.com/console/). This should be stored and retrieved as an environment variable. |
123162
| `apiSecret` | string | true | n/a | API Secret of your Cloudinary account, can be obtained from your [Cloudinary console](https://cloudinary.com/console/). This should be stored and retrieved as an environment variable. |
124-
| `resourceType` | string | false | image | This is the type of file. Possible values: image, raw, video. Note: Use the video resource type for all video resources as well as for audio files, such as .mp3. |
163+
| `resourceType` | string | false | image | This is the file type. Possible values: image, raw, video. Note: Use the video resource type for all video resources as well as for audio files, such as .mp3. |
125164
| `type` | string | false | all | This is the storage type: upload, private, authenticated, facebook, twitter, gplus, instagram_name, gravatar, youtube, hulu, vimeo, animoto, worldstarhiphop or dailymotion. |
126165
| `maxResults` | integer | false | 10 | Max number of resources to return |
127166
| `tags` | boolean | false | false | If true, include the list of tag names assigned to each resource |
@@ -134,7 +173,7 @@ An example `prefix` value is `gatsby-anime-videos/`. This will fetch only media
134173

135174
The `f_auto` and `q_auto` Cloudinary transformations are applied automatically to all media queries. This optimizes the delivered media quality and format.
136175

137-
> All media files sourced from Cloudinary are done when Gatsby creates an optimized build, hence you will need to trigger a new production build whenever new media files are added directly on Cloudinary.
176+
> All media files sourced from Cloudinary are done when Gatsby creates an optimized build; hence you will need to trigger a new production build whenever new media files are added directly on Cloudinary.
138177
139178
## How to use
140179

@@ -181,7 +220,7 @@ const Images = () => {
181220

182221
## Contribute
183222

184-
Want to contribute to make this tool even better? Feel free to send in issues and pull requests on feature requests, fixes, bugs, typos, performance lapses or any other challenge faced with using this tool.
223+
Want to contribute to making this tool even better? Feel free to send in issues and pull requests on feature requests, fixes, bugs, typos, performance lapses, or any other challenge faced with using this tool.
185224

186225
## License
187226

0 commit comments

Comments
 (0)