Skip to content

Commit

Permalink
add static file cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mawburn committed Jul 26, 2024
1 parent e5dfa06 commit 0f25dec
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,15 @@ FIREBASE_APP_ID=
ALLOW_SHARED_LINKS=true
ALLOW_SHARED_LINKS_PUBLIC=true

#==============================#
# Static File Cache Control #
#==============================#

# Leave commented out to use default of 1 month for max-age and 1 week for s-maxage
# NODE_ENV must be set to production for these to take effect
# STATIC_CACHE_MAX_AGE=604800
# STATIC_CACHE_S_MAX_AGE=259200

#===================================================#
# UI #
#===================================================#
Expand Down
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"bcryptjs": "^2.4.3",
"cheerio": "^1.0.0-rc.12",
"cohere-ai": "^7.9.1",
"compression": "^1.7.4",
"connect-redis": "^7.1.0",
"cookie": "^0.5.0",
"cors": "^2.8.5",
Expand Down
9 changes: 6 additions & 3 deletions api/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require('module-alias')({ base: path.resolve(__dirname, '..') });
const cors = require('cors');
const axios = require('axios');
const express = require('express');
const compression = require('compression');
const passport = require('passport');
const mongoSanitize = require('express-mongo-sanitize');
const { jwtLogin, passportLogin } = require('~/strategies');
Expand All @@ -17,6 +18,7 @@ const configureSocialLogins = require('./socialLogins');
const AppService = require('./services/AppService');
const noIndex = require('./middleware/noIndex');
const routes = require('./routes');
const staticCache = require('./utils/staticCache');

const { PORT, HOST, ALLOW_SOCIAL_LOGIN } = process.env ?? {};

Expand All @@ -38,14 +40,15 @@ const startServer = async () => {
app.get('/health', (_req, res) => res.status(200).send('OK'));

// Middleware
app.use(compression());
app.use(noIndex);
app.use(errorController);
app.use(express.json({ limit: '3mb' }));
app.use(mongoSanitize());
app.use(express.urlencoded({ extended: true, limit: '3mb' }));
app.use(express.static(app.locals.paths.dist));
app.use(express.static(app.locals.paths.fonts));
app.use(express.static(app.locals.paths.assets));
app.use(staticCache(app.locals.paths.dist));
app.use(staticCache(app.locals.paths.fonts));
app.use(staticCache(app.locals.paths.assets));
app.set('trust proxy', 1); // trust first proxy
app.use(cors());

Expand Down
3 changes: 2 additions & 1 deletion api/server/routes/static.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const express = require('express');
const staticCache = require('../utils/staticCache');
const paths = require('~/config/paths');

const router = express.Router();
router.use(express.static(paths.imageOutput));
router.use(staticCache(paths.imageOutput));

module.exports = router;
19 changes: 19 additions & 0 deletions api/server/utils/staticCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require('express');

const oneWeekInSeconds = 24 * 60 * 60 * 7;

const sMaxAge = process.env.STATIC_CACHE_S_MAX_AGE || oneWeekInSeconds;
const maxAge = process.env.STATIC_CACHE_MAX_AGE || oneWeekInSeconds * 4;

const staticCache = (staticPath) =>
express.static(staticPath, {
setHeaders: (res) => {
if (process.env.NODE_ENV.toLowerCase() !== 'production') {
return;
}

res.setHeader('Cache-Control', `public, max-age=${maxAge}, s-maxage=${sMaxAge}`);
},
});

module.exports = staticCache;
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0f25dec

Please sign in to comment.