Skip to content

Commit

Permalink
fix: Added Cache-Control header to static files so cloudflare will ca…
Browse files Browse the repository at this point in the history
…che them correctly
  • Loading branch information
CaramelKat committed Jul 29, 2024
1 parent 1bd9146 commit 27af873
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/services/juxt-web/routes/console/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,40 @@ const express = require('express');
const router = express.Router();
const path = require('path');

//* Keep the cache for 1 hour
const maxAge = 60 * 60;

router.get('/', function (req, res) {
res.redirect('/titles/show');
});

router.get('/css/:filename', function (req, res) {
res.set('Content-Type', 'text/css');
res.set('Cache-Control', `public, max-age=${maxAge}`);
res.sendFile('/css/' + req.params.filename, {root: path.join(__dirname, '../../../../webfiles/' + req.directory)});
});

router.get('/js/:filename', function (req, res) {
res.set('Content-Type', 'application/javascript; charset=utf-8');
res.set('Cache-Control', `public, max-age=${maxAge}`);
res.sendFile('/js/' + req.params.filename, {root: path.join(__dirname, '../../../../webfiles/' + req.directory)});
});

router.get('/images/:filename', function (req, res) {
res.set('Content-Type', 'image/png');
res.set('Cache-Control', `public, max-age=${maxAge}`);
res.sendFile('/images/' + req.params.filename, {root: path.join(__dirname, '../../../../webfiles/' + req.directory)});
});

router.get('/fonts/:filename', function (req, res) {
res.set('Content-Type', 'font/woff');
res.set('Cache-Control', `public, max-age=${maxAge}`);
res.sendFile('/fonts/' + req.params.filename, {root: path.join(__dirname, '../../../../webfiles/' + req.directory)});
});

router.get('/favicon.ico', function (req, res) {
res.set('Content-Type', 'image/x-icon');
res.set('Cache-Control', `public, max-age=${maxAge}`);
res.sendFile('/images/favicon.ico', {root: path.join(__dirname, '../../../../webfiles/' + req.directory)});
});

Expand Down

0 comments on commit 27af873

Please sign in to comment.