This middleware for Koa applications efficiently serves a `favicon.ico` file, handling requests for `/favicon.ico` and setting appropriate cache headers. Built upon the proven `serve-favicon` package, it provides a straightforward way to integrate favicon serving directly into the Koa middleware stack. The current stable version is 2.1.0, a mature release from the Koa organization. Its release cadence is generally slow, primarily responding to Koa framework updates or critical maintenance. It differentiates itself by its minimalistic API and direct compatibility with Koa's `app.use()` pattern, making it a standard choice for basic favicon serving in Koa projects. It supports custom `maxAge` caching and MIME type specification.
npm install koa-faviconVerified import paths — ran on the pinned version, not inferred.
This example demonstrates setting up a basic Koa application to serve a favicon from a specified path with a 7-day cache, showcasing typical usage with ES Modules.
Upgrade `koa-favicon` to version `2.x` for compatibility with Koa v2+ applications using `async/await`. If you are specifically targeting Koa v1, use `koa-favicon@^1.0.0`.
Always use `path.join(__dirname, 'your_directory', 'favicon.ico')` to construct an absolute and robust path, ensuring the file exists at the specified location relative to your application's entry point. When using ES Modules, `__dirname` needs to be derived (as shown in the quickstart).
Ensure you multiply your desired duration in seconds by `1000`. For example, `1000 * 60 * 60 * 24` sets the `maxAge` for one full day.
Double-check the absolute path provided to the `favicon()` middleware. Verify the file's existence and correct its path using `path.join` for robustness, especially with different environments.
Ensure you are correctly importing Koa (`import Koa from 'koa';`) and instantiating it with `const app = new Koa();`. This error is rare in modern Koa environments.