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');
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

View File

@ -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))
});