Vue gzip压缩导致js无法解析 Content-Type: text/html(JS内容)(压缩完成是xxx.js.gz)

压缩配置 Vue.config.js

插件compression-webpack-plugin

// gzip压缩

    config.plugin('compressionPlugin')
    .use(
      // 代码混淆
      new CompressionWebpackPlugin({
        // eslint-disable-next-line
        filename: "[file].gz[query]",
        algorithm: 'gzip',
        test: /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i,
        threshold: 10240,
        minRatio: 1,
        deleteOriginalAssets: true
      })
    )
    

压缩后的js是xxx.js.gz
image.png

NGINX配置

# gzip 配置
gzip on; #开启或关闭gzip on off
gzip_disable "msie6"; #不使用gzip IE6
gzip_min_length 100k; #gzip压缩最小文件大小,超出进行压缩(自行调节)
gzip_buffers 4 16k; #buffer 不用修改
gzip_comp_level 8; #压缩级别:1-10,数字越大压缩的越好,时间也越长
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; #  压缩文件类型    

# histroy路由模式配置
location ^~/xxx/driver {
        root   /xxx/static_resource;
        index  index.html index.htm;
        try_files $uri $uri/ /xxx/driver/index.html;    
    }

实际请求返回内容 js文件
image.png

image.png

1. 问题1为什么没返回js文件是因为 压缩后的是js.gz 项目里依旧引用的js 所以路径错误么?

2. 是因为nginx配置错误么?所以没走gzip

阅读 3.9k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题