如何在Sails.js中gzip JavaScript和CSS资源? [英] How to gzip JavaScript and CSS assets in Sails.js?

查看:138
本文介绍了如何在Sails.js中gzip JavaScript和CSS资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Sails.js(Node)应用程序中为我的资产启用gzip压缩。在生产环境中启动应用程序时, assets / linker / js assets / linker / styles 中的所有资源都是连接,缩小,成功(如Gruntfile中指定)。生成以下输出文件:

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

我也想添加gzip压缩,因此我已经安装了grunt-contrib-compress并将压缩任务添加到Gruntfile中。我可以使用成功的gzip压缩文件获得以下结果。

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

我的问题是,服务器需要使用gzip压缩文件(当客户端接受编码)而不是正常的文件时,我不能找到一种方法来做到这一点。也许政策是这样做的方式吗?还有另一种方式来使用快速中间件?

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?

如果有帮助,这是当前的sails-linker任务,将javascript production.js文件插入到标记中: / p>

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']
    }
  },

  ...

非常感谢。

推荐答案

我建议您阅读此github问题( Enab le默认情况下在Sails.js中进行快速压缩),并且此stackoverflow答案(添加快速中间件进行参数验证)以获取更多详细信息和sailsjs框架方式。

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.

这篇关于如何在Sails.js中gzip JavaScript和CSS资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆