1

I am trying to add new projects to my GatsbyJS site using Netlify CMS. The problem is that my code is using relative path to reach the thumb images but when uploading a new thumb image from netlify it saves it as an absolute path. This is causing a problem and I dont understand what it is exactly. Is Gatsby not able to read the absolute path that is being received from the CMS? If that could be the problem, how could i make that the CMS gives me a relative path?

Any help will be much appreciated :)

This is the path in my markdown files:

thumb: ../images/thumbs/memory.png

This is the path created by the CMS:

/src/images/thumbs/img_2370.jpg

This is my config.yml

    backend:
  name: git-gateway
  branch: master # Branch to update (optional; defaults to master)

publish_mode: editorial_workflow

media_folder: "src/images/thumbs"

collections:
  - name: "projects" # Used in routes, e.g., /admin/collections/blog
    label: "Projects" # Used in the UI
    folder: "src/projects" # The path to the folder where the documents are stored
    create: true # Allow users to create new documents in this collection
    slug: "{{slug}}" # Filename template, e.g., YYYY-MM-DD-title.md
    fields: # The fields for each document, usually in front matter
      - { label: "Title", name: "title", widget: "string" }
      - { label: "Stack", name: "stack", widget: "string" }
      - { label: "Slug", name: "slug", widget: "string" }
      - { label: "Url", name: "url", widget: "string" }
      - { label: "Publish Date", name: "date", widget: "datetime" }
      - { label: "Thumb", name: "thumb", widget: "image" }
      - { label: "Body", name: "body", widget: "markdown" }
Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
Simon CF
  • 25
  • 1
  • 1
  • 8

1 Answers1

0

This is a "known issue" that Netlify should fix soon (it has been 2 years since it has been spotted so far). Netlify doesn't support relative paths hence the image paths are broken.

Consider using the following solutions:

  • gatsby-plugin-netlify-cms-paths adding the following in your gatsby-config.js:

    module.exports = {
       plugins: [
    
         // Including in your Gatsby plugins will transform any paths in your frontmatter
         `gatsby-plugin-netlify-cms-paths`,
    
         // Including in your Remark plugins will transform any paths in your markdown body
         {
           resolve: `gatsby-transformer-remark`,
           options: {
             plugins: [
               `gatsby-plugin-netlify-cms-paths`,
             ],
           },
         },
       ],
    }
    
  • gatsby-remark-relative-images-v2 by adding:

       {
         resolve: `gatsby-transformer-remark`,
         options: {
           plugins: [
             // gatsby-remark-relative-images-v2 must
             // go before gatsby-remark-images
             {
               resolve: `gatsby-remark-relative-images-v2`,
             },
             {
               resolve: `gatsby-remark-images`,
               options: {
                 // It's important to specify the maxWidth (in pixels) of
                 // the content container as this plugin uses this as the
                 // base for generating different widths of each image.
                 maxWidth: 590,
               },
             },
           ],
         },
       },
    
  • Check that media_folder and public_folder are properly configured:

    media_folder: src/images/thumbs
    public_folder: /images/thumbs
    

    Note the starting slash in the public_folder and double-check that those are the correct ones.

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • Thanks a lot for your answer! Sadly still not working. I have added the new plugins. Do i need to change the query I was using? I have also changes the config.yml to: media_folder: "src/images/thumbs" public_folder: "/src/images/thumbs" – Simon CF May 31 '22 at 10:44
  • I used to use `/static` folder for the uploaded assets. I guess that assets inside `/src` folder are not transpiled via webpack into the public one because there are any usages of them. Try using `media_folder: static/assets/images public_folder: /assets/images` (and change the paths of the markdowns accordingly) – Ferran Buireu May 31 '22 at 11:44
  • I have changed the images to be located in the static folder + adapted the config.yml, but still not working. I have adapted the path in the markdowns like this: ../../static/assets/images/countries.png But still what is being uploaded to github looks different: from my code: ../../static/assets/images/countries.png from CMS: /assets/images/140.jpeg – Simon CF May 31 '22 at 14:20
  • If the images are uploaded to the `static` folder at `/static/assets/images/imageName.jpg` the CMS path should be correct since the image exist at that route – Ferran Buireu May 31 '22 at 16:01
  • 3:13:46 PM: error Page data from page-data.json for the failed page "/projects/": { 3:13:46 PM: "componentChunkName": "component---src-pages-projects-index-js", "path": "/projects/", "result": { "data": { "projects": { "nodes": [ { "html": "

    testing images in the static folder

    ", "frontmatter": { "title": "testing images in static folderr", "stack": "react", "slug": "static-folder", Creating deploy upload records "url": "simonc.netlify.app", "thumb": null }, "id": "94da2cfa-4a31-5548-80e2-896e64ce8434" },
    – Simon CF May 31 '22 at 18:11
  • This is the error I am getting from netlify. Somehow the image is not being uploaded when creating a new project from the CMS... – Simon CF May 31 '22 at 18:11