From f43e58f31295856611ca5a242de39ed63278e148 Mon Sep 17 00:00:00 2001 From: Tedde Lundgren Date: Wed, 26 Feb 2020 09:54:46 +0700 Subject: [PATCH 1/2] add support and test for specifying mime type of icon --- index.js | 5 ++++- test/index.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index daa4e54..2aad542 100644 --- a/index.js +++ b/index.js @@ -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 * @return {Function} * @api public */ @@ -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) { @@ -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; } }; diff --git a/test/index.js b/test/index.js index 02d4742..855d1da 100644 --- a/test/index.js +++ b/test/index.js @@ -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)); From 820b1925622a3affe79666ce59688672c36b2e13 Mon Sep 17 00:00:00 2001 From: Tedde Lundgren Date: Thu, 27 Feb 2020 14:12:10 +0700 Subject: [PATCH 2/2] update readme with mime option --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index cc6b889..6b5c7d8 100644 --- a/Readme.md +++ b/Readme.md @@ -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