3

I'm trying to follow this in order to serve some static compressed files. https://medium.com/@rajaraodv/two-quick-ways-to-reduce-react-apps-size-in-production-82226605771a

I'm using webpack compression plugin to generate the gzip files.

new CompressionPlugin({ asset: '[path].gz[query]', algorithm: 'gzip', test: /\.(js|css)$/, deleteOriginalAssets: true }) 

On my server I have this middleware.

app.get('*.js', function (req, res, next) { req.url = req.url + '.gz'; res.set('Content-Encoding', 'gzip'); next(); }); 

When I'm running the app, in browser I'm getting GET http://localhost:8080/app.js net::ERR_CONTENT_DECODING_FAILED

Probably, I still have to do something more but no idea what exactly.

Thank you, Ionut

1 Answer 1

2

Looks like you're missing the content type. Use this code in your express server:

app.get('*.js', function(req, res, next) { req.url = req.url + '.gz'; res.set('Content-Encoding', 'gzip'); res.set('Content-Type', 'text/javascript'); next(); }); app.get('*.css', function(req, res, next) { req.url = req.url + '.gz'; res.set('Content-Encoding', 'gzip'); res.set('Content-Type', 'text/css'); next(); }); 
Sign up to request clarification or add additional context in comments.

4 Comments

What will i see in the browser network tab, will it be compressed version (.gz) or normal (.js)
the browser uncompresses .gz files so I'm assuming it will be .js
Thanks. One last point to clarify, In browser's network tab i can see .js bundle getting loaded and size is also same. Is it because it gets decompressed or anything wrong? My middleware is supplying .gz version but network tab shows .js with original size not compressed. Is there any docs having these details to go through? Thanks for the help.
i'm not too sure what's going on. I'm not that familiar with zip files. It sounds like your middleware isn't working. I'm pretty sure you should be seeing .gz files in your browser. I don't know of any docs to read. If you check your network tab and look under the headers from the server response, if it has something like content-encoding: gzip or something like that, then it's doing its job. But I think you might investigate further.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.