0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Let Ghost serve favicon instead of using dependency

no ref
- Remove static-favicon dependency
- Refactor robots.txt middleware to also serve favicon
- Add ETag
This commit is contained in:
Fabian Becker 2014-09-09 18:49:26 +00:00
parent c38c0cdfe1
commit f2fcb5b62b
4 changed files with 24 additions and 13 deletions

View file

@ -5,9 +5,9 @@
var api = require('../api'), var api = require('../api'),
bodyParser = require('body-parser'), bodyParser = require('body-parser'),
config = require('../config'), config = require('../config'),
crypto = require('crypto'),
errors = require('../errors'), errors = require('../errors'),
express = require('express'), express = require('express'),
favicon = require('static-favicon'),
fs = require('fs'), fs = require('fs'),
hbs = require('express-hbs'), hbs = require('express-hbs'),
logger = require('morgan'), logger = require('morgan'),
@ -211,14 +211,14 @@ function checkSSL(req, res, next) {
next(); next();
} }
// ### Robots Middleware // ### ServeSharedFile Middleware
// Handle requests to robots.txt and cache file // Handles requests to robots.txt and favicon.ico (and caches them)
function robots() { function serveSharedFile(file, type, maxAge) {
var content, // file cache var content,
filePath = path.join(config.paths.corePath, '/shared/robots.txt'); filePath = path.join(config.paths.corePath, 'shared', file);
return function robots(req, res, next) { return function serveSharedFile(req, res, next) {
if (req.url === '/robots.txt') { if (req.url === path.join('/', file)) {
if (content) { if (content) {
res.writeHead(200, content.headers); res.writeHead(200, content.headers);
res.end(content.body); res.end(content.body);
@ -230,9 +230,10 @@ function robots() {
content = { content = {
headers: { headers: {
'Content-Type': 'text/plain', 'Content-Type': type,
'Content-Length': buf.length, '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 body: buf
}; };
@ -274,7 +275,7 @@ setupMiddleware = function (server) {
} }
// Favicon // Favicon
expressServer.use(favicon(corePath + '/shared/favicon.ico')); expressServer.use(serveSharedFile('favicon.ico', 'image/x-icon', utils.ONE_DAY_S));
// Static assets // Static assets
expressServer.use('/shared', express['static'](path.join(corePath, '/shared'), {maxAge: utils.ONE_HOUR_MS})); 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()); expressServer.use(middleware.staticTheme());
// Serve robots.txt if not found in theme // 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 // Add in all trailing slashes, properly include the subdir path
// in the redirect. // in the redirect.

View file

@ -15,6 +15,7 @@ var request = require('supertest'),
cacheRules = { cacheRules = {
public: 'public, max-age=0', public: 'public, max-age=0',
day: 'public, max-age=' + testUtils.ONE_DAY_S,
hour: 'public, max-age=' + testUtils.ONE_HOUR_S, hour: 'public, max-age=' + testUtils.ONE_HOUR_S,
year: 'public, max-age=' + testUtils.ONE_YEAR_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' 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) { it('should retrieve default robots.txt', function (done) {
request.get('/robots.txt') request.get('/robots.txt')
.expect('Cache-Control', cacheRules.year) .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) .expect(200)
.end(doEnd(done)); .end(doEnd(done));
}); });

View file

@ -539,5 +539,6 @@ module.exports = {
} }
}, },
ONE_HOUR_S: 3600, ONE_HOUR_S: 3600,
ONE_DAY_S: 86400,
ONE_YEAR_S: 31536000 ONE_YEAR_S: 31536000
}; };

View file

@ -62,7 +62,6 @@
"semver": "2.2.1", "semver": "2.2.1",
"showdown": "https://github.com/ErisDS/showdown/archive/v0.3.2-ghost.tar.gz", "showdown": "https://github.com/ErisDS/showdown/archive/v0.3.2-ghost.tar.gz",
"sqlite3": "2.2.7", "sqlite3": "2.2.7",
"static-favicon": "1.0.2",
"unidecode": "0.1.3", "unidecode": "0.1.3",
"validator": "3.4.0", "validator": "3.4.0",
"xml": "0.0.12" "xml": "0.0.12"