6

I'm trying to add a favicon to a Next js project. The project was created using create-next-app.

I have a favicon.png in the public folder - Following the directions from static file serving here

In my Layout file I have below code:

       <Head>
          <title>{title}</title>
          <meta charSet="utf-8" />
          <meta
            name="viewport"
            content="initial-scale=1.0, width=device-width"
          />
          <link rel="shortcut icon" href="../public/favicon.png" />
          <link
            rel="stylesheet"
            href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
          />
        </Head>

Layout file lives in a components directory and public is in the root.

However, the favicon is not showing up. What am I doing wrong?

Sooraj
  • 9,717
  • 9
  • 64
  • 99

2 Answers2

4

The favicon is loaded by browser and not during build time. You should specify the webroot directory.

<link rel="icon" type="image/png" href="/favicon.png" />
Nikolai Kiselev
  • 6,201
  • 2
  • 27
  • 37
  • @Sooraj, what exactly error do you have? Does it say in a browser console/network tab that favicon is missing? I believe that if you `favicon.ico` in `/public` and you reference to it like `/favicon.ico` in the tag it will work. You can create a new project with `create-next-app` and the default scaffolding already has favicon included. However, I would assume that you don't see the favicon because it doesn't have type specified like `type="image/png"` – Nikolai Kiselev Mar 27 '20 at 16:14
  • @Sooraj, I've updated the answer, try to specify `type="image/png"` and let me know if you have any errors – Nikolai Kiselev Mar 27 '20 at 16:17
0

In your example you do not need to move up a directory. Simply use /public instead of ../public.

<link rel="shortcut icon" href="/public/favicon.ico" />

Pablo Rocha
  • 530
  • 4
  • 9