From 087f3691fbb24cc8a01cc4c8c6faace6fd4a14cf Mon Sep 17 00:00:00 2001 From: LordMathis Date: Mon, 25 Jun 2018 14:27:25 +0200 Subject: [PATCH] Implement cache control --- src/utils/api.js | 3 +++ src/utils/staticFiles.js | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/utils/api.js b/src/utils/api.js index 87c85b8..209f776 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -5,11 +5,13 @@ const path = require('path'); const config = require('./config.json'); api.get('/blog', (req, res) => { + res.set('Cache-Control', 'no-cache'); res.json(data.posts); }); api.get('/about', (req, res) => { const renderPath = path.join(process.cwd(), '/renders', 'about.html'); + res.set('Cache-Control', 'max-age=86400'); fs.readFile(renderPath, 'utf8', (err, data) => { if (err) { res.json({ @@ -24,6 +26,7 @@ api.get('/about', (req, res) => { }); api.get('/post/:postname', (req, res) => { + res.set('Cache-Control', 'no-cache'); const postname = req.params.postname; const post = data.posts.find((el) => { return el.filename === postname diff --git a/src/utils/staticFiles.js b/src/utils/staticFiles.js index e684ad8..b825e0e 100644 --- a/src/utils/staticFiles.js +++ b/src/utils/staticFiles.js @@ -3,25 +3,34 @@ const path = require('path'); staticFiles.get('/bundle.js', (req, res) => { if (req.acceptsEncodings('gzip')) { - res.set('Content-Encoding', 'gzip'); - res.set('Content-Type', 'text/javascript'); + res.set({ + 'Content-Encoding': 'gzip', + 'Content-Type': 'text/javascript', + 'Cache-Control': 'max-age=86400' + }); res.sendFile(path.join(process.cwd(), '/public/static/bundle.js.gz')); } else { + res.set('Cache-Control', 'max-age=86400'); res.sendFile(path.join(process.cwd(), '/public/static/bundle.js')); } }); staticFiles.get('/bundle.css', (req, res) => { if (req.acceptsEncodings('gzip')) { - res.set('Content-Encoding', 'gzip'); - res.set('Content-Type', 'text/css'); + res.set({ + 'Content-Encoding': 'gzip', + 'Content-Type': 'text/css', + 'Cache-Control': 'max-age=86400' + }); res.sendFile(path.join(process.cwd(), '/public/static/bundle.css.gz')); } else { + res.set('Cache-Control', 'max-age=86400'); res.sendFile(path.join(process.cwd(), '/public/static/bundle.css')); } }); staticFiles.get('*.jpg', (req, res) => { + res.set('Cache-Control', 'max-age=31536000'); res.sendFile(path.join(process.cwd(), '/public/static', req.url)) });