Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving favicon next.js

I am using the boilerplate Next.js starter. In index.js, an instance of the express application is defined as expressApp. I am simply attempting to serve my favicon using serve-favicon and having no luck:

expressApp.use(favicon(path.join(__dirname, "static", "brand", "favicon.ico")));

Checked the path that is being resolved and it is correct. How does next.js implementation of express differ?

like image 589
wmash Avatar asked Nov 15 '25 17:11

wmash


1 Answers

From the serve-favicon repository:

Note This module is exclusively for serving the "default, implicit favicon", which is GET /favicon.ico. For additional vendor-specific icons that require HTML markup, additional middleware is required to serve the relevant files, for example serve-static.

So it's not possible with this package to serve a favicon in another path, because that would require adding markup. To fix this, add a link tag inside your pages/_document.js template:

<Head>
  <link rel="icon" type="image/x-icon" href="/brand/favicon.ico" />
</Head>

While keeping your serve-favicon code in the index.js file:

expressApp.use(favicon(path.join(__dirname, "static", "brand", "favicon.ico")));
like image 157
Fabian Schultz Avatar answered Nov 17 '25 08:11

Fabian Schultz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!