Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress build with/without ejecting create-react-app. Also include compressed file into script tag into index.html

I'm using react-scripts build to make production ready build and to deploy it on AWS server I'm using

GENERATE_SOURCEMAP=false yarn build && aws s3 sync build/ s3://*********

Is there any way to apply gzip compression to build files and include gzipped files into index.html and deploy it on aws so that in browser gzipped files would be served.

like image 566
harish kumar Avatar asked Apr 16 '19 09:04

harish kumar


People also ask

How compress React js build?

Use Gzip and Brotli Compression js, style. css. Brotli provides better compression than gzip. In our experience, Brotli compressed JS files are 14% smaller than gzip, HTML files are 21% smaller than gzip.

Does create React app use gzip?

Compress-create-react-appPerforms gzip and brotli compression for html, css and js files. In case the compression is not sufficient, or its configuration is not good, it is possible to use one of the procedures below.

Should I eject from Create React app?

Ejecting your React app has major tradeoffs. It gives you all the configuration you want, but it's time-consuming, expensive, and requires a lot of knowledge. With CRA, you can have a React app without needing to understand every piece of configuration.


1 Answers

Install gzipper package (https://www.npmjs.com/package/gzipper). Modify build command like this

"build": "react-scripts build && gzipper --verbose ./build"

and build your project you will get both gzip and regular file. It is upto you to make server to serve gzip instead of regular file. If you are using nginx you have to setup your server in nginx.conf file as below

server {
        # Gzip Settings
        gzip on;
        gzip_static on; # allows pre-serving of .gz file if it exists 
        gzip_disable "msie6"; # Disable for user-agent Internet explorer 6. Not supported.
        gzip_proxied any; # enable gzip for all proxied requests
        gzip_buffers 16 8k; # number and size of buffers to compress a response
        gzip_http_version 1.1;
        gzip_min_length 256; # Only gzip files of size in bytes
        gzip_types text/plain text/css text/html application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
        gunzip on; # Uncompress on the fly
        listen       4000;
        server_name  localhost;



        location / {
            root   html/react-app;
            index  index.html index.htm;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


    }
like image 155
Shreejan Acharya Avatar answered Sep 29 '22 13:09

Shreejan Acharya