Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support and test for specifying mime type of icon #30

Merged
merged 2 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Returns a middleware serving the favicon found on the given `path`.
#### options

- `maxAge` cache-control max-age directive in ms, defaulting to 1 day.
- `mime` specify the mime-type of the favicon, defaults to "image/x-icon"

## License

Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const fs = require('fs');
*
* @param {String} path
* @param {Object} [options]
* @param {Number} [options.maxAge=null]
* @param {String} [options.mime="image/x-icon"] MIME type of the file at path
tedeh marked this conversation as resolved.
Show resolved Hide resolved
* @return {Function}
* @api public
*/
Expand All @@ -33,6 +35,7 @@ module.exports = function (path, options){
? 86400000
: Math.min(Math.max(0, options.maxAge), 31556926000);
const cacheControl = `public, max-age=${maxAge / 1000 | 0}`;
const mime = options.mime || 'image/x-icon';

return (ctx, next) => {
if ('/favicon.ico' != ctx.path) {
Expand All @@ -46,7 +49,7 @@ module.exports = function (path, options){
// lazily read the icon
if (!icon) icon = fs.readFileSync(path);
ctx.set('Cache-Control', cacheControl);
ctx.type = 'image/x-icon';
ctx.type = mime;
ctx.body = icon;
}
};
Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ describe('favicon()', function(){
.expect(200, body, done);
});

it('should send the favicon with a different mime type', done => {
const body = fs.readFileSync(path);

const app = new Koa();
app.use(favicon(path, {mime: 'image/png'}));

request(app.listen())
.get('/favicon.ico')
.expect('Content-Type', 'image/png')
.expect(200, body, done);
});

it('should set cache-control headers', done => {
const app = new Koa();
app.use(favicon(path));
Expand Down