使用webpack的Vue.js不能提供压缩的GZIP.js文件。

问题描述 投票:0回答:1

我的服务器不向客户端提供GZIPd JavaScript文件。

我有一个简单的Vue.js应用程序,托管在Heroku上。当我通过控制台中的 "npm run build "来构建网站时,它为每个JavaScript文件填充了4个文件的distjs目录,正如我所期望的那样。

因此,举个例子。

chunk-vendors.e26db277.js
chunk-vendors.e26db277.js.gz
chunk-vendors.e26db277.js.map
chunk-vendors.e26db277.js.map.gz

为了启用压缩功能,我用下面的命令安装了webpack。

    npm install --save-dev compression-webpack-plugin

然后我设置了 vue.config.js 到下面。

    const CompressionPlugin = require('compression-webpack-plugin');

    module.exports = {
      chainWebpack(config) {
        config.plugins.delete('prefetch');


        config.plugin('CompressionPlugin').use(CompressionPlugin);
      }
    };

基本上我是按照这个教程来的https:/medium.com@aetherus.zhouvue-cli-3-performance-optimization-55316dcd491c

当我在浏览器中检查HTTP请求时,它说它接受gzip。

Accept-Encoding: gzip, deflate, br

我被卡住的地方是,让服务器实际传送.gz文件。

在教程中,它说 "这样一个静态网站的服务器块是这样的。

    server {
      listen 80;
      server_name www.example.io;  
      root /path/to/the/directory;
      index index.html;
      gzip_static on;

      location /index.html {
        etag on;
      }
      location / {
        etag off;
        add_header Cache-Control max-age=315360000, immutable;
      }
    }

但我在哪里可以找到这个块呢?

这是我的server.js。

const express = require('express')
const serveStatic = require('serve-static')
const path = require('path')

const app = express()

//here we are configuring dist to serve app files
app.use('/', serveStatic(path.join(__dirname, '/dist')))

// this * route is to serve project on different page routes except root `/`
app.get(/.*/, function (req, res) {
  res.sendFile(path.join(__dirname, '/dist/index.html'))
})

const port = process.env.PORT || 8080;
app.listen(port);
javascript node.js vue.js webpack gzip
1个回答
1
投票

服务器块是NGINX的典范。

当使用express时,必须安装一个Node.js压缩中间件。

例如。

$ npm install compression

服务器的js必须调整如下:

const compression = require('compression') // <-- import this library
const express = require('express')
const serveStatic = require('serve-static')
const path = require('path')

const app = express()

// use compression
app.use(compression()) // <-- use the library
[...]
© www.soinside.com 2019 - 2024. All rights reserved.