From 920f480a577c562071ff68e169c233152c2abb74 Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Tue, 10 Dec 2019 17:05:30 -0500 Subject: [PATCH] add cache-control header to font and image requests --- lbrytv/index.js | 2 ++ lbrytv/middleware/cache-control.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 lbrytv/middleware/cache-control.js diff --git a/lbrytv/index.js b/lbrytv/index.js index cae893d7e..148cf35a1 100644 --- a/lbrytv/index.js +++ b/lbrytv/index.js @@ -5,6 +5,7 @@ const serve = require('koa-static'); const logger = require('koa-logger'); const router = require('./src/routes'); const redirectMiddleware = require('./middleware/redirect'); +const cacheControlMiddleware = require('./middleware/cache-control'); const app = new Koa(); const DIST_ROOT = path.resolve(__dirname, 'dist'); @@ -20,6 +21,7 @@ app.use(async (ctx, next) => { }); app.use(logger()); +app.use(cacheControlMiddleware); app.use(redirectMiddleware); app.use(serve(DIST_ROOT)); // Check if the request url matches any assets inside of /dist diff --git a/lbrytv/middleware/cache-control.js b/lbrytv/middleware/cache-control.js new file mode 100644 index 000000000..6ff2373f7 --- /dev/null +++ b/lbrytv/middleware/cache-control.js @@ -0,0 +1,15 @@ +const THREE_MONTHS_IN_SECONDS = 7776000; + +async function redirectMiddleware(ctx, next) { + const { + request: { url }, + } = ctx; + + if (url.includes('.png') || url.includes('font.css') || url.includes('.woff')) { + ctx.set('Cache-Control', `public, max-age=${THREE_MONTHS_IN_SECONDS}`); + } + + return next(); +} + +module.exports = redirectMiddleware;