Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

umi 打包配置gzip #6

Open
simonyouth opened this issue Nov 7, 2019 · 0 comments
Open

umi 打包配置gzip #6

simonyouth opened this issue Nov 7, 2019 · 0 comments

Comments

@simonyouth
Copy link
Owner

simonyouth commented Nov 7, 2019

gzip是可以服务端仅配置ngnix来实现,这与前端包没有关系。

响应流程:Ngnix接受请求->将资源以gzip格式压缩->返回给浏览器->浏览器解压->渲染
ngnix.conf

 gzip  on;
 gzip_types text/plain application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

gzip也可以前后端都配置。需要前端提供.gz格式的包

响应流程:

if(*.gz) return *.gz;
// 如果配置了gzip: on
return gzip(resource);
// 如果gzip: off;
// return resource;

使用compression-webpack-plugin进行gzip的压缩。在umi里使用:
.umirc.ts

export default {
  chainWebpack(config) {
     config
      .plugin('compression-webpack')
      .use(CompressionPlugin,  [{
        deleteOriginalAssets: true, // 是否删除压缩前的文件,看情况配置
        algorithm: 'gzip', // 压缩算法,默认就是gzip
        test: /\.js(\?.*)?$/i, // 根据情况配置,此处仅压缩.js
      }])
  }
}

打包后的文件
pack-in-gzip

ngnix.conf(在之前的基础上添加, 根据情况开启gzip: on)

gzip_static on;

效果图

配置成功gzip后的header
success-config-gzip
命令行测试 curl -I http://localhost/umi.js

未压缩
without-gzip

ngnix压缩
gzip-only-in-ngnix
前端压缩(gzip_static)
static-gzip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant