Implement cache control

This commit is contained in:
LordMathis 2018-06-25 14:27:25 +02:00
parent 1e23e7df65
commit 087f3691fb
2 changed files with 16 additions and 4 deletions

View File

@ -5,11 +5,13 @@ const path = require('path');
const config = require('./config.json'); const config = require('./config.json');
api.get('/blog', (req, res) => { api.get('/blog', (req, res) => {
res.set('Cache-Control', 'no-cache');
res.json(data.posts); res.json(data.posts);
}); });
api.get('/about', (req, res) => { api.get('/about', (req, res) => {
const renderPath = path.join(process.cwd(), '/renders', 'about.html'); const renderPath = path.join(process.cwd(), '/renders', 'about.html');
res.set('Cache-Control', 'max-age=86400');
fs.readFile(renderPath, 'utf8', (err, data) => { fs.readFile(renderPath, 'utf8', (err, data) => {
if (err) { if (err) {
res.json({ res.json({
@ -24,6 +26,7 @@ api.get('/about', (req, res) => {
}); });
api.get('/post/:postname', (req, res) => { api.get('/post/:postname', (req, res) => {
res.set('Cache-Control', 'no-cache');
const postname = req.params.postname; const postname = req.params.postname;
const post = data.posts.find((el) => { const post = data.posts.find((el) => {
return el.filename === postname return el.filename === postname

View File

@ -3,25 +3,34 @@ const path = require('path');
staticFiles.get('/bundle.js', (req, res) => { staticFiles.get('/bundle.js', (req, res) => {
if (req.acceptsEncodings('gzip')) { if (req.acceptsEncodings('gzip')) {
res.set('Content-Encoding', 'gzip'); res.set({
res.set('Content-Type', 'text/javascript'); 'Content-Encoding': 'gzip',
'Content-Type': 'text/javascript',
'Cache-Control': 'max-age=86400'
});
res.sendFile(path.join(process.cwd(), '/public/static/bundle.js.gz')); res.sendFile(path.join(process.cwd(), '/public/static/bundle.js.gz'));
} else { } else {
res.set('Cache-Control', 'max-age=86400');
res.sendFile(path.join(process.cwd(), '/public/static/bundle.js')); res.sendFile(path.join(process.cwd(), '/public/static/bundle.js'));
} }
}); });
staticFiles.get('/bundle.css', (req, res) => { staticFiles.get('/bundle.css', (req, res) => {
if (req.acceptsEncodings('gzip')) { if (req.acceptsEncodings('gzip')) {
res.set('Content-Encoding', 'gzip'); res.set({
res.set('Content-Type', 'text/css'); 'Content-Encoding': 'gzip',
'Content-Type': 'text/css',
'Cache-Control': 'max-age=86400'
});
res.sendFile(path.join(process.cwd(), '/public/static/bundle.css.gz')); res.sendFile(path.join(process.cwd(), '/public/static/bundle.css.gz'));
} else { } else {
res.set('Cache-Control', 'max-age=86400');
res.sendFile(path.join(process.cwd(), '/public/static/bundle.css')); res.sendFile(path.join(process.cwd(), '/public/static/bundle.css'));
} }
}); });
staticFiles.get('*.jpg', (req, res) => { staticFiles.get('*.jpg', (req, res) => {
res.set('Cache-Control', 'max-age=31536000');
res.sendFile(path.join(process.cwd(), '/public/static', req.url)) res.sendFile(path.join(process.cwd(), '/public/static', req.url))
}); });