Skip to content

Commit

Permalink
reverting and providing both url and secure_url to node
Browse files Browse the repository at this point in the history
  • Loading branch information
colbyfayock committed Oct 9, 2023
1 parent ef35b43 commit 40f8896
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export const query = graphql`
query {
allCloudinaryMedia {
nodes {
url
secure_url
# url - if using secure: false
}
}
}
Expand Down Expand Up @@ -259,6 +260,8 @@ When `true`, includes the context data assigned to each resource. Helpful in ret

Force HTTPS URLs for asset delivery even if they are embedded in non-secure HTTP pages. In most cases, it's recommended to keep this parameter as `true`.

When using `true`, URLs will be returned using `secure_url` following Cloudinary's resource response, otherwise, will be returned as `url`.

**Type:** Boolean\
**Default:** true

Expand Down Expand Up @@ -286,7 +289,7 @@ Set this parameter to true if you are an Advanced plan user with a private CDN d
## ⚠️ Gotchas

- Gatsby pulls the data from Cloudinary when it builds; you need to trigger a rebuild whenever new media files are added to the Cloudinary account.
- `f_auto` and `q_auto` Cloudinary transformations are applied automatically to the `url` value optimizing the delivered media quality and format.
- `f_auto` and `q_auto` Cloudinary transformations are applied automatically to the `secure_url` or `url` value optimizing the delivered media quality and format.

 

Expand Down
4 changes: 2 additions & 2 deletions demo/src/pages/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function BasicPage({ data }) {
key={index}
alt={media.context?.custom?.alt}
width="200px"
src={media.url}
src={media.secure_url}
/>
))}
</main>
Expand All @@ -20,7 +20,7 @@ export const query = graphql`
query {
allCloudinaryMedia {
nodes {
url
secure_url
context {
custom {
alt
Expand Down
6 changes: 3 additions & 3 deletions demo/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function IndexPage({ data }) {
</tr>

{data.allCloudinaryMedia.nodes.map((media) => {
const { url, context } = media;
const { secure_url, context } = media;
const example1Image = getImage(media.example1ImageData);
const example2Image = getImage(media.example2ImageData);
const example3Image = getImage(media.example3ImageData);
Expand All @@ -49,7 +49,7 @@ export default function IndexPage({ data }) {
<img
width="300"
style={{ maxWidth: '100%', display: 'block' }}
src={url}
src={secure_url}
alt={alt}
/>
</td>
Expand Down Expand Up @@ -86,7 +86,7 @@ export const query = graphql`
}
allCloudinaryMedia {
nodes {
url
secure_url
gatsbyImageData(width: 300, placeholder: BLURRED)
example1ImageData: gatsbyImageData(
width: 300
Expand Down
21 changes: 16 additions & 5 deletions plugin/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,27 @@ const getNodeData = (
gatsbyUtils,
media,
cloudName,
secure,
cname,
secureDistribution,
privateCdn,
) => {
const { createNodeId, createContentDigest } = gatsbyUtils;

// When Cloudinary returns a resource via the API, they return both
// a secure_url and a url, with the url including `http://`. We want
// to extend this option to what we return so that the developer
// can have the option of using the URL they prefer for their
// specific use case along with any transformations

const url = generateCloudinaryUrl(media, {
secure: secure,
secure: false,
cname: cname,
secure_distribution: secureDistribution,
private_cdn: privateCdn,
});

const secureUrl = generateCloudinaryUrl(media, {
secure: true,
cname: cname,
secure_distribution: secureDistribution,
private_cdn: privateCdn,
Expand All @@ -63,8 +75,9 @@ const getNodeData = (
originalFormat: media.format,
// Keep all original data around
cloudinaryData: media,
// Generated url
// Generated urls
url: url,
secure_url: secureUrl,
// Internal
internal: {
type: NODE_TYPE,
Expand All @@ -79,7 +92,6 @@ const createCloudinaryNodes = async (
cloudinary,
resourceOptions,
cloudName,
secure,
cname,
secureDistribution,
privateCdn,
Expand All @@ -104,7 +116,6 @@ const createCloudinaryNodes = async (
gatsbyUtils,
resource,
cloudName,
secure,
cname,
secureDistribution,
privateCdn,
Expand Down

0 comments on commit 40f8896

Please sign in to comment.