Browse Source

perf: implement caching of static files

zyachel 3 years ago
parent
commit
170fbabe5e
2 changed files with 8 additions and 2 deletions
  1. 5 1
      app.js
  2. 3 1
      config.env.template

+ 5 - 1
app.js

@@ -38,7 +38,11 @@ app.use(
 app.set('view engine', 'pug'); // setting pug as a view engine
 app.set('views', path.join(__dirname, 'views/pug')); // directory from where html template will be sourced
 
-app.use(express.static(path.join(__dirname, 'public'))); // directory from where files like css, images, fonts, will be sourced
+app.use(
+  express.static(path.join(__dirname, 'public'), {
+    maxAge: process.env.CACHE_PERIOD || '1h',
+  })
+); // directory from where files like css, images, fonts, will be sourced
 
 if (process.env.NODE_ENV === 'development') app.use(morgan('dev')); // for logging requests
 // app.use(express.json({ limit: '3mb' })); // for parsing json

+ 3 - 1
config.env.template

@@ -5,4 +5,6 @@ URL=http://localhost:3000
 # enviorment
 NODE_ENV=production
 # change image quality
-IMAGE_QUALITY=500
+IMAGE_QUALITY=500
+# cache duration for static assets(eg: '2.5 days', 3600, 2m)
+CACHE_PERIOD=1h