Search code examples
javascriptreactjscreate-react-appgatsbyfavicon

Not able to add favicon in gatsby config file


Even after adding favicon to gatsby hello world starter project in gatsby config file, its not working. I tried googling and searched in stackoverflow for similar question, How do i add favicon gatsby-config.js?. But that doesn't help or i maybe wrong somewhere.

Please correct me!!

GATSBY CONFIG.JS

/**
 * Configure your Gatsby site with this file.
 *
 * See: https://www.gatsbyjs.org/docs/gatsby-config/
 */

module.exports = {
  /* Your site config here */
  siteMetadata: {
    title: "xxxxxxx",
    author: "Subin",
  },
  plugins: [
    "gatsby-plugin-react-helmet",
    {
      resolve: "gatsby-source-contentful",
      options: {
        spaceId: process.env.CONTENTFUL_SPACE_ID,
        accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
      },
    },
    "gatsby-plugin-sass",
    // this plugin will pull all the files in our project system
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: "src",
        path: `${__dirname}/src/`,
        icon: `../src/images/favicon-32x32.png`,
      },
    },
    "gatsby-plugin-sharp",
    // REMARK plugin needed to extract the markdown files and parses
    {
      resolve: "gatsby-transformer-remark",
      options: {
        plugins: [
          "gatsby-remark-relative-images",
          {
            resolve: "gatsby-remark-images",
            options: {
              maxWidth: 750,
              linkImagesOriginal: false,
            },
          },
        ],
      },
    },
  ],
}

PROJECT DIRECTORY IMAGE

Image : Tree hierarchy of my project structure


Solution

  • To display your favicon, you need to have the gatsby-plugin-manifest installed, it doesn't come with the hello world starter.

    npm install --save gatsby-plugin-manifest
    

    And here is your gatsby-config.js with some default settings to this plugin:

    /**
     * Configure your Gatsby site with this file.
     *
     * See: https://www.gatsbyjs.org/docs/gatsby-config/
     */
    
    module.exports = {
      /* Your site config here */
      siteMetadata: {
        title: "xxxxxxx",
        author: "Subin"
      },
      plugins: [
        "gatsby-plugin-react-helmet",
        {
          resolve: "gatsby-source-contentful",
          options: {
            spaceId: process.env.CONTENTFUL_SPACE_ID,
            accessToken: process.env.CONTENTFUL_ACCESS_TOKEN
          }
        },
        "gatsby-plugin-sass",
        // this plugin will pull all the files in our project system
        {
          resolve: "gatsby-source-filesystem",
          options: {
            name: "src",
            path: `${__dirname}/src/`,
            icon: `../src/images/favicon-32x32.png`
          }
        },
        "gatsby-plugin-sharp",
        // REMARK plugin needed to extract the markdown files and parses
        {
          resolve: "gatsby-transformer-remark",
          options: {
            plugins: [
              "gatsby-remark-relative-images",
              {
                resolve: "gatsby-remark-images",
                options: {
                  maxWidth: 750,
                  linkImagesOriginal: false
                }
              }
            ]
          }
        },
        {
          resolve: `gatsby-plugin-manifest`,
          options: {
            name: "xxx",
            short_name: "xxxx",
            start_url: "/",
            background_color: "#6b37bf",
            theme_color: "#6b37bf",
            // Enables "Add to Homescreen" prompt and disables browser UI (including back button)
            // see https://developers.google.com/web/fundamentals/web-app-manifest/#display
            display: "standalone",
            icon: "src/images/favicon-32x32.png" // This path is relative to the root of the site.
          }
        }
      ]
    };
    

    Remember to stop your development server and start a brand new one when modifying gatsby-config.js in order to see your changes.

    Can you try this and let me know if it works as intended?