22

For some reason my favicon is not working.

I have the favicon saved as favicon.ico in the /public directory and am referencing it in the <Head> of my pages (located in the /pages directory) but to no avail.

Can anyone help?

-

Here is my code for my index.js:

  <Head>
      <title>Create Next App</title>
      <link rel="icon" href="/favicon.ico" />

dir/pages/index.js
dir/public/favicon.ico
atman_lens
  • 474
  • 2
  • 6
  • 16
  • 1
    Are you trying to see it locally or through a hosted online service? – tomerpacific May 16 '20 at 12:47
  • I'm trying to see it locally. But it doesn't work when I deploy it to Vercel's Now either – atman_lens May 16 '20 at 12:49
  • 1
    I would try to replace your relative path to the icon to the actual path (locally) and see if it works locally. Also add the type attribute ```type="image/x-icon"```. – tomerpacific May 16 '20 at 12:52
  • Nope, that's not working. Could it be because I am repeating the component with the tag on each page (index.js, whats-on.js etc.) instead of just creating a single component that contains the ? – atman_lens May 16 '20 at 12:55
  • 1
    What's the exact problem, that the icon is unreachable through the web server or that the browser is not fetching it and using it in the expected locations? – Álvaro González May 16 '20 at 13:03
  • Are they all located in the same directory relative to the path to the favicon? Can you try just having one page with the reference to the favicon and seeing if that works? – tomerpacific May 16 '20 at 13:04
  • I'm not sure if it is unreachable or if the browser is fetching it. How do I find that out? Next js is meant to automatically direct image urls to the /public directory. I'm actually using an official next.js template which already referenced its own favicon.ico (which I replaced with mine) but even before I did anything to the app the favicon wasn't working. I've tried the path '../public/favicon.ico' despite Next being meant to automatically direct it but that's not working either. – atman_lens May 16 '20 at 13:14
  • 1
    To determine if the favicon is reachable you can try to load it in your browser. Type the URL in the location bar (that bar on top where you type google queries). To determine if the browser is recognising it as favicon you can check whether it shows up in the page tab. – Álvaro González May 16 '20 at 13:45
  • Yes, the favicon is reachable – atman_lens May 16 '20 at 13:47
  • It's reachable at /favicon.ico as expected (not /public/favicon.ico) – atman_lens May 16 '20 at 13:48

13 Answers13

39

Put the favicons in an /image directory inside the /public directory and put the code below in your _app.js

<Head>
          <link rel="shortcut icon" href="/images/favicon.ico" />
          <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png" />
          <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png"/>
          <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png"/>
</Head>
infecting
  • 523
  • 6
  • 10
  • 3
    I confirm this. – Greg Forel Aug 12 '21 at 07:17
  • 1
    This worked for me I was migrating from the next js version 8 to 11 and this works. – anup Aug 18 '21 at 19:27
  • 7
    Why is this necessary? It fixed my issue however I've noticed the Next.js boilerplate project does not do this and it seems to work there ... – sambecker Dec 09 '21 at 05:19
  • 1
    It works in _document.js too. – francis Feb 15 '22 at 20:49
  • My favicon was working perfectly on every other page except the 404 page. This worked for me and I don't know why? @sambecker have you discovered why this configuration works this way? Quite weird. – Victor Eke Jul 21 '22 at 11:28
  • I’m not sure @Eke but in case it helps, one of the mistakes I made was using a convenience component within `

    `. Apparently all links must be direct descendants of `

    `.

    – sambecker Jul 23 '22 at 03:10
  • I just added it to a /public/image folder without adding any code to and it worked for me on next 12.2.3 – skpn Aug 25 '22 at 07:59
  • 1
    They asked why it works this way, although the react documentation does not say about it. I found an article about favicon. Explain their logic and browser support and why so. --> https://css-tricks.com/favicon-quiz/ – al-bulat Dec 21 '22 at 09:53
13

It is possible that the .ico image was incorrectly created - perhaps converted online from a .png. This will cause the image to be viewable in the browser and elsewhere but unable to be used as a favicon.

In order to trouble shoot, try using a .png instead of .ico and see if the issue persists. If this solves your problem, consider using a converter targeted specifically for favicons, i.e. https://favicon.io/favicon-converter/


In my case, the file was in the correct place (/public/favicon.ico), was being referenced properly (href="/favicon.ico"), and was served when visiting http://localhost:3000/favicon.ico, yet it did not display in the browser tab until I reconverted it.

joematune
  • 1,087
  • 10
  • 16
10

I was facing exactly the same issue as you did. It seems it is necessary to put the image file in /public/images/

It turns to work properly once I have done this.

Eric Lee
  • 171
  • 2
  • 10
8

Only helpful if you are running into issues while using next basePath:

I was running into an issue where my favicon was not showing up. In my case it was because I was using basePath: '/some-base-path' in my next config. When you add a basePath it changes the path to the static assets.

Example: A basePath of /test with an image at public/favicon.ico can be referenced at /test/public/favicon.ico

Debugging note: I also had to clear my cache to see the changes (or try incognito).

nvk
  • 221
  • 3
  • 1
3

In my case, I had public/ in the src/ directory.

I changed it to:

.
├── next.config.js
├── public
│   ├── favicon.ico
│   └── img
└── src
    ├── pages
    └── styles

(moved public/ to the root .)

nezort11
  • 326
  • 1
  • 4
  • 11
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 02 '22 at 02:37
  • For Next.js 11 (11.1.4) this solution worked both in dev and production (AWS Amplify). – Rick Jun 07 '22 at 09:47
1

while building your app for production, just cut favicon.ico from root and paste into public folder.

0

remove / from: /favicon.ico and your image should load if the format ico works. If it do not work convert the .ico image to .png or .jpg.

<link rel="icon" href="favicon.ico" />
Filip Huhta
  • 2,043
  • 7
  • 25
0

I got this error while I was creating my favicon, and it turns out that i've put the wrong image format in type! For example, I did this:

<link rel="icon" type="image/png" href="/favicon.ico">

but it turns out I had to change image/png to image/x-icon because the formats didn't match.

<link rel="icon" type="image/x-icon" href="/favicon.ico">
Zepvil
  • 75
  • 2
  • 6
0

To continue on @joematune answer, if you converted your .ico from .png or something like that, it will probably not work out of a random website. The readme.md file in the starter kit shares a link that converts it successfully. As a bouns, it also give you a bunch of variations (16x16, 32x32, apple-touch, etc.). Just replacing the old files with the new ones worked for me without modifying anything.

this is the link

MatanyaP
  • 306
  • 3
  • 15
0

If you think your configuration is correct but it's not still working, clear .next folder and run yarn build once again. Changes should apply this time.

Mahdieh Shavandi
  • 4,906
  • 32
  • 41
0

save favicon into /public, and use like this:

<link rel="icon" href="/favicon.ico" />
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Hamid Mohamadi
  • 141
  • 2
  • 4
0

For anyone who uses basePath property in Next.js configuration file, I have solved this issue to add my basePath value to href attribute of my <link> tag.

ex)

<link rel="icon" href="/[yourBasePath]/images/favicon.ico"
Y.Han
  • 47
  • 4
0

App router:

Place favicon.ico in public/img folder y a then add to layout.js

  <head>
    <link rel="icon" href="/img/favicon.ico" sizes="any" />
  </head>
Abel
  • 3,989
  • 32
  • 31