Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 849
Ferdinand Torggler Avatar asked Dec 23 '13 11:12

Ferdinand Torggler


1 Answers

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.

like image 158
Pierre Broucz Avatar answered Oct 27 '22 00:10

Pierre Broucz