Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gzip and webpack compression

I can not make gzip work. I wanna make gzip files beforehand. I do that with compression-webpack-plugin. I use this files on server the usual way.

app.use(Express.static(path.join(__dirname, '../', 'dist')))

app.get('*', (req: Object, res: Object) => {
  res.render('index')
})

And I refrence those files in my templaate.

<head>
    <meta charset="UTF-8" />
    <title>Q</title>
      <link rel='stylesheet' type='text/css' href="stylesLocal.29kf81a60pl57850llfi.js.gz">
</head>

  <body>
      <div id="app"><%- app %></div>
      <script src="bundle.2720b1a98103167676ac.js.gz"></script>
      <script src="vendor.57erz1a981hk5786756u.js.gz"></script>
  </body>
</html>

Everything works if I don't gzip the files but when i send .gz files it breaks. I am reading that I should set Content-Encoding: gzip and Content-Type and I tried that but whatever file content-type I put it complains since I am sending css, js and text file. Don't know how to make this work?

like image 843
Igor-Vuk Avatar asked Oct 29 '22 06:10

Igor-Vuk


1 Answers

It sounds like you already have .gz files on the server. If you want Express to serve them, you need to us something like connect-gzip-static: https://github.com/pirxpilot/connect-gzip-static

How it works

We start by locating all compressed files (ie. files with .gz and .br extensions) in root directory. All HTTP GET and HTTP HEAD requests with Accept-Encoding header set to gzip are checked against the list of compressed files and, if possible, fulfilled by returning the compressed versions. If compressed version is not found or if the request does not have an appropriate Accept- Encoding header, the request is processed in the same way as standard static middleware would handle it.

like image 163
olore Avatar answered Nov 17 '22 13:11

olore