From f2fcb5b62b1a14b47522a58ad164102873804f98 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Tue, 9 Sep 2014 18:49:26 +0000 Subject: [PATCH] Let Ghost serve favicon instead of using dependency no ref - Remove static-favicon dependency - Refactor robots.txt middleware to also serve favicon - Add ETag --- core/server/middleware/index.js | 25 ++++++++++---------- core/test/functional/routes/frontend_test.js | 10 ++++++++ core/test/utils/index.js | 1 + package.json | 1 - 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/core/server/middleware/index.js b/core/server/middleware/index.js index ad6e4b2c63..9a8254b910 100644 --- a/core/server/middleware/index.js +++ b/core/server/middleware/index.js @@ -5,9 +5,9 @@ var api = require('../api'), bodyParser = require('body-parser'), config = require('../config'), + crypto = require('crypto'), errors = require('../errors'), express = require('express'), - favicon = require('static-favicon'), fs = require('fs'), hbs = require('express-hbs'), logger = require('morgan'), @@ -211,14 +211,14 @@ function checkSSL(req, res, next) { next(); } -// ### Robots Middleware -// Handle requests to robots.txt and cache file -function robots() { - var content, // file cache - filePath = path.join(config.paths.corePath, '/shared/robots.txt'); +// ### ServeSharedFile Middleware +// Handles requests to robots.txt and favicon.ico (and caches them) +function serveSharedFile(file, type, maxAge) { + var content, + filePath = path.join(config.paths.corePath, 'shared', file); - return function robots(req, res, next) { - if (req.url === '/robots.txt') { + return function serveSharedFile(req, res, next) { + if (req.url === path.join('/', file)) { if (content) { res.writeHead(200, content.headers); res.end(content.body); @@ -230,9 +230,10 @@ function robots() { content = { headers: { - 'Content-Type': 'text/plain', + 'Content-Type': type, 'Content-Length': buf.length, - 'Cache-Control': 'public, max-age=' + utils.ONE_YEAR_S + ETag: '"' + crypto.createHash('md5').update(buf, 'utf8').digest('hex') + '"', + 'Cache-Control': 'public, max-age=' + maxAge }, body: buf }; @@ -274,7 +275,7 @@ setupMiddleware = function (server) { } // Favicon - expressServer.use(favicon(corePath + '/shared/favicon.ico')); + expressServer.use(serveSharedFile('favicon.ico', 'image/x-icon', utils.ONE_DAY_S)); // Static assets expressServer.use('/shared', express['static'](path.join(corePath, '/shared'), {maxAge: utils.ONE_HOUR_MS})); @@ -300,7 +301,7 @@ setupMiddleware = function (server) { expressServer.use(middleware.staticTheme()); // Serve robots.txt if not found in theme - expressServer.use(robots()); + expressServer.use(serveSharedFile('robots.txt', 'text/plain', utils.ONE_YEAR_S)); // Add in all trailing slashes, properly include the subdir path // in the redirect. diff --git a/core/test/functional/routes/frontend_test.js b/core/test/functional/routes/frontend_test.js index e8d226e1f9..4f18f1683e 100644 --- a/core/test/functional/routes/frontend_test.js +++ b/core/test/functional/routes/frontend_test.js @@ -15,6 +15,7 @@ var request = require('supertest'), cacheRules = { public: 'public, max-age=0', + day: 'public, max-age=' + testUtils.ONE_DAY_S, hour: 'public, max-age=' + testUtils.ONE_HOUR_S, year: 'public, max-age=' + testUtils.ONE_YEAR_S, private: 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0' @@ -463,6 +464,15 @@ describe('Frontend Routing', function () { it('should retrieve default robots.txt', function (done) { request.get('/robots.txt') .expect('Cache-Control', cacheRules.year) + .expect('ETag', /[0-9a-f]{32}/i) + .expect(200) + .end(doEnd(done)); + }); + + it('should retrieve default favicon.ico', function (done) { + request.get('/favicon.ico') + .expect('Cache-Control', cacheRules.day) + .expect('ETag', /[0-9a-f]{32}/i) .expect(200) .end(doEnd(done)); }); diff --git a/core/test/utils/index.js b/core/test/utils/index.js index 344b6f4063..cb0ff422db 100644 --- a/core/test/utils/index.js +++ b/core/test/utils/index.js @@ -539,5 +539,6 @@ module.exports = { } }, ONE_HOUR_S: 3600, + ONE_DAY_S: 86400, ONE_YEAR_S: 31536000 }; diff --git a/package.json b/package.json index 091b868add..4d8a16ff43 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,6 @@ "semver": "2.2.1", "showdown": "https://github.com/ErisDS/showdown/archive/v0.3.2-ghost.tar.gz", "sqlite3": "2.2.7", - "static-favicon": "1.0.2", "unidecode": "0.1.3", "validator": "3.4.0", "xml": "0.0.12"