Browse Source

Updated a header with a ETag for caching

pull/4/head
jrtechs 6 years ago
parent
commit
8f5697bb59
2 changed files with 12 additions and 2 deletions
  1. +5
    -1
      img/image.js
  2. +7
    -1
      includes/includes.js

+ 5
- 1
img/image.js View File

@ -1,4 +1,5 @@
const utils = require('../utils/utils.js'); const utils = require('../utils/utils.js');
const crypto = require('crypto');
module.exports= module.exports=
{ {
@ -10,13 +11,14 @@ module.exports=
main: function(result, fileName, cache) main: function(result, fileName, cache)
{ {
//result.contentType = 'image/png'; //result.contentType = 'image/png';
result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=604800'});
var img = cache.get(fileName); var img = cache.get(fileName);
if(img == null) if(img == null)
{ {
utils.include("." + fileName).then(function(content) utils.include("." + fileName).then(function(content)
{ {
var eTag = crypto.createHash('md5').update(img).digest('hex');
result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'});
result.write(content); result.write(content);
result.end(); result.end();
cache.put(content); cache.put(content);
@ -24,6 +26,8 @@ module.exports=
} }
else else
{ {
var eTag = crypto.createHash('md5').update(img).digest('hex');
result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'});
result.write(img); result.write(img);
result.end(); result.end();
} }

+ 7
- 1
includes/includes.js View File

@ -10,6 +10,8 @@ const FOOTER_FILE = "includes/footer.html";
const Promise = require('promise'); const Promise = require('promise');
const crypto = require('crypto');
module.exports = module.exports =
{ {
/** Appends the header html section to the result which is /** Appends the header html section to the result which is
@ -43,7 +45,7 @@ module.exports =
*/ */
sendCSS: function(result, path, cache) sendCSS: function(result, path, cache)
{ {
result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control': 'public, max-age=604800'});
var css = cache.get(path); var css = cache.get(path);
@ -51,6 +53,8 @@ module.exports =
{ {
utils.include("./" + path).then(function(content) utils.include("./" + path).then(function(content)
{ {
var eTag = crypto.createHash('md5').update(content).digest('hex');
result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'});
result.write(content); result.write(content);
result.end(); result.end();
cache.put(path, content); cache.put(path, content);
@ -61,6 +65,8 @@ module.exports =
} }
else else
{ {
var eTag = crypto.createHash('md5').update(css).digest('hex');
result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'});
result.write(css); result.write(css);
result.end(); result.end();
} }

Loading…
Cancel
Save