Add Favicon with React and Webpack

前端 未结 14 2134
星月不相逢
星月不相逢 2020-12-12 19:46

I am attempting to add a favicon to a React-based website that I made using webpack. It has been a total nightmare to add a favicon and I have tried many solutions to no ava

14条回答
  •  伪装坚强ぢ
    2020-12-12 20:15

    Browsers look for your favicon in /favicon.ico, so that's where it needs to be. You can double check if you've positioned it in the correct place by navigating to [address:port]/favicon.ico and seeing if your icon appears.

    In dev mode, you are using historyApiFallback, so will need to configure webpack to explicitly return your icon for that route:

    historyApiFallback: {
        index: '[path/to/index]',
        rewrites: [
            // shows favicon
            { from: /favicon.ico/, to: '[path/to/favicon]' }
        ]
    }
    

    In your server.js file, try explicitly rewriting the url:

    app.configure(function() {
        app.use('/favicon.ico', express.static(__dirname + '[route/to/favicon]'));
    });
    

    (or however your setup prefers to rewrite urls)

    I suggest generating a true .ico file rather than using a .png, since I've found that to be more reliable across browsers.

提交回复
热议问题