Skip to main content
better code
Source Link
master_dodo
  • 1.4k
  • 3
  • 21
  • 35

With the help of this plugin and as was suggested here I got it working

My code: Ensure that you've pre-gzipped .js and .css files

const checkForHTML = req => { const url = req.url.split('.'); const extension = url[url.length -1]; if (['/'].indexOf(extension) > -1) { return true; //compress only .html files sent from server } return false; }; var compress = require('compression'); app.use(compress({filter: checkForHTML})); // Move this as a different express module const encodeResToGzip = contentType => (req, res, contentTypenext) => { req.url = req.url + '.gz'; res.set('Content-Encoding', 'gzip'); res.set('Content-Type', contentType);    next(); }; app.get("*.js", function (req, res, next) { encodeResToGzip(req, res, 'text/javascript'); next(); });   app.get('*"*.css', function(req, rescss", next) { encodeResToGzip(req, res, 'text/css'); next(); }); 

I wanted compression to happen only for .html because I'm using .ejs template, so need to compress .html on runtime. Compressing static files(js/css) using express compression isn't good idea because it will do it on every request and those are static files. Or else, cache your results as suggested here

Other solution using nginx, as you posted in your comments also seems nice.

With the help of this plugin and as was suggested here I got it working

My code: Ensure that you've pre-gzipped .js and .css files

const checkForHTML = req => { const url = req.url.split('.'); const extension = url[url.length -1]; if (['/'].indexOf(extension) > -1) { return true; //compress only .html files sent from server } return false; }; var compress = require('compression'); app.use(compress({filter: checkForHTML})); // Move this as a different express module const encodeResToGzip = (req, res, contentType) => { req.url = req.url + '.gz'; res.set('Content-Encoding', 'gzip'); res.set('Content-Type', contentType); }; app.get("*.js", function (req, res, next) { encodeResToGzip(req, res, 'text/javascript'); next(); });   app.get('*.css', function(req, res, next) { encodeResToGzip(req, res, 'text/css'); next(); }); 

I wanted compression to happen only for .html because I'm using .ejs template, so need to compress .html on runtime. Compressing static files(js/css) using express compression isn't good idea because it will do it on every request and those are static files. Or else, cache your results as suggested here

Other solution using nginx, as you posted in your comments also seems nice.

With the help of this plugin and as was suggested here I got it working

My code: Ensure that you've pre-gzipped .js and .css files

const checkForHTML = req => { const url = req.url.split('.'); const extension = url[url.length -1]; if (['/'].indexOf(extension) > -1) { return true; //compress only .html files sent from server } return false; }; var compress = require('compression'); app.use(compress({filter: checkForHTML})); const encodeResToGzip = contentType => (req, res, next) => { req.url = req.url + '.gz'; res.set('Content-Encoding', 'gzip'); res.set('Content-Type', contentType);    next(); }; app.get("*.js", encodeResToGzip('text/javascript')); app.get("*.css", encodeResToGzip('text/css')); 

I wanted compression to happen only for .html because I'm using .ejs template, so need to compress .html on runtime. Compressing static files(js/css) using express compression isn't good idea because it will do it on every request and those are static files. Or else, cache your results as suggested here

Other solution using nginx, as you posted in your comments also seems nice.

added 395 characters in body
Source Link
master_dodo
  • 1.4k
  • 3
  • 21
  • 35

With the help of this plugin and as was suggested here I got it working

My code: Ensure that you've pre-gzipped .js and .css files

const checkForHTML = req => { const url = req.url.split('.'); const extension = url[url.length -1]; if (['/'].indexOf(extension) > -1) { return true; //compress only .html files sent from server } return false; }; var compress = require('compression'); app.use(compress({filter: checkForHTML})); // Move this as a different express module const encodeResToGzip = (req, res, contentType) => { req.url = req.url + '.gz'; res.set('Content-Encoding', 'gzip'); res.set('Content-Type', contentType); }; app.get("*.js", function (req, res, next) { encodeResToGzip(req, res, 'text/javascript'); next(); }); app.get('*.css', function(req, res, next) { encodeResToGzip(req, res, 'text/css'); next(); }); 

I wanted compression to happen only for .html because I'm using .ejs template, so need to compress .html on runtime. Compressing static files(js/css) using express compression isn't good idea because it will do it on every request and those are static files. Or else, cache your results as suggested here

Other solution using nginx, as you posted in your comments also seems nice.

With the help of this plugin and as was suggested here I got it working

My code: Ensure that you've pre-gzipped .js and .css files

const checkForHTML = req => { const url = req.url.split('.'); const extension = url[url.length -1]; if (['/'].indexOf(extension) > -1) { return true; //compress only .html files sent from server } return false; }; var compress = require('compression'); app.use(compress({filter: checkForHTML})); // Move this as a different express module const encodeResToGzip = (req, res, contentType) => { req.url = req.url + '.gz'; res.set('Content-Encoding', 'gzip'); res.set('Content-Type', contentType); }; app.get("*.js", function (req, res, next) { encodeResToGzip(req, res, 'text/javascript'); next(); }); app.get('*.css', function(req, res, next) { encodeResToGzip(req, res, 'text/css'); next(); }); 

With the help of this plugin and as was suggested here I got it working

My code: Ensure that you've pre-gzipped .js and .css files

const checkForHTML = req => { const url = req.url.split('.'); const extension = url[url.length -1]; if (['/'].indexOf(extension) > -1) { return true; //compress only .html files sent from server } return false; }; var compress = require('compression'); app.use(compress({filter: checkForHTML})); // Move this as a different express module const encodeResToGzip = (req, res, contentType) => { req.url = req.url + '.gz'; res.set('Content-Encoding', 'gzip'); res.set('Content-Type', contentType); }; app.get("*.js", function (req, res, next) { encodeResToGzip(req, res, 'text/javascript'); next(); }); app.get('*.css', function(req, res, next) { encodeResToGzip(req, res, 'text/css'); next(); }); 

I wanted compression to happen only for .html because I'm using .ejs template, so need to compress .html on runtime. Compressing static files(js/css) using express compression isn't good idea because it will do it on every request and those are static files. Or else, cache your results as suggested here

Other solution using nginx, as you posted in your comments also seems nice.

Source Link
master_dodo
  • 1.4k
  • 3
  • 21
  • 35

With the help of this plugin and as was suggested here I got it working

My code: Ensure that you've pre-gzipped .js and .css files

const checkForHTML = req => { const url = req.url.split('.'); const extension = url[url.length -1]; if (['/'].indexOf(extension) > -1) { return true; //compress only .html files sent from server } return false; }; var compress = require('compression'); app.use(compress({filter: checkForHTML})); // Move this as a different express module const encodeResToGzip = (req, res, contentType) => { req.url = req.url + '.gz'; res.set('Content-Encoding', 'gzip'); res.set('Content-Type', contentType); }; app.get("*.js", function (req, res, next) { encodeResToGzip(req, res, 'text/javascript'); next(); }); app.get('*.css', function(req, res, next) { encodeResToGzip(req, res, 'text/css'); next(); });