question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add responsiveness support for images uploaded through Netlify CMS

See original GitHub issue

Currently, the recommended way to upload images to a static website through Netlify CMS is by using the static folder, which does not offer any support for responsiveness. I would like to ask for a way to make CMS-managed assets queryable through GraphQL.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

5reactions
kripodcommented, Nov 30, 2017

Thank you for your kind assistance! Although the createNodeField method didn’t work as expected, I ended up with the following code, based on yours:

exports.onCreateNode = ({
  node, getNode, getNodes, boundActionCreators,
}) => {
  const { createNodeField, createParentChildLink } = boundActionCreators;

  if (node.internal.type === 'MarkdownRemark') {
    const slug = createFilePath({ node, getNode, basePath: 'pages' });
    createNodeField({
      node,
      name: 'slug',
      value: slug,
    });

    // Attach thumbnail's ImageSharp node by public path if necessary
    if (typeof node.frontmatter.thumbnail === 'string') {
      // Find absolute path of linked path
      const pathToFile = path
        .join(__dirname, 'static', node.frontmatter.thumbnail)
        .split(path.sep)
        .join('/');

      // Find ID of File node
      const fileNode = getNodes().find(n => n.absolutePath === pathToFile);

      if (fileNode != null) {
        // Find ImageSharp node corresponding to the File node
        const imageSharpNodeId = fileNode.children.find(n => n.endsWith('>> ImageSharp'));
        const imageSharpNode = getNodes().find(n => n.id === imageSharpNodeId);

        // Add ImageSharp node as child
        createParentChildLink({ parent: node, child: imageSharpNode });
      }
    }
  }
};

It would be great if there was a more in-depth documentation about linking nodes (or even their properties) together.

2reactions
waywardmcommented, Jul 27, 2018

Netlify Image Solution

To allow using gatsby image processing plugins on your frontmatter

In each Markdown file

relpath: ../../..  
logo: /static/images/myimage.png

In gatsby-config.js

Under plugins add the code to access the static image folder as files

{
      resolve: 'gatsby-source-filesystem',
      options: {
        name: 'static/images',
        path: `${__dirname}/static/images`,
      },
    },

In gatsby-node.js

Create a new nodeField for each image needed

exports.onCreateNode = ({ node, getNode, actions }) => {
  const { createNodeField } = actions;

if (node.internal.type === 'MarkdownRemark') {

if (node.frontmatter.relpath && node.frontmatter.logo ) {

      const logopath = node.frontmatter.relpath + node.frontmatter.logo

      createNodeField({
        node,
        name: 'logolink',
        value: logopath
      });
    }

}

}

In config.yml

Add media folder

media_folder: "static/images" 

In each collection in config.yml

State relative path for each collection that require an image in the frontmatter as a hidden field that user doesn’t need to modify

- {label: "Relative Path", name: "relpath", widget: "hidden", default: "../../.."}

Query in Graphql

{
  allMarkdownRemark {
    edges {
      node {
        id
        frontmatter {
          title
        }
        fields {
          logolink {
            childImageSharp {
              id
            }
          }
        }
      }
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Transform images - Netlify Docs
Use dynamic image transformation for files tracked by the Large Media service. Upload images at full resolution, then serve exactly the file size...
Read more >
Netlify CMS 2.1.0 adds external media support with Uploadcare
Netlify CMS 2.1.0 adds external media support with Uploadcare, allowing files like images and videos to be stored outside of your Git ...
Read more >
Media Management With the Cloudinary-Netlify CMS Integration
You can do a lot more with the Media Library widget, for example, upload, tag, move, or delete images. In other words, the...
Read more >
How to Optimize Images on Netlify with the Cloudinary Build ...
Step 3: Updating plugin configuration to upload images to Cloudinary ... By default, the Cloudinary Build Plugin for Netlify will “fetch” the ...
Read more >
Gatsby + Netlify CMS images from Markdown are not converted
The images that I uploaded. They were not responsive. But all of the original post that come with the templete are responsive. Also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found