How to gzip JavaScript and CSS assets in Sails.js?

express, gzip, node.js, sails.js

Solution

i recommend you to read this github issue ( Enable Express compression in Sails.js by default ), and this stackoverflow answer ( Add express middleware for param validations ) for further details and sailsjs framework way.

Hope this help, and don't hesitate to ask more details if you encounter some difficulties.

Problem

I am trying to enable gzip compression for my assets in a Sails.js (Node) application. When launching the app in production environment, all the assets in `assets/linker/js` and `assets/linker/styles` are concatenated, minified, uglified successfully (as specified in the Gruntfile). The following output files are generated: ``` .tmp/ public/ min/ production.js production.css ``` I would like to add gzip compression as well, therefore i have installed grunt-contrib-compress and added the compress task to the Gruntfile. I can get the following results with successfully gzipped files. ``` .tmp/ public/ min/ production.js production.js.gz production.css production.css.gz ``` My problem is now that the server needs to respond with the gzipped files (when the client accepts the encoding) instead of the normal ones and I can't find a way to do this. Maybe policies are the sails-way to do something like this? Or is there another way to use express middleware? If it helps, this is the current sails-linker task which inserts the javascript production.js file into the markup: ``` ... prodJs: { options: { startTag: '<!--SCRIPTS-->', endTag: '<!--SCRIPTS END-->', fileTmpl: '<script src="%s"></script>', appRoot: '.tmp/public' }, files: { '.tmp/public/**/*.html': ['.tmp/public/min/production.js'], 'views/**/*.html': ['.tmp/public/min/production.js'], 'views/**/*.ejs': ['.tmp/public/min/production.js'] } }, ... ``` Many thanks in advance.

Original source