serve favicons over hashed paths for improved caching

This commit is contained in:
Nick Krecklow
2020-05-08 04:47:10 -05:00
parent 66da5f6497
commit 4dfecce966
4 changed files with 73 additions and 18 deletions

View File

@ -1,3 +1,5 @@
const crypto = require('crypto')
const TimeTracker = require('./time')
const config = require('../config')
@ -61,11 +63,8 @@ class ServerRegistration {
update.recordData = this.recordData
}
// Compare against this.data.favicon to support favicon overrides
const newFavicon = resp.favicon || this.data.favicon
if (this.updateFavicon(newFavicon)) {
// Append an updated favicon
update.favicon = newFavicon
if (this.updateFavicon(resp.favicon)) {
update.favicon = this.getFaviconUrl()
}
// Append a result object
@ -101,7 +100,7 @@ class ServerRegistration {
const payload = {
versions: this.versions,
recordData: this.recordData,
favicon: this.lastFavicon
favicon: this.getFaviconUrl()
}
// Only append graphPeakData if defined
@ -134,7 +133,8 @@ class ServerRegistration {
message: 'Pinging...'
},
recordData: this.recordData,
graphPeakData: this.getGraphPeak()
graphPeakData: this.getGraphPeak(),
favicon: this.data.favicon
}
}
@ -216,13 +216,35 @@ class ServerRegistration {
}
updateFavicon (favicon) {
// If data.favicon is defined, then a favicon override is present
// Disregard the incoming favicon, regardless if it is different
if (this.data.favicon) {
return false
}
if (favicon && favicon !== this.lastFavicon) {
this.lastFavicon = favicon
// Generate an updated hash
// This is used by #getFaviconUrl
if (!this._faviconHasher) {
this._faviconHasher = crypto.createHash('md5')
}
this.faviconHash = this._faviconHasher.update(favicon).digest('hex').toString()
return true
}
return false
}
getFaviconUrl () {
if (this.faviconHash) {
return '/hashedfavicon?' + this.faviconHash
} else if (this.data.favicon) {
return this.data.favicon
}
}
updateProtocolVersionCompat (incomingId, outgoingId, protocolIndex) {
// If the result version matches the attempted version, the version is supported
const isSuccess = incomingId === outgoingId