How to set favicon.ico properly on vue.js webpack project?
Asked Answered
L

7

122

I have created a vue webpack project using vue-cli.

vue init webpack myproject

And then ran the project under dev mode:

npm run dev

I got this error:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/favicon.ico

So inside webpack, how to import the favicon.ico correctly?

Likeness answered 22/10, 2016 at 9:32 Comment(1)
Have you tried just dropping it into the root of the site? :) Or in the public build folder?Constitutionalism
A
181

Check out the Project Structure of webpack template: https://vuejs-templates.github.io/webpack/structure.html

Note that there is a static folder, along with node_modules, src, etc.

If you put some image into the static folder, like favicon.png, it will be made available at http://localhost:8080/static/favicon.png

Here is the documentation for static assets: https://vuejs-templates.github.io/webpack/static.html

For your favicon issue, you can put a favicon.ico or favicon.png into the static folder and refer in the <head> of your index.html as follows:

<head>
    <meta charset="utf-8">
    <link rel="shortcut icon" type="image/png" href="/static/favicon.png"/>
    <title>My Vue.js app</title>
    ...
</head>

If you do not define a favicon.ico in your index.html, then the browser will request for a favicon from the website root (default behaviour). If you specify a favicon as above, you will not see that 404 anymore. The favicon will also start showing up in your browser tabs.

As a side note, here is the reason why I prefer PNG instead of ICO file:

favicon.png vs favicon.ico - why should I use PNG instead of ICO?

Antiquate answered 22/10, 2016 at 11:33 Comment(5)
what if I want to keep my favicon in src/assets? is it best to leave it in static folder along with .gitkeep?Wuhan
@AkinHwan I haven't attempted this yet, but if you keep any image in src/assets, it will be treated as a module dependency and will go into the final build file as an inline image (base64 format). This will unnecessarily increase the build size. Nothing to worry, it will not affect the performance in any way. It is my preference to keep images as "real static assets" as explained in the docs. Your preferences may vary. You need to try both methods and choose the one that works for you.Antiquate
This answer seems a bit out of date. In the current vue webpack template, there's a public directory, not a static directory.Ludhiana
@Ludhiana also, using "shortcut" in the link rel is only used for internet explorer.Haft
This is working for routing but for direct link it is not workedScurvy
K
23

For some reason, the above solutions did not work for me before converting the default favicon.ico file to favicon.png and renaming it to favicon-xyz.png e.g. (I have put this file in /public folder) and edited the index.html file as follows:

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="icon" href="<%= BASE_URL %>favicon-xyz.png">
    .
    .
    .
</head>

Might be useful for someone.

Keary answered 10/4, 2020 at 17:14 Comment(2)
It works for me nicely. Just needed to put my custom "favicon" icon into the "/public" folder after removing original one.Electroballistics
This works oddly wellElwin
V
14

Little update for Laravel 5 to 10, place your favicon.ico or favicon.png into the /public folder and refer to it in your index.html like this:

<head>
    <meta charset="utf-8">
    <link rel="shortcut icon" type="image/png" href="/favicon.png"/>
    <title>My Vue.js app</title>
    ...
</head>

Hope it helps !

Victor answered 3/9, 2019 at 12:58 Comment(1)
This no longer worksMalan
D
6

As of 2022, @Mani answer is a little outdated as static assets are now served in public folder other than static.

Just generate a .ico favicon file (this site provides free online favicon generation), rename it to favicon.ico and place it in public folder, no need to change index.html, reload and new favicon will be displayed.

Dubitation answered 12/4, 2022 at 2:14 Comment(1)
This does not work, simply replacing the favicon.ico does nothing, the same vue icon is showingMalan
E
2

Just add shortcut in rel tag like that :

<link rel="shortcut icon" href="<%= BASE_URL %>favicon.ico">
Estuarine answered 13/12, 2022 at 12:22 Comment(0)
L
1

You may use vue-pwa-asset-generator in order to generate a full set of favicons foreach platform.

Licorice answered 4/10, 2022 at 13:49 Comment(0)
R
0

Thanks. This did the trick as per eightballs comments. I needed to move the file to the /public folder and rename it :D

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>gear.png">
    <title>Prodigy</title>
</head>
Riflery answered 17/8, 2022 at 8:42 Comment(1)
Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From ReviewGullah

© 2022 - 2024 — McMap. All rights reserved.